Examples of TopicSubscriber


Examples of javax.jms.TopicSubscriber

      topicConnection2.start();

      // We don't want local messages on this topic.
      TopicSession session1 = topicConnection1.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
      Topic topic = (Topic) context.lookup(TEST_TOPIC);
      TopicSubscriber subscriber1 = session1.createSubscriber(topic, null, true);
      TopicPublisher sender1 = session1.createPublisher(topic);

      //Now a sender
      TopicSession session2 = topicConnection2.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
      TopicPublisher sender2 = session2.createPublisher(topic);

      drainMessagesForTopic(subscriber1);

      //send some messages
      sender1.publish(session1.createTextMessage("Local Message"));
      sender2.publish(session2.createTextMessage("Remote Message"));

      // Get the messages, we should get the remote message
      // but not the local message
      TextMessage msg1 = (TextMessage) subscriber1.receive(2000);
      if (msg1 == null)
      {
         fail("Did not get any messages");
      }
      else
      {
         getLog().debug("Got message: " + msg1);
         if (msg1.getText().equals("Local Message"))
         {
            fail("Got a local message");
         }
         TextMessage msg2 = (TextMessage) subscriber1.receive(2000);
         if (msg2 != null)
         {
            getLog().debug("Got message: " + msg2);
            fail("Got an extra message.  msg1:" + msg1 + ", msg2:" + msg2);
         }
View Full Code Here

Examples of javax.jms.TopicSubscriber

      topicConnection2.start();

      // Session 1
      TopicSession session1 = topicConnection1.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
      Topic topic = (Topic) context.lookup(TEST_TOPIC);
      TopicSubscriber subscriber1 = session1.createSubscriber(topic, null, true);
      TopicPublisher sender1 = session1.createPublisher(topic);

      // Session 2
      TopicSession session2 = topicConnection2.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
      TopicSubscriber subscriber2 = session2.createSubscriber(topic, null, true);
      TopicPublisher sender2 = session2.createPublisher(topic);

      drainMessagesForTopic(subscriber1);
      drainMessagesForTopic(subscriber2);

      //send the message
      sender1.publish(session1.createTextMessage("Message"));

      assertTrue("Subscriber1 should not get a message", subscriber1.receiveNoWait() == null);
      TextMessage msg = (TextMessage) subscriber2.receive(2000);
      assertTrue("Subscriber2 should get a message, got " + msg, msg != null && msg.getText().equals("Message"));

      //send it back
      sender2.publish(msg);

      msg = (TextMessage) subscriber1.receive(2000);
      assertTrue("Subscriber1 should get a message, got " + msg, msg != null && msg.getText().equals("Message"));
      assertTrue("Subscriber2 should not get a message", subscriber2.receiveNoWait() == null);

      topicConnection1.stop();
      topicConnection1.close();
      topicConnection2.stop();
      topicConnection2.close();
View Full Code Here

Examples of javax.jms.TopicSubscriber

         TopicSession sendSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
         TopicPublisher sender = sendSession.createPublisher(topic);

         getLog().debug("Clearing the topic");
         TopicSession subSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
         TopicSubscriber subscriber = subSession.createDurableSubscriber(topic, "test");
         Message message = subscriber.receive(50);
         while (message != null)
            message = subscriber.receive(50);
         subSession.close();

         getLog().debug("Subscribing to topic, looking for Value = 'A'");
         subSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
         subscriber = subSession.createDurableSubscriber(topic, "test", "Value = 'A'", false);

         getLog().debug("Send some messages");
         message = sendSession.createTextMessage("Message1");
         message.setStringProperty("Value", "A");
         sender.publish(message);
         message = sendSession.createTextMessage("Message2");
         message.setStringProperty("Value", "A");
         sender.publish(message);
         message = sendSession.createTextMessage("Message3");
         message.setStringProperty("Value", "B");
         sender.publish(message);

         getLog().debug("Retrieving the A messages");
         message = subscriber.receive(2000);
         assertTrue("Expected message 1", message != null);
         assertTrue("Should get an A", message.getStringProperty("Value").equals("A"));
         message = subscriber.receive(2000);
         assertTrue("Expected message 2", message != null);
         assertTrue("Should get a second A", message.getStringProperty("Value").equals("A"));
         assertTrue("That should be it for A", subscriber.receive(2000) == null);

         getLog().debug("Closing the subscriber without acknowledgement");
         subSession.close();

         getLog().debug("Subscribing to topic, looking for Value = 'B'");
         subSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
         subscriber = subSession.createDurableSubscriber(topic, "test", "Value = 'B'", false);

         getLog().debug("Retrieving the non-existent B messages");
         assertTrue("B should not be there", subscriber.receive(2000) == null);

         getLog().debug("Closing the subscriber.");
         subSession.close();

         getLog().debug("Subscribing to topic, looking for those Value = 'A'");
         subSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
         subscriber = subSession.createDurableSubscriber(topic, "test", "Value = 'A'", false);
         assertTrue("Should not be any A the subscription was changed", subscriber.receive(2000) == null);
         subSession.close();

         getLog().debug("Subscribing to topic, looking for everything");
         subSession = topicConnection.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE);
         subscriber = subSession.createDurableSubscriber(topic, "test", null, false);

         message = sendSession.createTextMessage("Message4");
         message.setStringProperty("Value", "A");
         sender.publish(message);

         message = subscriber.receive(2000);
         assertTrue("Expected message 4", message != null);
         assertTrue("Should be an A which we don't acknowledge", message.getStringProperty("Value").equals("A"));
         subSession.close();

         getLog().debug("Subscribing to topic, looking for the Value = 'A'");
         subSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
         subscriber = subSession.createDurableSubscriber(topic, "test", "Value = 'A'", false);
         assertTrue(
            "Should not be any A, the subscription was changed. Even though the old and new selectors match the message",
            subscriber.receive(2000) == null);
         subSession.close();

         getLog().debug("Closing the send session");
         sendSession.close();
