2012-07-20 125 views
5

我目前正在爲持久訂閱編寫一個messenger服務(它可能最終會變得不耐用,我們仍在討論這一點),並且正在尋找一些關於如何處理我們的服務器由於某種原因暫時關閉的場景,我們需要自動重新訂閱該主題。下面是它如何連接的示例代碼:設置JMS連接以便自動重新連接的理想方法

public void DurableChatter(String broker, String username, String password) 
{ 
    javax.jms.MessageProducer publisher = null; 
    javax.jms.MessageConsumer subscriber = null; 
    javax.jms.Topic topic = null; 

    //Create a connection: 
    try{ 
     javax.jms.ConnectionFactory factory; 
     factory = (new progress.message.jclient.ConnectionFactory (broker)); 
     connection = factory.createConnection (username, password); 

     //Durable Subscriptions are indexed by username, clientID and subscription name 
     //It is a good proactice to set the clientID: 
     connection.setClientID(CLIENT_ID); 
     pubSession = connection.createSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE); 
     subSession = connection.createSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE); 
    } 
    catch (javax.jms.JMSException jmse){ 
     System.err.println ("Error: Cannot connect to Broker - " + broker); 
     jmse.printStackTrace(); 
     System.exit(1); 
    } 

    //Create Publisher and Durable Subscriber: 
    try{ 

     topic = pubSession.createTopic(APP_TOPIC); 
     subscriber = subSession.createDurableSubscriber(topic, "SampleSubscription"); 
     subscriber.setMessageListener(this); 
     publisher = pubSession.createProducer(topic); 
     connection.start(); 
    } 
    catch (javax.jms.JMSException jmse){ 
     System.out.println("Error: connection not started."); 
     jmse.printStackTrace(); 
     System.exit(1); 
    } 

    //Wait for user input 

    try 
    { 
     System.out.println("Enter text to send as message and press enter."); 
     java.io.BufferedReader stdin = 
      new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); 
     while (true) 
     { 
      String s = stdin.readLine(); 

      if(s == null){ 
       exit(); 
      } 
      else if (s.length()>0) 
      { 
       try 
       { 
        javax.jms.TextMessage msg = pubSession.createTextMessage(); 
        msg.setText(username + ": " + s); 
        //Publish the message persistantly: 
        publisher.send(
         msg,        //message 
         javax.jms.DeliveryMode.PERSISTENT, //publish persistantly 
         javax.jms.Message.DEFAULT_PRIORITY,//priority 
         MESSAGE_LIFESPAN);     //Time to Live 
       } 
       catch (javax.jms.JMSException jmse){ 
        System.err.println("Error publishing message:" + jmse.getMessage()); 
       } 
      } 
     } 
    } 
    catch (java.io.IOException ioe) 
    { 
     ioe.printStackTrace(); 
    } 
} 

回答

0

你需要多快的故障檢測? 設置您的協議,以確保每個客戶端至少每分鐘發送一條消息(您需要向通信協議添加一些新的「絨毛」保持消息) - 任何未收到保持消息的客戶端都可以安全地承擔服務器關閉並開始重新連接。

理想情況下,這種事情最好用UDP​​廣播,而不是JMS(用於開銷),但我假設你有UDP廣播作爲選項,你會使用jgroups來做羣集檢測/故障轉移/重新加入您。

+0

如果這就是你要求的,它不一定是瞬間的。無論如何,這可以通過try/catch塊來完成嗎? – Icebreaker 2012-07-20 17:16:14

+0

僅當底層連接斷開時引發某種異常。我不能不告訴你。 – radai 2012-07-22 20:03:32

3

你應該讓你的客戶implement javax.jmsExceptionListener

這將允許您的客戶端在連接丟失時即時接收來自JMS API的回調,即使您的應用程序目前不綁定發佈任何內容。

創建Connection後,連接並啓動它,請撥打connection.setExceptionListener(myListener)。另請參閱Javadoc Connection