NoSuchMethodError with ActiveMQ

2019-03-06 11:50发布

java.lang.NoSuchMethodError: org.apache.activemq.thread.TaskRunnerFactory.setThreadClassLoader(Ljava/lang/ClassLoader;)V
    at org.apache.activemq.broker.BrokerService.getTaskRunnerFactory(BrokerService.java:1265)
    at org.apache.activemq.broker.BrokerService.createRegionBroker(BrokerService.java:2346)
    at org.apache.activemq.broker.BrokerService.createBroker(BrokerService.java:2305)
    at org.apache.activemq.broker.BrokerService.getBroker(BrokerService.java:1017)
    at org.apache.activemq.broker.BrokerService.getAdminConnectionContext(BrokerService.java:2576)
    at org.apache.activemq.broker.BrokerService.startVirtualConsumerDestinations(BrokerService.java:2717)
    at org.apache.activemq.broker.BrokerService.startDestinations(BrokerService.java:2567)
    at org.apache.activemq.broker.BrokerService.doStartBroker(BrokerService.java:726)
    at org.apache.activemq.broker.BrokerService.startBroker(BrokerService.java:720)
    at org.apache.activemq.broker.BrokerService.start(BrokerService.java:623)
    at com.bp.pnc.publisher.app.PncPublisherApplication.main(PncPublisherApplication.java:77)

The code I am using is

BrokerService broker = new BrokerService();
TransportConnector connector = new TransportConnector();
connector.setUri(new URI("tcp://localhost:61616"));
broker.addConnector(connector);
broker.start();

The problem occurs at broker.start() method. I am using activemq 5.14.0. I am using Java 7. I looked at the documentation and exact line where this is happening.

this.taskRunnerFactory.setThreadClassLoader(this.getClass().getClassLoader());

1条回答
在下西门庆
2楼-- · 2019-03-06 12:32

There are different reasons why this error can occur:

  • You run your application with an older activemq.jar than the one you used for compiling your source
  • Your application has more jars in its classpath and one of them contains classes of activemq as well (because it's using classes for itself). If that jar is loaded before your activemq.jar (i.e. it appears "in front" of the acitvemq.jar) the older version is in use.

If the latter, you can put the following code into your class (before the code you shown in your question) to see where the class is loaded from:

Class clazz = TaskRunnerFactory.class;
String name = clazz.getName().replace('.', '/') + ".class";
String loc = clazz.getClassLoader().getResource(name).toString();
System.out.println(loc);
查看更多
登录 后发表回答