由於某些原因,我無法創建持久使用者以從activemq中接收消息。我研究了here和here,但使用最新版本(以及Java 1.7)尚未解決問題。我必須使用錯誤的版本/實現某些東西,但我一直無法將其解決。下面是代碼:當我嘗試調用org.apache.activemq.ActiveMQSession.createDurableConsumer時,爲什麼會收到java.lang.AbstractMethodError
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import org.apache.activemq.ActiveMQConnectionFactory;
ConnectionFactory cf = new ActiveMQConnectionFactory("bob", "bobsPword", "tcp://localhost:61616");
Connection connection = cf.createConnection();
connection.start();
Session session = connection.createSession(false, QueueSession.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("TEST.QUEUE.FOO");
MessageConsumer consumer = session.createDurableConsumer(topic, "clientId");
Message message = consumer.receiveNoWait();
if (message instanceof TextMessage) {
System.out.println("message: " + ((TextMessage) message).getText());
}else{
System.out.println("unexpected message...");
}
錯誤:
Exception in thread "main" java.lang.AbstractMethodError: org.apache.activemq.ActiveMQSession.createDurableConsumer(Ljavax/jms/Topic;Ljava/lang/String;)Ljavax/jms/MessageConsumer;
我的依賴關係:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.fusesource.stompjms</groupId>
<artifactId>stompjms-client</artifactId>
<version>1.19</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>5.10.0</version>
</dependency>
</dependencies>
您可能在編譯時和運行時使用不同的jar。嘗試從出現錯誤的代碼中打印類路徑,您可能會遇到令人驚訝的事情。 – StephaneM