1

I am trying to convert my code to lambda version and I get java.lang.ArrayIndexOutOfBoundsException: 18 exception when I am building the code.

This is the working code:

    private void run(){
        Producer<String, String> producer = (Producer<String, String>) new KafkaProducer();
        producer.send(null, new Callback() {
            public void onCompletion(RecordMetadata metadata, Exception e) {  }
        });
    }

This code gives the error:

    private void run(){
        Producer<String, String> producer = (Producer<String, String>) new KafkaProducer();
        producer.send(null, (metadata,e) -> { });
    }

This is the Error message:

java.lang.ArrayIndexOutOfBoundsException: 18
at aQute.lib.osgi.Clazz.parseClassFile(Clazz.java:412)
at aQute.lib.osgi.Clazz.parseClassFile(Clazz.java:333)
at aQute.lib.osgi.Clazz.parseClassFileWithCollector(Clazz.java:323)
at aQute.lib.osgi.Clazz.parseClassFile(Clazz.java:313)
at aQute.lib.osgi.Analyzer.analyzeJar(Analyzer.java:1646)
at aQute.lib.osgi.Analyzer.analyzeBundleClasspath(Analyzer.java:1534)
at aQute.lib.osgi.Analyzer.analyze(Analyzer.java:115)
at aQute.lib.osgi.Builder.analyze(Builder.java:296)
at aQute.lib.osgi.Analyzer.calcManifest(Analyzer.java:292)
at aQute.lib.osgi.Builder.build(Builder.java:68)
at org.apache.felix.bundleplugin.BundlePlugin.buildOSGiBundle(BundlePlugin.java:375)
at org.apache.felix.bundleplugin.BundlePlugin.execute(BundlePlugin.java:270)
at org.apache.felix.bundleplugin.BundlePlugin.execute(BundlePlugin.java:224)
at org.apache.felix.bundleplugin.BundlePlugin.execute(BundlePlugin.java:215)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:957)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:193)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
[ERROR] Error building bundle com.wso2telco:logging-extension:bundle:3.0.0 : Exception: 18
[ERROR] Error building bundle com.wso2telco:logging-extension:bundle:3.0.0 : Invalid class file: MessageSender$KafkaDataPublisher.class
[ERROR] Error(s) found in bundle configuration

Please note that for simplifications most of the code is removed.

Full code after simplification:(This is working code)

public class MessageSender {

    ExecutorService executorService = null;

    public MessageSender(ExecutorService executorService) {
       this.executorService = executorService;
    }

    public void sendMessage(String transactionLog) {
       Runnable worker = new KafkaDataPublisher();
       executorService.execute(worker);
    }

    public static class KafkaDataPublisher implements Runnable {  
       @Override
       public void run() {
          Producer<String, String> producer = (Producer<String, String>) new KafkaProducer();
          producer.send(null, new Callback() {
              public void onCompletion(RecordMetadata metadata, Exception e) {}
        });
       }
    }

}

Versions:

Java 8

1 Answer 1

0

Have you tried like this:

private void run(){
    Producer<String, String> producer = (Producer<String, String>) new KafkaProducer();
    producer.send(null, (RecordMetadata metadata, Exception e) -> { });
}
Sign up to request clarification or add additional context in comments.

2 Comments

What about like this: producer.send(null).onCompletion( (RecordMetadata metadata, Exception e) -> { } );
Thanks Carlos for the help, I am unable to find a method similar to onCompletion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.