Package javax.jms

Examples of javax.jms.Connection


   }
  
  
   public void testRelayMessage() throws Exception
   {
      Connection conn = cf.createConnection();

      conn.start();

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

      MessageConsumer cons = sess.createConsumer(queue);

      final Object lock = new Object();

      final int numMessages = 100;


      class MyListener implements MessageListener
      {
         boolean failed;

         int count;

         public void onMessage(Message m)
         {
            try
            {
               MessageProducer prod = sess.createProducer(queue2);

               prod.send(m);

               count++;

               if (count == numMessages)
               {
                  synchronized (lock)
                  {
                     lock.notify();
                  }

               }
            }
            catch (JMSException e)
            {
               failed = true;
            }
         }
      }

      MyListener listener = new MyListener();

      cons.setMessageListener(listener);

      MessageProducer prod = sess.createProducer(queue);

      for (int i = 0; i < numMessages; i++)
      {
         prod.send(sess.createMessage());
      }

      synchronized (lock)
      {
         lock.wait();
      }

      conn.close();

      assertFalse(listener.failed);
   }
View Full Code Here


    * Eventually the first client will have it's resources cleared up so the delivery gets
    * cancelled but we need to prompt deliver() so the reconnected client gets it
    */
   public void testRedeliveryToCompetingConsumerOnQueue() throws Exception
   {
      Connection conn = null;
     
      try
      {
         conn = cf.createConnection();
     
         Session sessSend = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
  
         MessageProducer prod = sessSend.createProducer(queue);
  
         conn.start();
  
         Session sessConsume1 = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
  
         MessageConsumer cons1 = sessConsume1.createConsumer(queue);
  
         TextMessage tm = sessSend.createTextMessage();
  
         tm.setText("Your mum");
  
         prod.send(tm);
  
         TextMessage tm2 = (TextMessage)cons1.receive();
  
         assertNotNull(tm2);
  
         assertEquals("Your mum", tm2.getText());
  
         // Don't ack
  
         // Create another consumer
  
         Session sessConsume2 = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
  
         MessageConsumer cons2 = sessConsume2.createConsumer(queue);
  
         // this should cancel message and cause delivery to other consumer
  
         log.trace("Closed session 1");
         sessConsume1.close();
  
         TextMessage tm3 = (TextMessage)cons2.receive(1000);
  
         assertNotNull(tm3);
  
         assertEquals("Your mum", tm3.getText());
  
         tm3.acknowledge();
      }
      finally
      {

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

   }
View Full Code Here

   }


   public void testRedeliveryToCompetingConsumerOnSubscription() throws Exception
   {
      Connection conn = cf.createConnection();

      conn.setClientID("wibble");

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

      MessageProducer prod = sessSend.createProducer(topic);

      conn.start();

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

      MessageConsumer cons1 = sessConsume1.createDurableSubscriber(topic, "sub1");

      TextMessage tm = sessSend.createTextMessage();

      tm.setText("Your mum");

      prod.send(tm);

      TextMessage tm2 = (TextMessage)cons1.receive();

      assertNotNull(tm2);

      assertEquals("Your mum", tm2.getText());

      //Don't ack

      //Create another consumer

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

      MessageConsumer cons2 = sessConsume2.createDurableSubscriber(topic, "sub1");

      //this should cancel message and cause delivery to other consumer

      sessConsume1.close();

      TextMessage tm3 = (TextMessage)cons2.receive(1000);

      assertNotNull(tm3);

      assertEquals("Your mum", tm3.getText());

      tm3.acknowledge();

      conn.close();

   }
View Full Code Here

   // Invalid destination test
   //

   public void testCreateConsumerOnInexistentDestination() throws Exception
   {
      Connection pconn = cf.createConnection();

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

         try
         {
            ps.createConsumer(new JBossTopic("NoSuchTopic"));
            fail("should throw exception");
         }
         catch(InvalidDestinationException e)
         {
            // OK
         }
      }
      finally
      {
         pconn.close();
      }
   }
View Full Code Here

   /* Test that an ack can be sent after the consumer that received the message has been closed.
    * Acks are scoped per session.
    */
   public void testAckAfterConsumerClosed() throws Exception
   {
      Connection connSend = null;
      Connection connReceive = null;

      try
      {

         connSend = cf.createConnection();

         connSend.start();

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

         MessageProducer prod = sessSend.createProducer(queue2);

         prod.setDeliveryMode(DeliveryMode.PERSISTENT);

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

         prod.send(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());

         //It is legal to close the consumer before committing the tx which is when
         //the acks are sent
         cons.close();

         sessReceive.commit();

         connReceive.close();

         log.trace("Done test");

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

      consumerConnection.close();
   }

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

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

          Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
          MessageProducer prod = sess.createProducer(queue);
          TextMessage tm1 = sess.createTextMessage("a");
          TextMessage tm2 = sess.createTextMessage("b");
          TextMessage tm3 = sess.createTextMessage("c");
          prod.send(tm1);
          prod.send(tm2);
          prod.send(tm3);
          sess.commit();

          MessageConsumer cons1 = sess.createConsumer(queue);

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

          cons1.close();

          MessageConsumer cons2 = sess.createConsumer(queue);

          sess.commit();

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

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

          TextMessage rm4 = (TextMessage)cons2.receive(1500);
          assertNull(rm4);


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

    }
View Full Code Here

    }


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

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

          Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
          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);
          sess.commit();

          MessageConsumer cons1 = sess.createConsumer(queue);

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

          cons1.close();

          MessageConsumer cons2 = sess.createConsumer(queue);

          sess.commit();

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

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

          TextMessage rm4 = (TextMessage)cons2.receive(1500);
          assertNull(rm4);


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

    }
View Full Code Here

    }

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

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

          Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
          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);
          sess.commit();

          MessageConsumer cons1 = sess.createConsumer(queue);

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

          cons1.close();

          sess.commit();

          MessageConsumer cons2 = sess.createConsumer(queue);

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

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

          TextMessage rm4 = (TextMessage)cons2.receive(1500);
          assertNull(rm4);


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

    }
View Full Code Here

    }

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

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

          Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
          MessageProducer prod = sess.createProducer(queue);
          TextMessage tm1 = sess.createTextMessage("hello1");
          log.trace(tm1.getJMSMessageID());
          TextMessage tm2 = sess.createTextMessage("hello2");
          TextMessage tm3 = sess.createTextMessage("hello3");
          prod.send(tm1);
          prod.send(tm2);
          prod.send(tm3);
          sess.commit();

          MessageConsumer cons1 = sess.createConsumer(queue);

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

          log.trace("rolling back");
          //rollback should cause redelivery of messages not acked
          sess.rollback();
          log.trace("rolled back");

          TextMessage rm2 = (TextMessage)cons1.receive(1500);
          assertEquals("hello1", rm2.getText());
          log.trace(rm1.getJMSMessageID());

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

          TextMessage rm4 = (TextMessage)cons1.receive(1500);
          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 testRedel4() throws Exception
    {
       Connection conn = null;

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

          Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
          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);
          sess.commit();

          MessageConsumer cons1 = sess.createConsumer(queue);

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

          cons1.close();

          //Cancelling is asynch so can take some time
          Thread.sleep(500);
         
          //rollback should cause redelivery of messages

          //in this case redelivery occurs to a different receiver

          sess.rollback();

          MessageConsumer cons2 = sess.createConsumer(queue);

          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

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.