我是JMS的新手,并通过Active MQ Hello world的示例。说我有一个场景,每当我做入门 在DB员工的桌子底下,我必须把消息queue.here是从Hello World示例现在在JMS队列上发布消息?
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();
}
}
}
我的问题生产者代码片段是,如果我关闭连接和会话,它会关闭队列吗?如果是的话,如果消息还没有被消费,会发生什么?
第二个问题是,如果我需要第二次在同一队列(即“TEST.FOO”)上发布消息,是否需要第二次调用createMessageOnQueue
方法。如果是,是否不会创建新的队列session.createQueue("TEST.FOO")?