[英]Unable to access ActiveMQ using JMS based code and amqp 1.0
我正在嘗試使用AMQP 1.0連接到ActiveMQ代理,但是我想在我的應用程序代碼中使用JMS。 我對使用JMS感興趣,主要是因為我希望開發人員能夠使用他們已經熟悉的API。
我有在本地主機上運行的ActiveMQ 5.14.0和以下代碼:
public static void main(String[] args) throws JMSException, InterruptedException {
Connection connection = null;
try {
// Producer
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("amqp://localhost:5672");
connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("customerTopic");
// Publish
MessageProducer producer = session.createProducer(topic);
for ( int i = 0; i < 10; i++) {
Message msg = session.createTextMessage("Task : " + i);
producer.send(msg);
}
session.close();
} finally {
if (connection != null) {
connection.close();
}
}
}
代碼總是以相同的方式失敗,並且在stacktrace中具有以下根本原因:
Caused by: org.apache.activemq.transport.InactivityIOException: Channel was inactive for too (>30000) long: tcp://127.0.0.1:5672
這發生在connection.start()
方法調用上。
如果我對ActiveMQ tcp端點運行相同的代碼,則它將按預期執行。
我的pom文件依賴關系如下(並且我懷疑這是我的問題的根源,因為我發現依賴關系的文檔極其難以遵循)
<dependencies>
<dependency>
<groupId>org.apache.qpid</groupId>
<artifactId>qpid-amqp-1-0-client-jms</artifactId>
<version>0.32</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-amqp</artifactId>
<version>5.14.0</version>
</dependency>
</dependencies>
我的直接問題是“為什么不起作用?”。
我的補充(基於觀點)問題是“嘗試使用AMQP 1.0之上的JMS抽象值得嗎,還是我應該放棄學習提供商特定的API?”
最好與jndi合作
public static void main(String[] args) throws JMSException, InterruptedException, NamingException {
Connection connection = null;
try {
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.qpid.jms.jndi.JmsInitialContextFactory");
props.setProperty("connectionfactory.myFactoryLookup",
"amqp://localhost:5672");
props.put("topic." + "MyTOPIC", "customerTopic");
InitialContext ic = new InitialContext(props);
ConnectionFactory cf1 = (ConnectionFactory) ic.lookup("myFactoryLookup");
Topic topic = (Topic) ic.lookup("MyTOPIC");
connection = cf1.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(topic);
connection.start();
for (int i = 0; i < 10; i++) {
Message msg = session.createTextMessage("Task : " + i);
producer.send(msg);
}
session.close();
} finally {
if (connection != null) {
connection.close();
}
}
}
更換
<dependency>
<groupId>org.apache.qpid</groupId>
<artifactId>qpid-amqp-1-0-client-jms</artifactId>
<version>0.32</version>
</dependency>
通過
<dependency>
<groupId>org.apache.qpid</groupId>
<artifactId>qpid-jms-client</artifactId>
<version>0.9.0</version>
</dependency>
在經紀人方面,您需要添加:
<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?transport.transformer=jms"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.