2015-10-10 62 views
0

我有一個特定主題的發件人和收件人。我將發送者和接收者作爲WAS 7.0上的servlet運行。 正在WAS上設置TopicTopic Connection Factory。但我無法收到發送的消息。當我嘗試使用Queue而不是Topic時,它工作正常。JMS - 在WebSphere Application Server上使用主題7

下面是我使用的代碼。

public class CommonServlet extends HttpServlet{ 

    private static final long serialVersionUID = 1L; 
    private static boolean initialized = false; 
    private static InitialContext initialContext = null; 
    private ConnectionFactory connectionFactory = null; 
    private Destination destination = null; 

    protected final void initialize(){ 
     try{ 
      if(initialized == false){ 
       // Get JNDI initial context 
       initialContext = new InitialContext(); 

       // Flag as initialized 
       initialized = true; 
      } 
     } 
     catch(Throwable t){ 
      System.out.println(t.getMessage()); 
     } 
    } 

    /** 
    * @return 
    */ 
    protected final Destination getDestination(){ 
     if(destination != null){ 
      return destination; 
     } 

     try{ 
      destination = (Destination) initialContext.lookup("jms/TestTopic"); 
     } 
     catch(NamingException e){ 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return destination; 
    } 

    /** 
    * @return 
    */ 
    protected final ConnectionFactory getConnectionFactory(){ 
     try{ 
      connectionFactory = (ConnectionFactory) initialContext.lookup("jms/TestTopicCF"); 
     } 
     catch(NamingException e){ 
      e.printStackTrace(); 
     } 
     return connectionFactory; 
    } 
} 

發件人servlet類

public class Sender extends CommonServlet{ 

    private static final long serialVersionUID = 1L; 

    @Override 
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ 
     System.out.println("inside do get of sender"); 
     doPost(req, resp); 
    } 

    @Override 
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ 
     String message = req.getParameter("message"); 
     sendMessage(message); 
    } 

    private void sendMessage(String messageText){ 
     initialize(); 
     try{ 
      ConnectionFactory factory = getConnectionFactory(); 

      Destination destination = getDestination(); 

      Connection connection = factory.createConnection(); 
      connection.start(); 

      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 

      MessageProducer sender = session.createProducer(destination); 

      TextMessage txtMsg = session.createTextMessage(messageText); 

      sender.send(txtMsg); 
     } 
     catch(Exception ex){ 
      ex.printStackTrace(); 
     } 
    } 
} 

接收機servlet類

public class Receiver extends CommonServlet{ 

    private static final long serialVersionUID = 1L; 


    @Override 
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ 
     doPost(req, resp); 
    } 

    @Override 
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ 
     getMessage(); 
    } 

    private void getMessage(){ 
     initialize(); 
     ConnectionFactory factory = getConnectionFactory(); 

     Destination destination = getDestination(); 

     try{ 
      Connection connection = factory.createConnection(); 

      connection.start(); 

      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 

      MessageConsumer receiver = session.createConsumer(destination); 

      Message message = receiver.receive(4000); 

      System.out.println(message);//COMING AS NULL 
     } 
     catch(JMSException e){ 
      e.printStackTrace(); 
     } 

    } 

} 

兩個TestTopicTestTopicCF在WAS配置管理控制檯下資源> JMS>主題連接工廠主題部分。運行應用程序時沒有例外。 如果我使用創建的隊列和隊列連接工廠,它工作正常。 有什麼我需要專門去做主題工作?

回答

1

當發佈者發送消息時,主題是與隊列不同的目標,默認情況下它們不會持續消息,並且訂閱者必須連接。查看一些詳細信息here

發佈者和訂閱者具有時間依賴性。 訂閱主題的客戶端只能使用 客戶端創建訂閱之後發佈的消息,並且訂閱者必須繼續使用 才能使用消息。

因此,在短期:

  • 當前的設計是不正確的主題,你就必須先調用接收器的servlet,已經有很長的接收超時,並在第二個窗口中嘗試發送者的servlet ,因爲你現在的信息只是丟失了。
  • 更好的方法是使用MDB作爲消息接收器,而不是servlet
  • 如果您需要接收消息發送到主題,而訂閱者處於非活動狀態,則需要配置持久訂閱者並配置主題在WAS中持久。
相關問題