View Full Code Here

Examples of javax.jms.TopicSubscriber

         TopicSession sendSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
         TopicPublisher sender = sendSession.createPublisher(topic);

         getLog().debug("Clearing the topic");
         TopicSession subSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
         TopicSubscriber subscriber = subSession.createDurableSubscriber(topic, "test");
         TextMessage message = (TextMessage) subscriber.receive(50);
         while (message != null)
            message = (TextMessage) subscriber.receive(50);
         subSession.close();

         getLog().debug("Subscribing to topic, with null selector");
         subSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
         subscriber = subSession.createDurableSubscriber(topic, "test", null, false);

         getLog().debug("Send a message");
         message = sendSession.createTextMessage("Message1");
         sender.publish(message);

         getLog().debug("Retrieving the message");
         message = (TextMessage) subscriber.receive(2000);
         assertTrue("Expected message 1", message != null);
         assertTrue("Should get Message1", message.getText().equals("Message1"));
         getLog().debug("Closing the subscriber");
         subSession.close();

         getLog().debug("Subscribing to topic, with an empty selector");
         subSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
         subscriber = subSession.createDurableSubscriber(topic, "test", "   ", false);

         getLog().debug("Send a message");
         message = sendSession.createTextMessage("Message2");
         sender.publish(message);

         getLog().debug("Retrieving the message");
         message = (TextMessage) subscriber.receive(2000);
         assertTrue("Expected message 2", message != null);
         assertTrue("Should get Message2", message.getText().equals("Message2"));
         getLog().debug("Closing the subscriber");

         getLog().debug("Removing the subscription");
View Full Code Here

