http://www-128.ibm.com/developerworks/cn/websphere/techjournal/0204_minocha/minocha.html
可以实现监听,但是我有个问题,实现监听的过程中网络断掉,则程序不能监听,需要重新启动,怎么解决这个问题?期待交流。

解决方案 »

  1.   

    //同步接收消息的点对点(PTP)
          if (qsession == null) {
            InitialContext ctx = JMSContext.getInitialContext();
            System.out.println("-----------"+JMS_FACTORY) ;
            qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
            qcon = qconFactory.createQueueConnection();
            qcon.start();
            qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            queue = (Queue) ctx.lookup(queueName);
            System.out.println("-----------"+queueName) ;
            qsender=qsession.createSender(queue);
            qreceiver = qsession.createReceiver(queue);
            qreceiver.setMessageListener(this);
            qreceiver.receive();
            qcon.start();
            //add code as follow
            while (! listener.messageReceived ) 

    try 

    // pause for a second to share the CPU 
    Thread.currentThread().sleep(1000); 

    catch (InterruptedException ie) 
    {
         System.out.println( "ERROR: " + ie );
         System.out.println("LinkedException: " + ie.toString());    
              System.exit(-1);
               } 
    }       }
          //使用监听器的订阅/发布(pub/sub)
          /*
          if (tsession == null) {
            InitialContext ctx = JMSContext.getContext();
            tconFactory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY);
            tcon = tconFactory.createTopicConnection();
            tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
            topic = (Topic) ctx.lookup(topicName);
            tsubscriber = tsession.createSubscriber(topic,value,true);
            tsubscriber.setMessageListener(this);
            tcon.start();//***************************************
    //add code as follow
            while (! listener.messageReceived ) 

    try 

    // pause for a second to share the CPU 
    Thread.currentThread().sleep(1000); 

    catch (InterruptedException ie) 
    {
         System.out.println( "ERROR: " + ie );
         System.out.println("LinkedException: " + ie.toString());    
              System.exit(-1);
               } 

    //**************************************************
          }
      

  2.   

    public static boolean messageReceived = false;