简体   繁体   English

在JMS队列上发布消息?

[英]Publishing message on JMS queue?

i am new to JMS and going thru the example of Active MQ Hello world . 我是JMS的新手,以Active MQ Hello world为例。 Say i have a scenario whenever i make entry under employee table in DB, i have to put the message in queue.here is the producer code snippet from hello world example 假设我有一个场景,每当我在DB的雇员表下输入条目时,都必须将消息放入队列中。这是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();
            }
        }
    }

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(ie "TEST.FOO") second time , do i need to call createMessageOnQueue method second time. 第二个问题是我是否需要第二次将消息发布在同一队列(即“ TEST.FOO”)上,是否需要第二次调用createMessageOnQueue方法。 If yes, will it not create new queue with session.createQueue("TEST.FOO")? 如果是,它将不会使用session.createQueue("TEST.FOO")?创建新队列session.createQueue("TEST.FOO")?

The queue is created once and only you can delete it manually. 队列仅创建一次,只有您可以手动将其删除。 Once the message is sent to a queue, it will wait on the queue until it's consumed (unlike topics). 消息发送到队列后,它将在队列中等待直到消息被消耗(与主题不同)。

You don't need to re-create the message if you want to send it twice. 如果要发送两次,则无需重新创建消息。 But then again, why would you send it two times? 但是话又说回来,为什么还要发送两次?

I feel that your problem might be solved using JMS transactions. 我认为可以使用JMS事务解决您的问题。

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? 如果是,如果还没有使用消息,将会发生什么?

message will still be on queue. 消息仍在排队。 No such thing as 'closing a queue'. 没有“关闭队列”之类的事情。

Second question is if i need to publish the message on same queue(ie "TEST.FOO") second time , do i need to call createMessageOnQueue method second time. 第二个问题是我是否需要第二次将消息发布在同一队列(即“ TEST.FOO”)上,是否需要第二次调用createMessageOnQueue方法。 If yes, will it not create new queue with session.createQueue("TEST.FOO")? 如果是,它将不会使用session.createQueue(“ TEST.FOO”)创建新队列?

session.createQueue("TEST.FOO") does not necessarily create queue, it just get a reference to existing queue. session.createQueue(“ TEST.FOO”)不一定创建队列,它只是获取对现有队列的引用。

javadoc of session#createQueue() session#createQueue()的javadoc

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提供程序中创建物理主题。 JMS does not provide a method to create the physical topic, since this would be specific to a given JMS provider. JMS不提供创建物理主题的方法,因为这将特定于给定的JMS提供程序。 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. 创建物理主题是特定于提供程序的,并且通常是管理员执行的管理任务,尽管某些提供程序可能会在需要时自动创建它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM