Package javax.jms

Examples of javax.jms.Session


      Consumer[] consumers = new Consumer[numConsumers];
     
           
      for (int i = 0; i < numRelayers; i++)
      {
         Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        
         MessageConsumer cons = sess.createConsumer(topic, "name = 'Watt'");
         //MessageConsumer cons = sess.createConsumer(topic);
        
         MessageProducer prod = sess.createProducer(topic);
        
         relayers[i] = new Relayer(prod);
        
         cons.setMessageListener(relayers[i]);
      }
     
      for (int i = 0; i < numConsumers; i++)
      {
         Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        
         MessageConsumer cons = sess.createConsumer(topic, "name = 'Tim'");
        
         consumers[i] = new Consumer();
        
         cons.setMessageListener(consumers[i]);
      }
     
      conn.start();
     
      Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
     
      MessageProducer prod = sess.createProducer(topic);
     
      prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
     
      for (int i = 0; i < numMessages; i++)
      {
         Message m = sess.createMessage();
        
         m.setStringProperty("name", "Watt");
        
         prod.send(m);
        
View Full Code Here


         if (clientID != null)
         {
            sourceConn.setClientID(clientID);
         }
         
         Session sess;
        
         if (sourceAndTargetSameServer)
         {
            //We simply use a single local transacted session for consuming and sending     
           
            sourceSession = sourceConn.createSession(true, Session.SESSION_TRANSACTED);
           
            sess = sourceSession;
         }
         else
         {
            //Source and destination are on different resource managers
           
            if (qualityOfServiceMode == QOS_ONCE_AND_ONLY_ONCE)
            {
               //Create an XASession for consuming from the source
               if (trace) { log.trace("Creating XA source session"); }
              
               sourceSession = ((XAConnection)sourceConn).createXASession();
              
               sess = ((XASession)sourceSession).getSession();
              
               usingXA = true;
            }
            else
            {
               if (trace) { log.trace("Creating non XA source session"); }
              
               //Create a standard session for consuming from the source
              
               //If the QoS is at_most_once, and max batch size is 1 then we use AUTO_ACKNOWLEDGE
               //If the QoS is at_most_once, and max batch size > 1 or -1, then we use CLIENT_ACKNOWLEDGE
               //We could use CLIENT_ACKNOWLEDGE for both the above but AUTO_ACKNOWLEGE may be slightly more
               //performant in some implementations that manually acking every time but it really depends
               //on the implementation.
               //We could also use local transacted for both the above but don't for the same reasons.
              
               //If the QoS is duplicates_ok, we use CLIENT_ACKNOWLEDGE
               //We could use local transacted, whether one is faster than the other probably depends on the
               //messaging implementation but there's probably not much in it
              
               int ackMode;
               if (qualityOfServiceMode == QOS_AT_MOST_ONCE && maxBatchSize == 1)
               {
                  ackMode = Session.AUTO_ACKNOWLEDGE;
               }
               else
               {
                  ackMode = Session.CLIENT_ACKNOWLEDGE;
               }
              
               sourceSession = sourceConn.createSession(false, ackMode);
              
               sess = sourceSession;
            }
         }
           
         if (subName == null)
         {
            if (selector == null)
            {
               consumer = sess.createConsumer(sourceDestination);
            }
            else
            {
               consumer = sess.createConsumer(sourceDestination, selector, false);
            }
         }
         else
         {
            //Durable subscription
            if (selector == null)
            {
               consumer = sess.createDurableSubscriber((Topic)sourceDestination, subName);
            }
            else
            {
               consumer = sess.createDurableSubscriber((Topic)sourceDestination, subName, selector, false);
            }
         }
        
         //Now the sending session
        
         if (!sourceAndTargetSameServer)
         {           
            if (usingXA)
            {
               if (trace) { log.trace("Creating XA dest session"); }
              
               //Create an XA sesion for sending to the destination
              
               targetSession = ((XAConnection)targetConn).createXASession();
              
               sess = ((XASession)targetSession).getSession();
            }
            else
            {
               if (trace) { log.trace("Creating non XA dest session"); }
              
               //Create a standard session for sending to the destination
              
               //If maxBatchSize == 1 we just create a non transacted session, otherwise we
               //create a transacted session for the send, since sending the batch in a transaction
               //is likely to be more efficient than sending messages individually
              
               boolean manualCommit = maxBatchSize == 1;
              
               targetSession = targetConn.createSession(manualCommit, manualCommit ? Session.SESSION_TRANSACTED : Session.AUTO_ACKNOWLEDGE);
              
               sess = targetSession;
            }      
         }
        
         if (usingXA)
         {
            if (trace) { log.trace("Starting JTA transaction"); }
           
            tx = startTx();
           
            enlistResources(tx);                 
         }
        
         producer = sess.createProducer(targetDestination);
                         
         consumer.setMessageListener(new SourceListener());
        
         return true;
      }
