Package javax.jms

Examples of javax.jms.Connection


    }


    public void testRedel5() throws Exception
    {
       Connection conn = null;

       try
       {
          conn = cf.createConnection();
          conn.start();

          Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
          MessageProducer prod = sess.createProducer(queue);
          TextMessage tm1 = sess.createTextMessage("hello1");
          TextMessage tm2 = sess.createTextMessage("hello2");
          TextMessage tm3 = sess.createTextMessage("hello3");
          prod.send(tm1);
          prod.send(tm2);
          prod.send(tm3);

          MessageConsumer cons1 = sess.createConsumer(queue);

          TextMessage rm1 = (TextMessage)cons1.receive(1500);
          assertNotNull(rm1);
          assertEquals("hello1", rm1.getText());

          //redeliver
          sess.recover();

          TextMessage rm2 = (TextMessage)cons1.receive(1500);
          assertNotNull(rm2);
          assertEquals("hello1", rm2.getText());

          TextMessage rm3 = (TextMessage)cons1.receive(1500);
          assertNotNull(rm3);
          assertEquals("hello2", rm3.getText());

          TextMessage rm4 = (TextMessage)cons1.receive(1500);
          assertNotNull(rm4);
          assertEquals("hello3", rm4.getText());


          //This last step is important - there shouldn't be any more messages to receive
          TextMessage rm5 = (TextMessage)cons1.receive(1500);
          assertNull(rm5);
       }
       finally
       {
          if (conn != null)
          {
             conn.close();
          }
       }
    }
View Full Code Here


       }
    }

    public void testRedel6() throws Exception
    {
       Connection conn = null;

       try
       {
          conn = cf.createConnection();
          conn.start();

          Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
          MessageProducer prod = sess.createProducer(queue);
          TextMessage tm1 = sess.createTextMessage("hello1");
          TextMessage tm2 = sess.createTextMessage("hello2");
          TextMessage tm3 = sess.createTextMessage("hello3");
          prod.send(tm1);
          prod.send(tm2);
          prod.send(tm3);

          MessageConsumer cons1 = sess.createConsumer(queue);

          TextMessage rm1 = (TextMessage)cons1.receive(1500);
          assertNotNull(rm1);
          assertEquals("hello1", rm1.getText());

          cons1.close();
 
          //Give time for asynch cancel to happen
          Thread.sleep(500);

          log.debug("sess.recover()");

          //redeliver
          sess.recover();

          MessageConsumer cons2 = sess.createConsumer(queue);

          log.debug("receiving ...");

          TextMessage rm2 = (TextMessage)cons2.receive(1500);
          assertNotNull(rm2);
          assertEquals("hello1", rm2.getText());

          TextMessage rm3 = (TextMessage)cons2.receive(1500);
          assertNotNull(rm3);
          assertEquals("hello2", rm3.getText());

          TextMessage rm4 = (TextMessage)cons2.receive(1500);
          assertNotNull(rm4);
          assertEquals("hello3", rm4.getText());


          //This last step is important - there shouldn't be any more messages to receive
          TextMessage rm5 = (TextMessage)cons2.receive(1500);
          assertNull(rm5);


       }
       finally
       {
          if (conn != null)
          {
             conn.close();
          }
       }

    }
View Full Code Here

   /**
    * http://www.jboss.org/index.html?module=bb&op=viewtopic&t=71350
    */
   public void testRedel7() throws Exception
   {
      Connection conn = null;

       try
       {
          conn = cf.createConnection();
          conn.start();

          Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);

          MessageProducer prod = sess.createProducer(queue);
         
          TextMessage tm1 = sess.createTextMessage("1");
         
          TextMessage tm2 = sess.createTextMessage("2");
         
          TextMessage tm3 = sess.createTextMessage("3");
         
          prod.send(tm1);
          prod.send(tm2);
          prod.send(tm3);

          log.trace("Creating consumer");
          MessageConsumer cons1 = sess.createConsumer(queue);

          log.trace("Waiting for message");
         
          TextMessage r1 = (TextMessage)cons1.receive();
         
          assertEquals(tm1.getText(), r1.getText());

          log.trace("Got first message");

          cons1.close();

          log.trace("Closed consumer");

          MessageConsumer cons2 = sess.createConsumer(queue);

          log.trace("Waiting for second message");
          TextMessage r2 = (TextMessage)cons2.receive();
         
          assertEquals(tm2.getText(), r2.getText());

          log.trace("got second message");

          TextMessage r3 = (TextMessage)cons2.receive();
         
          assertEquals(tm3.getText(), r3.getText());

          r1.acknowledge();
          r2.acknowledge();
          r3.acknowledge();
       }
       finally
       {
          if (conn != null)
          {
             conn.close();
          }
       }
   }
