gpt4 book ai didi

java - MessageConsumer接收方法未从远程机器队列读取消息

转载 作者:行者123 更新时间:2023-11-30 06:11:59 24 4
gpt4 key购买 nike

public class MessageReceiver {
// URL of the JMS server
private static String url = "tcp://atuleusbduv012.aemud.com:61616";
// default broker URL is : tcp://localhost:61616"
// Name of the queue we will receive messages from
private static String subject = "activemq.test.incoming.master";
// Queue Name.You can create any/many queue names as per your requirement.
public static void main(String[] args) throws JMSException, InterruptedException {
// Getting JMS connection from the server
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("agile","123456789",url);
//ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();
// Creating session for sending messages
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
System.out.println("session :: "+session.toString());
// Getting the queue 'amd.tmpesb.sapphire.incoming.master'
Destination destination = session.createQueue(subject);
System.out.println("destination :: "+destination.toString());
// MessageConsumer is used for receiving (consuming) messages
MessageConsumer consumer = session.createConsumer(destination);
System.out.println("consumer :: "+consumer.toString());
// Here we receive the message.
Message message = consumer.receive();
System.out.println("message :: "+message.toString());
// We will be using TestMessage in our example. MessageProducer sent us a TextMessage
// so we must cast to it to get access to its .getText() method.
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Received message '" + textMessage.getText() + "'");
}
connection.close();
}
}

上面的代码没有从队列中读取消息,当我调试java类时,我发现,“执行在 consumer.receive(); 方法处停止,即它不断尝试读取消息但无法阅读。请让我知道,我在上面的 MessageReceiver 类中缺少什么

还有一些有关向上述队列发送/写入消息的更多详细信息。我们正在使用下面的类将消息发送到队列activemq.test.incoming.master并且正在成功发送消息

public class MessageSender {    
//URL of the JMS server. DEFAULT_BROKER_URL will just mean that JMS server is on localhost
private static String url = "tcp://atuleusbduv012.aemud.com:61616";
//private static String url = "tcp://localhost:61616";
// default broker URL is : tcp://localhost:61616"
private static String subject = "activemq.test.incoming.master";
// Queue Name.You can create any/many queue names as per your requirement.
public static void main(String[] args) throws JMSException {
// Getting JMS connection from the server and starting it
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("agile","123456789",url);
//ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();
//Creating a non transactional session to send/receive JMS message.
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
//Destination represents here our queue 'JCG_QUEUE' on the JMS server.
//The queue will be created automatically on the server.
Destination destination = session.createQueue(subject);
// MessageProducer is used for sending messages to the queue.
MessageProducer producer = session.createProducer(destination);
// We will send a small text message saying 'Hello World!!!'
TextMessage message = session.createTextMessage("Hello !!! Welcome to the world of ActiveMQ.");
// Here we are sending our message!
producer.send(message);
System.out.println("JCG printing@@ '" + message.getText() + "'");
connection.close();
}
}

我请求您让我知道上面的 MessageReceiver 类中缺少什么,因为它没有从队列activemq.test.incoming.master读取消息>.

最佳答案

我使用线程概念来读取消息并将消息写入队列。引用链接http://activemq.apache.org/hello-world.html其中receiver可以等待一段时间才能收到消息consumer.receive(3000)并且在尝试接收等待一段时间后使用 Thread.sleep(300) 产生更多消息通过它,我们将能够在远程机器中的队列中读取和写入消息。谢谢大家

关于java - MessageConsumer接收方法未从远程机器队列读取消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50045022/

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