乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      深入掌握J(rèn)MS(七):DeliveryMode例子

       moonboat 2009-07-12

      深入掌握J(rèn)MS(七):DeliveryMode例子

          在下面的例子中,分別發(fā)送一個Persistent和nonpersistent的消息,然后關(guān)閉退出JMS。

      import javax.jms.Connection;
      import javax.jms.DeliveryMode;
      import javax.jms.MessageProducer;
      import javax.jms.Queue;
      import javax.jms.Session;
      import org.apache.activemq.ActiveMQConnectionFactory;
      import org.apache.activemq.command.ActiveMQQueue;

      public class DeliveryModeSendTest {

          public static void main(String[] args) throws Exception {
              ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost");
         
              Connection connection = factory.createConnection();
              connection.start();
             
              Queue queue = new ActiveMQQueue("testQueue");
              Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                     
              MessageProducer producer = session.createProducer(queue);
              producer.setDeliveryMode(DeliveryMode.PERSISTENT);
              producer.send(session.createTextMessage("A persistent Message"));
             
              producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
              producer.send(session.createTextMessage("A non persistent Message"));
             
              System.out.println("Send messages sucessfully!");
          }
      }

          運(yùn)行上面的程序,當(dāng)輸出“Send messages sucessfully!”時,說明兩個消息都已經(jīng)發(fā)送成功,然后我們結(jié)束它,來停止JMS Provider。

          接下來我們重新啟動JMS Provicer,然后添加一個消費(fèi)者:

      import javax.jms.Connection;
      import javax.jms.JMSException;
      import javax.jms.Message;
      import javax.jms.MessageConsumer;
      import javax.jms.MessageListener;
      import javax.jms.Queue;
      import javax.jms.Session;
      import javax.jms.TextMessage;
      import org.apache.activemq.ActiveMQConnectionFactory;
      import org.apache.activemq.command.ActiveMQQueue;

      public class DeliveryModeReceiveTest {

          public static void main(String[] args) throws Exception {
              ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost");
         
              Connection connection = factory.createConnection();
              connection.start();
             
              Queue queue = new ActiveMQQueue("testQueue");
              Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
             
              MessageConsumer comsumer = session.createConsumer(queue);
              comsumer.setMessageListener(new MessageListener(){
                  public void onMessage(Message m) {
                      try {
                          System.out.println("Consumer get " + ((TextMessage)m).getText());
                      } catch (JMSException e) {
                          e.printStackTrace();
                      }
                  }
              });
          }
      }

      運(yùn)行上面的程序,可以得到下面的輸出結(jié)果:

      Consumer get A persistent Message

      可以看出消息消費(fèi)者只接收到一個消息,它是一個Persistent的消息。而剛才發(fā)送的non persistent消息已經(jīng)丟失了。

      另外, 如果發(fā)送一個non persistent消息, 而剛好這個時候沒有消費(fèi)者在監(jiān)聽, 這個消息也會丟失.

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多