Examples of javax.jms.TopicSubscriber

            String type = arg0.getParameter("type");
            PrintWriter out = arg1.getWriter();

            TopicConnection connection = tcf.createTopicConnection();
            TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
            TopicSubscriber topicSubscriber = session.createSubscriber(topic);
            TestListener test = new TestListener();
            topicSubscriber.setMessageListener(test);
            connection.start();
            TopicPublisher topicPublisher = session.createPublisher(topic);
            TextMessage tmsg = session.createTextMessage("JMS - Test Topic Message");
            topicPublisher.publish(tmsg);
            if ( msg != null ) {
                out.println("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>");
                out.println("<head><title>JMS Topic Sender Receiver</title></head>");
                out.println("<body>Received JMS Topic Message</body></html>");
            }
            else {
                out.println("<body>Did Not Receive JMS Topic Message</body></html>");
            }

            topicSubscriber.close();
            session.close();
            connection.stop();

        }
        catch ( Exception e ) {
View Full Code Here

Examples of javax.jms.TopicSubscriber

            publishMessages(pubSession, publisher, topic, DeliveryMode.PERSISTENT, 1*1024, 1, "true");
            pubSession.commit();
            assertEquals("DurableSubscription backing queue should now have 2 messages on it",
                         Integer.valueOf(2), dursubQueue.getMessageCount());

            TopicSubscriber durSub = pubSession.createDurableSubscriber(topic, SELECTOR_SUB_NAME,"testprop='true'", false);
            Message m = durSub.receive(2000);
            assertNotNull("Failed to receive an expected message", m);
            m = durSub.receive(2000);
            assertNotNull("Failed to receive an expected message", m);
            pubSession.commit();

            pubSession.close();
        }
View Full Code Here

Examples of javax.jms.TopicSubscriber

            publishMessages(session, publisher, topic, DeliveryMode.PERSISTENT, 1*1024, 1, "indifferent");
            session.commit();
            assertEquals("DurableSubscription backing queue should now have 2 messages on it",
                        Integer.valueOf(2), dursubQueue.getMessageCount());

            TopicSubscriber durSub = session.createDurableSubscriber(topic, SUB_NAME);
            Message m = durSub.receive(2000);
            assertNotNull("Failed to receive an expected message", m);
            m = durSub.receive(2000);
            assertNotNull("Failed to receive an expected message", m);

            session.commit();
            session.close();
        }
View Full Code Here

Examples of javax.jms.TopicSubscriber

    private void consumeDurableSubscriptionMessages(Connection connection, boolean selector) throws Exception
    {
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = null;
        TopicSubscriber durSub = null;

        if(selector)
        {
            topic = session.createTopic(SELECTOR_TOPIC_NAME);
            durSub = session.createDurableSubscriber(topic, SELECTOR_SUB_NAME,"testprop='true'", false);
        }
        else
        {
            topic = session.createTopic(TOPIC_NAME);
            durSub = session.createDurableSubscriber(topic, SUB_NAME);
        }


        // Retrieve the matching message
        Message m = durSub.receive(2000);
        assertNotNull("Failed to receive an expected message", m);
        if(selector)
        {
            assertEquals("Selector property did not match", "true", m.getStringProperty("testprop"));
        }
        assertEquals("ID property did not match", 1, m.getIntProperty("ID"));
        assertEquals("Message content was not as expected", generateString(1024) , ((TextMessage)m).getText());

        // Verify that no more messages are received
        m = durSub.receive(1000);
        assertNull("No more messages should have been recieved", m);

        durSub.close();
        session.close();
    }
View Full Code Here

Examples of javax.jms.TopicSubscriber

        Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
        conn.start();

        // create kipper
        Topic kipper = sess.createTopic("kipper");
        TopicSubscriber subscriber = sess.createDurableSubscriber(kipper, "kipper");

        subscriber.close();
        sess.unsubscribe("kipper");

        //Do something to show connection is active.
        sess.rollback();
        conn.close();
View Full Code Here

Examples of javax.jms.TopicSubscriber

        Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
        conn.start();

        // create kipper
        Topic kipper = sess.createTopic("kipper");
        TopicSubscriber subscriber = sess.createDurableSubscriber(kipper, "kipper");

        subscriber.close();
        try
        {
            sess.unsubscribe("kipper");

            //Do something to show connection is active.
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.