收到消息后关闭JMS会话和连接

时间:2015-05-11 09:10:45

标签: java jms

我有一个有状态会话bean,我发送和接收JMS消息。所有连接设置都是手动处理的,因此bean保存了javax.jms.connection和javax.jms.session的实例。该bean还实现了MessageListener以便能够接收消息。

现在,当我发送消息时,我使用session.createTemporaryQueue()创建一个临时队列。我将message.setJMSReplyTo()设置为同一个临时队列,最后创建了此队列的使用者,并将MessageListener设置为实现所有这些的相同的有状态会话bean。

我很高兴收到onMessage()方法的消息。但是,我想在收到消息后立即关闭会话和连接,这显然是onMessage()方法中不允许的。

所以问题是: 收到消息后,如何关闭会话和连接?我必须手动处理连接设置,不能使用MDB。

请注意: 这在Java EE环境(GlassFish 4.0)

中执行

编辑:

import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.inject.Inject;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import com.sun.messaging.ConnectionConfiguration;
import com.sun.messaging.QueueConnectionFactory;

@LocalBean
@Stateful
public class OpenMqClient implements MessageListener{
    private Connection connection;
    private Session session;
    private MessageConsumer responseConsumer;

    public OpenMqClient(){}

    public void sendMessage(String messageContent, String jmsBrokerUri, String queueName) {
        try{
            String host = System.getProperty("foo", jmsBrokerUri);
            QueueConnectionFactory cf = new QueueConnectionFactory();
            cf.setProperty(ConnectionConfiguration.imqAddressList, host);
            connection = null;
            session = null;

            //Setup connection
            connection = cf.createConnection();
            connection.start();
            session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

            //Setup queue and producer
            Queue queue = session.createQueue(queueName);
            MessageProducer producer = session.createProducer(queue);


            //Reply destination
            Queue responseQueue = session.createTemporaryQueue();
            responseConsumer = session.createConsumer(responseQueue);
            responseConsumer.setMessageListener(this);

            //Create message
            TextMessage textMessage = session.createTextMessage();
            textMessage.setJMSReplyTo(responseQueue);
            textMessage.setJMSCorrelationID("test0101");
            textMessage.setText(messageContent);

            producer.send(textMessage);
            System.out.println("Message sent");
        } catch (JMSException e) {
            e.printStackTrace();
            System.out.println("JMSException in Sender");
        }
    }

    @Override
    public void onMessage(Message arg0) {
        //On this event I want to close the session and connection, but it's not permitted
    }

}

1 个答案:

答案 0 :(得分:0)

就个人而言,我就是这样做的(注意我还没有测试过,或者对这段代码添加了很多错误处理)。

  1. 使连接成为静态 - 您可以(可能应该)为所有bean重用相同的连接,除非您有特定的理由不
  2. 在新线程中关闭会话

    public class OpenMqClient implements MessageListener {
    
        private static Connection connection;
        private static final String mutex = "mutex"; 
        private Session session;
        private MessageConsumer responseConsumer;
    
        public OpenMqClient() {
            if(connection == null) {
                synchronized(mutex) {
                    if(connection == null) {
                        String host = System.getProperty("foo", jmsBrokerUri);
                        QueueConnectionFactory cf = new QueueConnectionFactory();
                        cf.setProperty(ConnectionConfiguration.imqAddressList, host);
    
                        // Setup connection
                        connection = cf.createConnection();
                        connection.start();
                    }
                }
            }
        }
    
        public void sendMessage(String messageContent, String jmsBrokerUri, String queueName) {
            try {
                session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    
                // Setup queue and producer
                Queue queue = session.createQueue(queueName);
                MessageProducer producer = session.createProducer(queue);
    
                // Reply destination
                Queue responseQueue = session.createTemporaryQueue();
                responseConsumer = session.createConsumer(responseQueue);
                responseConsumer.setMessageListener(this);
    
                // Create message
                TextMessage textMessage = session.createTextMessage();
                textMessage.setJMSReplyTo(responseQueue);
                textMessage.setJMSCorrelationID("test0101");
                textMessage.setText(messageContent);
    
                producer.send(textMessage);
                System.out.println("Message sent");
            } catch (JMSException e) {
                e.printStackTrace();
                System.out.println("JMSException in Sender");
            }
        }
    
        @Override
        public void onMessage(Message arg0) {
            // do stuff
            new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        if(session != null)
                            try {
                                session.close();
                            } catch (JMSException e) {
                                e.printStackTrace();
                            }               
                    }
                }
            ).start();
        }
    }