【问题标题】:NoSuchMethodError in apache camel javaapache骆驼java中的NoSuchMethodError
【发布时间】:2020-10-16 18:04:35
【问题描述】:

我运行了以下代码:

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();
        }
}

用于将 input_box 文件夹中的数据转换为 activemq。

我收到以下错误:

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)

上面代码的第 18 行: 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>

请帮忙。

【问题讨论】:

  • 听起来你的类路径上有混合版本的 Camel JAR。确保只使用相同的版本。
  • org.apache.camelcamel-core2.14.4org.apache.camelcamel-jms2.24.0org.apache.camelcamel-activemq3.0.0

标签: apache-camel activemq


【解决方案1】:

根据您的 POM,您可以混合使用 3 个不同的 Camel 版本:2.14.4、2.24.0 和 3.0.0。

您必须对所有 Camel 组件使用相同的版本,正如 @claus-ibsen 已经评论的那样。

例如像下面的例子那样做(使用框架版本的属性并在其所有依赖项中使用它)。

但是,正如 Sneharghya 已经回答的那样,Camel 2.x 没有 camel-activemq,而是可以使用 ActiveMQ 的依赖项 activemq-camel

因此,POM 应该如下所示。但我认为 Camel 版本可能会有所不同。

<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>

您也可以使用Maven dependencyManagement

【讨论】:

  • 如何获得相同的版本?请告诉我,我是 apache camel 框架的新手。提前致谢。
  • 我更新了我的答案。这实际上不是 Camel 而是 Maven 问题
  • 更新后会出现另一个错误,但是对于上面的 pom.xml activemq 版本与其他 2 个依赖项不同。所以我相应地更新了。现在出现以下错误:线程“main”java.lang 中的异常.NoSuchMethodError: org.apache.activemq.camel.component.ActiveMQComponent.getMessageCreatedStrategy()Lorg/apache/camel/component/jms/MessageCreatedStrategy;在 context.start();在上面的代码中。
  • 抱歉,我没有注意到新的camel-activemq 组件。我修正了我的答案,但NoSuchMethodError 仍然是一个混合版本问题
【解决方案2】:

1.更新课程 有两个问题。

  • 您在一个名称jms 中注册您的组件并将消​​息发送到不同的组件activemq。它们应该相同。
  • 您正在执行 while 循环,而不是 while 循环多次启动上下文。
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。替换pom中的依赖如下:

<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>

【讨论】:

  • 嗨,我只想使用activemq。请告诉这些示例的code.bec' 我从互联网上下载了activemq并运行了它。
  • 现在我试了一下。没有运行时错误/异常。但没有输出。我的项目资源管理器中有 input_box 文件夹,我正在创建 1 个文本文件,我必须转到 activemq 服务器(localhost:8161/admin/queues.jsp),here 文件未填充。
  • 文件消费消息不断出现,我认为activemq有问题。不确定。
【解决方案3】:

camel-activemq 不适用于 3.0 之前的版本。 如果您想继续使用 camel 2.24.3,请从您的 pom 文件中删除 camel-activemq 依赖项并添加

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

【讨论】:

  • 如上更新后,现在出现以下错误。线程“main”中的异常java.lang.NoSuchMethodError: org.apache.activemq.camel.component.ActiveMQComponent.getMessageCreatedStrategy()Lorg/apache /camel/component/jms/MessageCreatedStrategy;,在 context.start().
  • 其实我用的是activemq 5.15.4版本,这个请告诉我camel core.in maven pom.xml的版本。
  • 使用 activemq-camel 版本 5.15.4。 Camel 2.24.3 会好的!
  • 不,它不工作,再次出现异常。对我来说 .Activemq:5.8.0 ,camel core-2.12.1 没有错误(在运行时也是)。但我的 Activemq 版本是 5.15 .4 所以没有输出。所以请告诉 Activemq 5.15.4,camel core 的确切版本是什么?在 maven pom 中。
  • 用骆驼2.19.5试试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-08
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-13
  • 2019-02-22
相关资源
最近更新 更多