我有一个用@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);
}
}
所有初始化都在静态块中完成。
所以我希望连接只要服务器正在侦听就可以了,但我希望在服务器关闭时显式关闭它。球衣是否有办法让你处理近距离赛事?
答案 0 :(得分: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);
}
}
}