View Full Code Here

   /**
    * http://www.jboss.org/index.html?module=bb&op=viewtopic&t=71350
    */
   public void testRedel8() throws Exception
   {
      Connection conn = null;

       try
       {
          conn = cf.createConnection();

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

          MessageProducer prod = sess.createProducer(queue);

          //Send 3 messages

          prod.send(sess.createTextMessage("1"));
          prod.send(sess.createTextMessage("2"));
          prod.send(sess.createTextMessage("3"));
         
          log.trace("Sent messages");

          conn.start();

          MessageConsumer cons1 = sess.createConsumer(queue);

          log.trace("closing cons1");

          cons1.close();

          log.trace("cons1 closed");

          MessageConsumer cons2 = sess.createConsumer(queue);

          log.trace("cons2 created");

          Message r1 = cons2.receive();
          Message r2 = cons2.receive();
          Message r3 = cons2.receive();

          //Messages should be received?
          assertNotNull(r1);
          assertNotNull(r2);
          assertNotNull(r3);
       }
       finally
       {
          if (conn != null)
          {
             conn.close();
          }
       }
   }
View Full Code Here

//   }


   public void testSendAndReceivePersistentDifferentConnections() throws Exception
   {
      Connection connSend = null;
      Connection connReceive = null;

      try
      {

         log.trace("Running testSendAndReceivePersistentDifferentConnections");

         connSend = cf.createConnection();

         connSend.start();

         Session sessSend = connSend.createSession(true, Session.AUTO_ACKNOWLEDGE);

         MessageProducer prod = sessSend.createProducer(null);

         prod.setDeliveryMode(DeliveryMode.PERSISTENT);

         Message m = sessSend.createTextMessage("hello");

         prod.send(queue2, m);

         sessSend.commit();

         connReceive = cf.createConnection();

         connReceive.start();

         Session sessReceive = connReceive.createSession(true, Session.SESSION_TRANSACTED);

         MessageConsumer cons = sessReceive.createConsumer(queue2);

         TextMessage m2 = (TextMessage)cons.receive(1500);

         assertNotNull(m2);

         assertEquals("hello", m2.getText());

         sessReceive.commit();

         cons.close();


         connReceive = cf.createConnection();

         connReceive.start();

         sessReceive = connReceive.createSession(true, Session.SESSION_TRANSACTED);

         cons = sessReceive.createConsumer(queue2);

         TextMessage m3 = (TextMessage)cons.receive(1500);

         assertNull(m3);


         log.trace("Done test");

      }
      finally
      {
         if (connSend != null) connSend.close();
         if (connReceive != null) connReceive.close();
      }
   }
View Full Code Here

   public void testNoLocal() throws Exception
   {
      if (log.isTraceEnabled()) log.trace("testNoLocal");

      Connection conn1 = null;
      Connection conn2 = null;

      try
      {

         conn1 = cf.createConnection();
         Session sess1 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);

         MessageProducer producer1 = sess1.createProducer(topic);
         producer1.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
         MessageConsumer consumer1 = sess1.createConsumer(topic, null, true);

         conn2 = cf.createConnection();

         Session sess2 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
         assertEquals(Session.AUTO_ACKNOWLEDGE, sess2.getAcknowledgeMode());
         MessageConsumer consumer2 = sess2.createConsumer(topic, null, true);

         // we need different session because we cannot access the same session from different
         // threads
         Session sess3 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
         assertEquals(Session.AUTO_ACKNOWLEDGE, sess3.getAcknowledgeMode());
         MessageConsumer consumer3 = sess3.createConsumer(topic, null, true);

         //Consumer 1 should not get the message but consumers 2 and 3 should

         conn1.start();
         conn2.start();

         class TestRunnable implements Runnable
         {
            boolean exceptionThrown;
            public Message m;
            MessageConsumer consumer;
            TestRunnable(MessageConsumer consumer)
            {
               this.consumer = consumer;
            }

            public void run()
            {
               try
               {
                  m = consumer.receive(1500);
               }
               catch (Exception e)
               {
                  exceptionThrown = true;
               }
            }
         }

         TestRunnable tr1 = new TestRunnable(consumer1);
         TestRunnable tr2 = new TestRunnable(consumer2);
         TestRunnable tr3 = new TestRunnable(consumer3);

         Thread t1 = new Thread(tr1);
         Thread t2 = new Thread(tr2);
         Thread t3 = new Thread(tr3);

         t1.start();
         t2.start();
         t3.start();

         Message m2 = sess1.createTextMessage("Hello");
         producer1.send(m2);

         t1.join();
         t2.join();
         t3.join();

         assertTrue(!tr1.exceptionThrown);
         assertTrue(!tr2.exceptionThrown);
         assertTrue(!tr3.exceptionThrown);

         assertNull(tr1.m);

         assertNotNull(tr2.m);
         assertNotNull(tr3.m);

      }
      finally
      {
         if (conn1 != null)
         {
            conn1.close();
         }
         if (conn2 != null)
         {
            conn2.close();
         }
      }

   }
