2014-11-18 2 views
1

Привет я добавил этот класс Weblistener к моему WebProjectActiveMQ не может получить мое сообщение

@WebListener 
public class SelfSend implements ServletContextListener { 

    private MessageProducer producer; 
    private Connection sendconnection; 
    private Connection receiveconnection; 
    private Session sendsession; 
    private Session receivesession; 
    private MessageConsumer receiver; 

    @Override 
    public void contextInitialized(ServletContextEvent arg0) { 
     try { 
      InitialContext initCtx = new InitialContext(); 
      ConnectionFactory connectionFactory = (ConnectionFactory) initCtx.lookup("java:comp/env/jms/ConnectionFactory"); 
      sendconnection = connectionFactory.createConnection(); 
      receiveconnection = connectionFactory.createConnection(); 
      sendsession = sendconnection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
      receivesession = receiveconnection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
      producer = sendsession.createProducer((Destination) initCtx.lookup("java:comp/env/jms/queue/MyQueue")); 
      receiver = receivesession.createConsumer((Destination) initCtx.lookup("java:comp/env/jms/queue/MyQueue")); 
      receiver.setMessageListener(new MessageListener() { 
       @Override 
       public void onMessage(Message message) { 
        System.out.println("MESSAGE RECEIVED"); 

       } 
      }); 
      TextMessage testMessage = sendsession.createTextMessage(); 
      testMessage.setStringProperty("from", "ki"); 
      producer.send(testMessage); 
      System.out.println("MESSAGE SENT"); 
     } catch (Exception e) { 
      throw new RuntimeException(e); 
     } 

    } 

    @Override 
    public void contextDestroyed(ServletContextEvent arg0) { 

    } 

} 

Но сообщение никогда не получил.

Когда я поставил Reciver в @WebServlet как этот

@Override 
public void init(ServletConfig config) throws ServletException { 
    super.init(config); 
    try { 
     InitialContext initCtx = new InitialContext(); 
     ConnectionFactory connectionFactory = (ConnectionFactory) initCtx.lookup("java:comp/env/jms/ConnectionFactory"); 
     connection = connectionFactory.createConnection(); 
     connection.start(); 
     session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
     receiver = session.createConsumer((Destination) initCtx.lookup("java:comp/env/jms/queue/MyQueue")); 
     receiver.setMessageListener(this); 
    } catch (Exception e) { 
     throw new RuntimeException(e); 
    } 
    mud = new MongoUserdata(); 
} 

я Recive сообщение, когда я положил его в обоих я получаю только каждое второе сообщение с Servlet-приемник, другой messasge, кажется, Потерянный.

Может ли кто-нибудь объяснить это странное поведение для меня?

ответ

0

В первом классе примера вы, похоже, не запускаете соединение с ресивером, что означает, что он не будет отправлять какие-либо сообщения, которые были получены. Тем не менее, он будет удерживать входящие сообщения в буфере предварительной выборки потребителей, что приведет к тому, что все остальные полученные сообщения получат.

Смежные вопросы