NoSuchMethodError in apache camel java

Viewed 2757

I ran the following code:

package com.dinesh.example4;

import javax.jms.ConnectionFactory;
//import org.apache.camel.support.HeaderFilterStrategyComponent;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.Component;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;
//import org.apache.camel.impl.*;

public class FileToActiveMQ {
    
    public static void main(String[] args) throws Exception {
        CamelContext context = new DefaultCamelContext();
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        context.addComponent("jms",JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
        context.addRoutes(new RouteBuilder() {
            
            @Override
            public void configure() throws Exception {
              from("file:input_box?noop=true")
                .to("activemq:queue:my_queue");
                }
        });
        while(true)
        context.start();
        }
}

for transforming data from input_box folder to activemq.

I am getting the following error:

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.camel.impl.HeaderFilterStrategyComponent.<init>(Ljava/lang/Class;)V
    at org.apache.camel.component.jms.JmsComponent.<init>(JmsComponent.java:71)
    at org.apache.camel.component.jms.JmsComponent.<init>(JmsComponent.java:87)
    at org.apache.camel.component.jms.JmsComponent.jmsComponent(JmsComponent.java:102)
    at org.apache.camel.component.jms.JmsComponent.jmsComponentAutoAcknowledge(JmsComponent.java:127)
    at com.dinesh.example4.FileToActiveMQ.main(FileToActiveMQ.java:18)

Here 18 th line in above code: context.addRoutes(new RouteBuilder() {

Pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dinesh</groupId>
  <artifactId>camel-application1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  
  <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-core -->
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-core</artifactId>
    <version>2.14.4</version>
</dependency>

 <dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-jms</artifactId>
    <version>2.24.0</version>
</dependency>
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-activemq</artifactId>
    <version>3.0.0</version>
</dependency>
  
  </dependencies>
</project>

Please help.

3 Answers

According to your POM you mix 3 different Camel versions: 2.14.4, 2.24.0 and 3.0.0.

You have to use the same version for all Camel components, as @claus-ibsen already commented.

Do it for example like the example below (with properties for the framework versions and use it in all its dependencies).

However, as Sneharghya already answered, Camel 2.x has no camel-activemq but instead can use the dependency activemq-camel of ActiveMQ.

Therefore the POM should look like this. But I think the Camel version can vary.

<properties>
    <amq.version>5.15.4</amq.version>     
    <camel.version>2.19.5</camel.version> 
</properties>

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-core</artifactId>
    <version>${camel.version}</version>
</dependency>
 <dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-jms</artifactId>
    <version>${camel.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-camel</artifactId>
    <version>${amq.version}</version>
</dependency>

You can also use Maven dependencyManagement.

camel-activemq is not available for versions older than 3.0. If you want to keep using camel 2.24.3, then remove the camel-activemq dependency from your pom file and add

<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-camel</artifactId>
    <version>5.15.13</version>
</dependency>

1. Update the class There are two issues.

  • One you were registering your component in one name jms and send the message to different component activemq. They should be same.
  • You were doing a while loop and instead the while loop starting the context many times.
public class FileToActiveMQ {

  public static void main(String[] args) throws Exception {
    CamelContext context = new DefaultCamelContext();
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
    context.addComponent("activemq", 
          JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
    context.addRoutes(new RouteBuilder() {
      @Override
      public void configure() throws Exception {

        from("file:input_box?noop=true")
        .to("activemq:queue:my_queue");
      }
    });
   context.start();
   while(true) {
     Thread.sleep(10000);
   }

  }
}

2. Replace the dependencies in pom as follows:

<dependencies>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-core</artifactId>
      <version>2.25.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-jms</artifactId>
      <version>2.25.1</version>
    </dependency>

    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-client</artifactId>
      <version>5.15.7</version>
    </dependency>
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-camel</artifactId>
      <version>5.15.7</version>
    </dependency>
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-pool</artifactId>
      <version>5.15.7</version>
    </dependency>

  </dependencies>
Related