View Full Code Here

    *
    */
   public void testQueuePersistence() throws Exception
   {
      Connection conn = cf.createConnection();
      Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageProducer prod = sess.createProducer(queue);
      prod.setDeliveryMode(DeliveryMode.PERSISTENT);
     
      for (int i = 0; i < 10; i++)
      {
         TextMessage tm = sess.createTextMessage("message" + i);
         prod.send(tm);
      }
     
      conn.close();
     
      ServerManagement.stopServerPeer();
     
      ServerManagement.startServerPeer();
     
      // Messaging server restart implies new ConnectionFactory lookup
      cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");

      ServerManagement.deployQueue("Queue");
     
      conn = cf.createConnection();
      sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
      conn.start();
      MessageConsumer cons = sess.createConsumer(queue);
      for (int i = 0; i < 10; i++)
      {
         TextMessage tm = (TextMessage)cons.receive(3000);
         assertNotNull(tm);
         if (tm == null)
View Full Code Here

    * First test that message order survives a restart
    */
   public void testMessageOrderPersistence_1() throws Exception
   {
      Connection conn = cf.createConnection();
      Session sessSend = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageProducer prod = sessSend.createProducer(queue);
     
      TextMessage m0 = sessSend.createTextMessage("a");
      TextMessage m1 = sessSend.createTextMessage("b");
      TextMessage m2 = sessSend.createTextMessage("c");
      TextMessage m3 = sessSend.createTextMessage("d");
      TextMessage m4 = sessSend.createTextMessage("e");
      TextMessage m5 = sessSend.createTextMessage("f");
      TextMessage m6 = sessSend.createTextMessage("g");
      TextMessage m7 = sessSend.createTextMessage("h");
      TextMessage m8 = sessSend.createTextMessage("i");
      TextMessage m9 = sessSend.createTextMessage("j");
     
      prod.send(m0, DeliveryMode.PERSISTENT, 0, 0);
      prod.send(m1, DeliveryMode.PERSISTENT, 1, 0);
      prod.send(m2, DeliveryMode.PERSISTENT, 2, 0);
      prod.send(m3, DeliveryMode.PERSISTENT, 3, 0);
      prod.send(m4, DeliveryMode.PERSISTENT, 4, 0);
      prod.send(m5, DeliveryMode.PERSISTENT, 5, 0);
      prod.send(m6, DeliveryMode.PERSISTENT, 6, 0);
      prod.send(m7, DeliveryMode.PERSISTENT, 7, 0);
      prod.send(m8, DeliveryMode.PERSISTENT, 8, 0);
      prod.send(m9, DeliveryMode.PERSISTENT, 9, 0);
     
      conn.close();
     
      ServerManagement.stopServerPeer();

      ServerManagement.startServerPeer();

      // Messaging server restart implies new ConnectionFactory lookup
      cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");

      ServerManagement.deployQueue("Queue");
     
      conn = cf.createConnection();
      Session sessReceive = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
      conn.start();
      MessageConsumer cons = sessReceive.createConsumer(queue);
    
      {
         TextMessage t = (TextMessage)cons.receive(1000);
         assertNotNull(t);
         assertEquals("j", t.getText());
View Full Code Here

    * Second test that message order survives a restart
    */
   public void testMessageOrderPersistence_2() throws Exception
   {
      Connection conn = cf.createConnection();
      Session sessSend = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageProducer prod = sessSend.createProducer(queue);
     
      TextMessage m0 = sessSend.createTextMessage("a");
      TextMessage m1 = sessSend.createTextMessage("b");
      TextMessage m2 = sessSend.createTextMessage("c");
      TextMessage m3 = sessSend.createTextMessage("d");
      TextMessage m4 = sessSend.createTextMessage("e");
      TextMessage m5 = sessSend.createTextMessage("f");
      TextMessage m6 = sessSend.createTextMessage("g");
      TextMessage m7 = sessSend.createTextMessage("h");
      TextMessage m8 = sessSend.createTextMessage("i");
      TextMessage m9 = sessSend.createTextMessage("j");

     
      prod.send(m0, DeliveryMode.PERSISTENT, 0, 0);
      prod.send(m1, DeliveryMode.PERSISTENT, 0, 0);
      prod.send(m2, DeliveryMode.PERSISTENT, 0, 0);
      prod.send(m3, DeliveryMode.PERSISTENT, 3, 0);
      prod.send(m4, DeliveryMode.PERSISTENT, 3, 0);
      prod.send(m5, DeliveryMode.PERSISTENT, 4, 0);
      prod.send(m6, DeliveryMode.PERSISTENT, 4, 0);
      prod.send(m7, DeliveryMode.PERSISTENT, 5, 0);
      prod.send(m8, DeliveryMode.PERSISTENT, 5, 0);
      prod.send(m9, DeliveryMode.PERSISTENT, 6, 0);
     
      conn.close();
     
      ServerManagement.stopServerPeer();

      ServerManagement.startServerPeer();

      // Messaging server restart implies new ConnectionFactory lookup
      cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");

      ServerManagement.deployQueue("Queue");
     
      conn = cf.createConnection();
      Session sessReceive = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
      conn.start();
      MessageConsumer cons = sessReceive.createConsumer(queue);
    
      {
         TextMessage t = (TextMessage)cons.receive(1000);
         assertNotNull(t);
         assertEquals("j", t.getText());
View Full Code Here

      Topic thisTopic = (Topic)initialContext.lookup("/topic/YetAnotherTopic");

      Connection conn = cf.createConnection();
      conn.setClientID("five");

      Session s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

      MessageConsumer ds = s.createDurableSubscriber(thisTopic, "sub", null, false);

      MessageProducer p = s.createProducer(thisTopic);
      p.setDeliveryMode(DeliveryMode.PERSISTENT);
      TextMessage tm = s.createTextMessage("thebody");
      p.send(tm);
      log.debug("message sent");

      conn.close();

      log.debug("stopping the server");
      ServerManagement.stopServerPeer();

      try
      {
         initialContext.lookup("/topic/YetAnotherTopic");
         fail("this should throw exception");
      }
      catch(NameNotFoundException e)
      {
         // OK
      }

      log.debug("starting the server");
      ServerManagement.startServerPeer();
      log.debug("server started");

      // Messaging server restart implies new ConnectionFactory lookup
      cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");

      ServerManagement.deployTopic("YetAnotherTopic");
      log.debug("topic deployed");

      thisTopic = (Topic)initialContext.lookup("/topic/YetAnotherTopic");

      conn = cf.createConnection();
      conn.setClientID("five");

      s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
      conn.start();

      ds = s.createDurableSubscriber(thisTopic, "sub", null, false);

      TextMessage rm = (TextMessage)ds.receive(3000);
      assertNotNull(rm);
      assertEquals("thebody", rm.getText());
View Full Code Here

   public void testDurableSubscriptionPersistence_2() throws Exception
   {
      Connection conn = cf.createConnection();
      conn.setClientID("Sausages");
     
      Session sessConsume = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
     
      MessageConsumer sub1 = sessConsume.createDurableSubscriber(topic, "sub1", null, false);
      MessageConsumer sub2 = sessConsume.createDurableSubscriber(topic, "sub2", null, false);
      MessageConsumer sub3 = sessConsume.createDurableSubscriber(topic, "sub3", null, false);
     
     
      Session sessSend = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageProducer prod = sessSend.createProducer(topic);
      prod.setDeliveryMode(DeliveryMode.PERSISTENT);
     
      for (int i = 0; i < 10; i++)
      {
         TextMessage tm = sessSend.createTextMessage("message" + i);
         prod.send(tm);
      }
     
      conn.close();
     
View Full Code Here

         // read the message from the queue

         Connection conn = cf.createConnection();
         conn.start();
         Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
         MessageConsumer cons = sess.createConsumer(queue);
         TextMessage tm = (TextMessage)cons.receive(5000);

         assertNotNull(tm);
         assertEquals(MESSAGE_TEXT, tm.getText());
View Full Code Here

   {
      Connection pconn = cf.createConnection();

      try
      {
         Session ps = pconn.createSession(false, Session.AUTO_ACKNOWLEDGE);

         try
         {
            ps.createBrowser(new JBossQueue("NoSuchQueue"));
            fail("should throw exception");
         }
         catch(InvalidDestinationException e)
         {
            // OK
View Full Code Here

    */
   public void testQueueMultipleSenders() throws Exception
   {
      Connection conn1 = cf.createConnection();
     
      Session sess1 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Session sess2 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Session sess3 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Session sess4 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Session sess5 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Session sess6 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Session sess7 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Session sess8 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
     
      Session sess9 = conn1.createSession(true, Session.SESSION_TRANSACTED);
      Session sess10 = conn1.createSession(true, Session.SESSION_TRANSACTED);
      Session sess11 = conn1.createSession(true, Session.SESSION_TRANSACTED);
      Session sess12 = conn1.createSession(true, Session.SESSION_TRANSACTED);
      Session sess13 = conn1.createSession(true, Session.SESSION_TRANSACTED);
      Session sess14 = conn1.createSession(true, Session.SESSION_TRANSACTED);
      Session sess15 = conn1.createSession(true, Session.SESSION_TRANSACTED);
      Session sess16 = conn1.createSession(true, Session.SESSION_TRANSACTED);
     
      XASession xaSess1 = ((XAConnection)conn1).createXASession();
      XASession xaSess2 = ((XAConnection)conn1).createXASession();
      XASession xaSess3 = ((XAConnection)conn1).createXASession();
      XASession xaSess4 = ((XAConnection)conn1).createXASession();
      XASession xaSess5 = ((XAConnection)conn1).createXASession();
      XASession xaSess6 = ((XAConnection)conn1).createXASession();
      XASession xaSess7 = ((XAConnection)conn1).createXASession();
      XASession xaSess8 = ((XAConnection)conn1).createXASession();
     
      Session sess17 = xaSess1.getSession();
      Session sess18 = xaSess2.getSession();
      Session sess19 = xaSess3.getSession();
      Session sess20 = xaSess4.getSession();
      Session sess21 = xaSess5.getSession();
      Session sess22 = xaSess6.getSession();
      Session sess23 = xaSess7.getSession();
      Session sess24 = xaSess8.getSession();
     
      MessageProducer prod1 = sess1.createProducer(queue1);
      prod1.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod2 = sess2.createProducer(queue1);
      prod2.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod3 = sess3.createProducer(queue1);
      prod3.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod4 = sess4.createProducer(queue1);
      prod4.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod5 = sess5.createProducer(queue1);
      prod5.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod6 = sess6.createProducer(queue1);
      prod6.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod7 = sess7.createProducer(queue1);
      prod7.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod8 = sess8.createProducer(queue1);
      prod8.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod9 = sess9.createProducer(queue1);
      prod9.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod10 = sess10.createProducer(queue1);
      prod10.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod11 = sess11.createProducer(queue1);
      prod11.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod12 = sess12.createProducer(queue1);
      prod12.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod13 = sess13.createProducer(queue1);
      prod13.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod14 = sess14.createProducer(queue1);
      prod14.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod15 = sess15.createProducer(queue1);
      prod15.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod16 = sess16.createProducer(queue1);
      prod16.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod17 = sess17.createProducer(queue1);
      prod17.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod18 = sess18.createProducer(queue1);
      prod18.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod19 = sess19.createProducer(queue1);
      prod19.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod20 = sess20.createProducer(queue1);
      prod20.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod21 = sess21.createProducer(queue1);
      prod21.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod22 = sess22.createProducer(queue1);
      prod22.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod23 = sess23.createProducer(queue1);
      prod23.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      MessageProducer prod24 = sess24.createProducer(queue1);
      prod24.setDeliveryMode(DeliveryMode.NON_PERSISTENT);


      Connection conn2 = cf.createConnection();
      conn2.start();
      Session sessReceive = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);     
      MessageConsumer cons = sessReceive.createConsumer(queue1);
     
 
      Runner[] runners = new Runner[] {
      new Sender("prod1", sess1, prod1, NUM_NON_PERSISTENT_MESSAGES),
      new Sender("prod2", sess2, prod2, NUM_PERSISTENT_MESSAGES),
View Full Code Here

TOP

Related Classes of javax.jms.Session

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.