3

我有一个用 @Path 注释的类,在这个类中我有一个处理 PUT 请求的方法。在此方法中,每次调用此方法时,我都使用 ActiveMQ 将消息发布到 JMS 主题。一切正常。

但现在我想添加一些清理代码(主要是关闭 ActiveMQ 连接)。我怎样才能做到这一点?

@PUT
@Consumes(MediaType.TEXT_PLAIN)
public void putString(String myString) throws JMSException {  
   if (txtmessage != null && producer != null ){
           txtmessage.clearBody();
           txtmessage.setText(myString);
           producer.send(txtmessage);
         }    
      }

所有的初始化都是在一个静态块中完成的。

因此,只要服务器正在侦听,我希望连接保持正常,但我想在服务器关闭时显式关闭它。球衣有没有办法让你处理近距离事件?

4

1 回答 1

3

实施

ServletContextListener:http: //tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/ServletContextListener.html

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Logger;

public class MyContextListener implements ServletContextListener {
    public final static String ACTIVE_MQ_SESSION = "ActiveMQSession";
    public final static String ACTIVE_MQ_PRODUCER = "ActiveMQProducer";

    Logger logger = Logger.getLogger(this.getClass());
    private static final int ackMode = Session.AUTO_ACKNOWLEDGE;
    private static final boolean transacted = false;

    private static final String brokerUrl = "vm://localhost:61616";

    private Connection connection;
    private Session session;
    private MessageProducer producer;

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        try {
            this.producer.close();
            this.session.close();
            this.connection.close();
        } catch (JMSException e) {
            logger.warn("tearDown()", e);
        }

    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                brokerUrl);

        try {
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(transacted, ackMode);
            Destination destination = session.createQueue("queue");
            producer = session.createProducer(destination);

            ServletContext sc = sce.getServletContext();
            sc.setAttribute(ACTIVE_MQ_SESSION, session);
            sc.setAttribute(ACTIVE_MQ_PRODUCER, producer);
        } catch (JMSException e) {
            logger.warn("setup() failed to setup connection brokerUrl="
                    + brokerUrl);
        }
    }

}

在 web.xml 中注册监听器:

<web-app...>
    <listener>
        <listener-class>package.MyContextListener</listener-class>
    </listener>
</web-app>

然后是 servlet(从您使用生产者和会话的地方):

import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.http.HttpServlet;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.core.MediaType;

public class MessageServlet extends HttpServlet {

    @PUT
    @Consumes(MediaType.TEXT_PLAIN)
    public void putString(String myString) throws JMSException {
        MessageProducer producer = (MessageProducer) getServletContext()
                .getAttribute(MyContextListener.ACTIVE_MQ_PRODUCER);

        Session session = (Session) getServletContext().getAttribute(
                MyContextListener.ACTIVE_MQ_SESSION);
        TextMessage txtmessage = session.createTextMessage();
        if (txtmessage != null && producer != null) {
            txtmessage.clearBody();
            txtmessage.setText(myString);
            producer.send(txtmessage);
        }
    }
}
于 2012-11-12T06:15:22.027 回答