gpt4 book ai didi

java - 在 JMS 队列上发布消息?

转载 作者:太空宇宙 更新时间:2023-11-04 15:05:32 26 4
gpt4 key购买 nike

我是 JMS 新手,正在查看 Active MQ Hello world 的示例。假设我每次进入时都会有一个场景在数据库中的员工表下,我必须将消息放入队列中。这是 hello world 示例中的生产者代码片段

public static class HelloWorldProducer  {
public void createMessageOnQueue() {
try {
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");

// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();

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

// Create the destination (Topic or Queue)
Destination destination = session.createQueue("TEST.FOO");

// Create a MessageProducer from the Session to the Topic or Queue
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

// Create a messages
String text = "Hello world! From: " + Thread.currentThread().getName() + " : " + this.hashCode();
TextMessage message = session.createTextMessage(text);

// Tell the producer to send the message
System.out.println("Sent message: "+ message.hashCode() + " : " + Thread.currentThread().getName());
producer.send(message);

// Clean up
session.close();
connection.close();
}
catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}
}

现在我的问题是,如果我关闭连接和 session ,它也会关闭队列吗?如果是,如果消息还没有被消费会发生什么?

第二个问题是,如果我需要在同一个队列(即“TEST.FOO”)上第二次发布消息,我是否需要第二次调用 createMessageOnQueue 方法。如果是,它不会使用 session.createQueue("TEST.FOO") 创建新队列吗?

最佳答案

Now my question is if i close the connection and session, will it close the queue also? If yes,what will happen if message has not been consumed yet?

消息仍将在队列中。没有“关闭队列”这样的事情。

Second question is if i need to publish the message on same queue(i.e "TEST.FOO") second time , do i need to call createMessageOnQueue method second time. If yes, will it not create new queue with session.createQueue("TEST.FOO")?

session.createQueue("TEST.FOO") 不一定创建队列,它只是获取对现有队列的引用。

session 的javadoc#createQueue()

Note that this method simply creates an object that encapsulates the name of a topic. It does not create the physical topic in the JMS provider. JMS does not provide a method to create the physical topic, since this would be specific to a given JMS provider. Creating a physical topic is provider-specific and is typically an administrative task performed by an administrator, though some providers may create them automatically when needed.

关于java - 在 JMS 队列上发布消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22040986/

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