2012-11-08 71 views
3

我有一個用@Path標註的類,並且在這個類中我有一個處理PUT請求的方法。在這種方法中,我每次調用此方法時都使用ActiveMQ將消息發佈到JMS主題。一切正常。我應該在哪裏關閉我的activemq連接(java,jersey)

但是現在我想添加一些清理代碼(主要是關閉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

在什麼情況下運行澤西島?作爲tomcat或glassfish等的一部分? –

+0

作爲tomcat的一部分。我使用eclipse來開發這個。 – redman

+0

@redman嗨,你能否在這種情況下幫助我。請找到鏈接 - http://stackoverflow.com/questions/19706788/jersey-rest-web-service-with-activemq-middleware-integration。謝謝你的時間。 – Kumar

回答

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); 
     } 
    } 
} 
+0

請看看這個問題,並提出一些補救措施。感謝您的時間和幫助。我正在爲此而苦苦掙扎。如果您提供電子郵件ID,我可以發送示例我也正在爲您做。 http://stackoverflow.com/questions/19706788/jersey-rest-web-service-with-activemq-middleware-integration感謝收益。 – Kumar

+0

我是否應該按照您在此建議的方式進行操作?請幫忙。謝謝 – Kumar