View Full Code Here

    *
    * Also see JMS 1.1 Spec. 6.12
    */
   public void testTopicRedelivery() throws Exception
   {
      Connection conn1 = null;

      try
      {

         conn1 = cf.createConnection();
         conn1.start();

         //Create 2 non durable subscribers on topic

         Session sess1 = conn1.createSession(false, Session.CLIENT_ACKNOWLEDGE);
         Session sess2 = conn1.createSession(false, Session.CLIENT_ACKNOWLEDGE);


         log.trace("careting consumer1");
         MessageConsumer cons1 = sess1.createConsumer(topic);

         log.trace("creating consumer2");
         MessageConsumer cons2 = sess2.createConsumer(topic);

         log.trace("starting connection");

         conn1.start();

         log.trace("started connection");

         Session sess3 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
         MessageProducer prod = sess3.createProducer(topic);
         prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

         TextMessage tm = sess3.createTextMessage("nurse!");
         prod.send(tm);

         TextMessage tm1 = (TextMessage)cons1.receive(1500);
         TextMessage tm2 = (TextMessage)cons2.receive(1500);

         assertNotNull(tm1);
         assertNotNull(tm2);
         assertEquals("nurse!", tm1.getText());
         assertEquals("nurse!", tm2.getText());

         //acknowledge tm1
         tm1.acknowledge();

         //tm2 has not been acknowledged
         //so should be redelivered on session.recover

         sess2.recover();

         tm2 = (TextMessage)cons2.receive(1500);
         assertNotNull(tm2);
         assertEquals("nurse!", tm2.getText());


         //but tm1 should not be redelivered
         tm1 = (TextMessage)cons1.receive(1500);
         assertNull(tm1);
      }
      finally
      {
         if (conn1 != null)
         {
            log.trace("closing connection");
            conn1.close();
         }
      }

   }
View Full Code Here

      See JMS spec. sec. 6.12

   */
   public void testNoRedeliveryOnNonDurableSubscriber() throws Exception
   {
      Connection conn1 = null;
      Connection conn2 = null;

      try
      {

         conn1 = cf.createConnection();
         conn1.start();

         Session sess1 = conn1.createSession(false, Session.CLIENT_ACKNOWLEDGE);
         MessageProducer prod = sess1.createProducer(topic);
         prod.setDeliveryMode(DeliveryMode.PERSISTENT);

         final int NUM_MESSAGES = 1;

         MessageConsumer cons = sess1.createConsumer(topic);

         for (int i = 0; i < NUM_MESSAGES; i++)
         {
            TextMessage tm = sess1.createTextMessage("helloxyz");
            prod.send(topic, tm);
         }

         //receive but don't ack

         int count = 0;
         while (true)
         {
            TextMessage tm = (TextMessage)cons.receive(1000);
            if (tm == null) break;
            assertEquals(tm.getText(), "helloxyz");
            count++;
         }
         assertEquals(NUM_MESSAGES, count);

         conn1.close();

         conn2 = cf.createConnection();
         conn2.start();

         Session sess2 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);

         MessageConsumer cons2 = sess2.createConsumer(topic);
         Message m = cons2.receive(1500);
         assertNull(m);

         conn2.close();
      }
      finally
      {
         if (conn1 != null)
         {
            conn1.close();
         }
         if (conn2 != null)
         {
            conn2.close();
         }
      }


   }
View Full Code Here

  
   // Public --------------------------------------------------------
  
   public void testSendReceiveMessage() throws Exception
   {
      Connection conn = cf.createConnection();
     
      Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
     
      MessageProducer prod = sess.createProducer(queue);
     
      //Make persistent to make sure message gets serialized
      prod.setDeliveryMode(DeliveryMode.PERSISTENT);
     
      MessageConsumer cons = sess.createConsumer(queue);
     
      TestMessage tm = new TestMessage(123, false);
     
      ObjectMessage om = sess.createObjectMessage();
     
      om.setObject(tm);
     
      conn.start();
     
      prod.send(om);
     
      ObjectMessage om2 = (ObjectMessage)cons.receive(1000);
     
      assertNotNull(om2);
     
      TestMessage tm2 = (TestMessage)om2.getObject();
     
      assertEquals(123, tm2.getId());
     
      conn.close();
           
   }
View Full Code Here

   }


   public void testProducersAndConsumers() throws Exception
   {
      Connection connectionProducer = cf.createConnection();
      Connection connectionReader = cf.createConnection();

      connectionReader.start();
      connectionProducer.start(); // try with and without this...

      ProducerThread producerThread[] = new ProducerThread[20];
      ReaderThread readerThread[] = new ReaderThread[20];
      TestThread threads[] = new TestThread[40];
View Full Code Here

TOP

Related Classes of javax.jms.Connection

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.