如何使用来自另一个应用程序Java的ActiveMQ Artemis jms主题



我遇到这样一种情况,如果我在应用程序中动态呼叫我的消费者,它将使用下面的代码。如何从其他应用程序接收此消息?以下工作代码:

public class TopicExample {
public static void main(final String[] args) throws Exception {
Connection connection = null;
InitialContext initialContext = null;
try {
initialContext = new InitialContext();
Topic topic = (Topic) initialContext.lookup("topic/exampleTopic");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(topic);
MessageConsumer messageConsumer = session.createConsumer(topic);
TextMessage message = session.createTextMessage("This is a text message ");
producer.send(message);
System.out.println("Sent message: " + message.getText());
connection.start();
System.out.println("*************************************************************");
TextMessage messageReceived = (TextMessage) messageConsumer.receive(1000);
System.out.println("messageConsumer received: " + messageReceived.getText());
messageConsumer.close();
} finally {
if (initialContext != null) {
initialContext.close();
}
if (connection != null) {
connection.close();
}
}
}
}

我使用的是相同的artemis代理,如果我检查控制台,消费者和生产者会显示,但消息始终为空,TextMessage messageReceived=(TextMessage(messageConsumer.remove(1000(,如果我使用MessageConsumermessageConsumer=session.createConsumer(主题(它永远不会去那里。我在不同的应用程序中的代码不起作用,如下所示:

public class ExampleSubscriber {
public static void main(String[] args) {
Connection connection = null;
InitialContext initialContext = null;
try {
initialContext = new InitialContext();
Topic topic = (Topic) initialContext.lookup("topic/exampleTopic");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer messageConsumer = session.createConsumer(topic);
// messageConsumer.setMessageListener(new DefaultMessageListener()); does not work
// TextMessage messageReceived = (TextMessage) messageConsumer.receive(1000); 
// System.out.println(messageReceived); if i dont use listener this is always null
} catch (NamingException e) {
e.printStackTrace();
} catch (JMSException e) {
e.printStackTrace();
} finally {
if (initialContext != null) {
try {
initialContext.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
private static class DefaultMessageListener implements MessageListener {
@Override
public void onMessage(Message message) {
System.out.println(message); //never comes here when using listener
}
}
}

消费者应用程序需要启动JMS连接来接收传入消息,即

connection.start();

您必须确保消息被发送到JMS主题,而消费者正在积极侦听消息。根据JMS规范的定义,JMS主题使用者在连接时仅获得发送到主题的消息。唯一的例外是,消费者已经创建了一个持久订阅,但您没有这样做。

谢谢大家,我尝试了你们所有的建议,但由于某种奇怪的原因,它没有起作用。经过整整两个晚上的研究,只有当我使用jms 2.0实现时,它才起作用

try (ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
JMSContext jmsContext = cf.createContext()) {
//add your consumer code here for consumer
//add your producer code here for producer
}

相关内容

  • 没有找到相关文章

最新更新