gpt4 book ai didi

java - ACTIVEMQ-发布者订阅者 Hello World 示例

转载 作者:IT老高 更新时间:2023-10-28 20:51:48 26 4
gpt4 key购买 nike

有两个程序:订阅者和发布者...订阅者能够将消息放到主题上并且消息发送成功。当我在浏览器上检查 activemq 服务器时,它显示 1 msg enqueued 。但是当我运行消费者代码时,它没有收到消息

这里是生产者代码:

import javax.jms.*;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class producer {

private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

public static void main(String[] args) throws JMSException {

ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();

// JMS messages are sent and received using a Session. We will
// create here a non-transactional session object. If you want
// to use transactions you should set the first parameter to 'true'
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);

Topic topic = session.createTopic("testt");

MessageProducer producer = session.createProducer(topic);

// We will send a small text message saying 'Hello'

TextMessage message = session.createTextMessage();

message.setText("HELLO JMS WORLD");
// Here we are sending the message!
producer.send(message);
System.out.println("Sent message '" + message.getText() + "'");

connection.close();
}
}

运行此代码后,控制台的输出为:

26 Jan, 2012 2:30:04 PM org.apache.activemq.transport.failover.FailoverTransport doReconnect
INFO: Successfully connected to tcp://localhost:61616
Sent message 'HELLO JMS WORLD'

这里是消费者代码:

import javax.jms.*;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class consumer {
// URL of the JMS server
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

// Name of the topic from which we will receive messages from = " testt"

public static void main(String[] args) throws JMSException {
// Getting JMS connection from the server

ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();

Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);

Topic topic = session.createTopic("testt");

MessageConsumer consumer = session.createConsumer(topic);

MessageListener listner = new MessageListener() {
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Received message"
+ textMessage.getText() + "'");
}
} catch (JMSException e) {
System.out.println("Caught:" + e);
e.printStackTrace();
}
}
};

consumer.setMessageListener(listner);
connection.close();

}
}

运行此代码后,它不显示任何内容。有人可以帮我解决这个问题吗?

最佳答案

您的问题是您的消费者正在运行,然后立即关闭。

尝试将其添加到您的消费者中:

    consumer.setMessageListener(listner);

try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}

connection.close();

这将等到您按下一个键才停止。

其他需要考虑的事项:

  • 使用 finally block 结束
  • Java 命名约定鼓励类的首字母使用大写

关于java - ACTIVEMQ-发布者订阅者 Hello World 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9015882/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com