3 Replies Latest reply on Dec 8, 2002 3:35 PM by John Fawcett

    JMS PubSub Design Pattern Question

    Patrick Ryan Newbie


      Hello Everyone
      I have an application where we want to keep a group of machines in sync when updates happen on the other machines. One approach we are considering is to use JMS to create a single Topic with non-durable subscribers.

      My question is, since each machine is both a publisher and a subscriber to the same Topic, is there a way to instruct the JMS provider to not send the publisher their own message? For example, if we have to update an in memory field, then send a message to inform the other machine to make the same update, I dont want the originator to get a message informing them to make the update again ( its too time consuming ).

      Now I have thought about not allowing the original publisher to make the update, then send the message but to wait for the message then make the update. However, then I dependent upon a JMS provider running even to run the system using a single machine so I would rather not be reliant upon a JMS provider.

      Any thoughts on how to do that would be appreciated.

      Thanks
      Pat

        • 1. Re: JMS PubSub Design Pattern Question
          John Fawcett Newbie

          I think you want to set the nolocal flag to true on your topic session. you can see an example in the jboss testsuite distributed with the source, you want to look at the Class: org.jboss.test.jbossmq.test.JBossMQUnitTestCase
          the relevent method is named: testTopicNoLocal.

          for your convenience here is the snippet:
          /**
          * Test to seeif the NoLocal feature of topics works.
          * Messages published from the same connection should not
          * be received by Subscribers on the same connection.
          */
          public void testTopicNoLocal() throws Exception
          {
          getLog().debug("Starting TopicNoLocal test");
          connect();

          TopicConnectionFactory topicFactory = (TopicConnectionFactory)context.lookup(TOPIC_FACTORY);
          TopicConnection topicConnection1 = topicFactory.createTopicConnection();
          topicConnection1.start();
          TopicConnection topicConnection2 = topicFactory.createTopicConnection();
          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);
          }
          }

          topicConnection1.stop();
          topicConnection1.close();
          topicConnection2.stop();
          topicConnection2.close();

          disconnect();
          getLog().debug("TopicNoLocal test passed");
          }

          • 2. Re: JMS PubSub Design Pattern Question
            Patrick Ryan Newbie

            Thank you! I should have seen that one. I really hate when someone has to read the spec to me. Thanks again.

            • 3. Re: JMS PubSub Design Pattern Question
              John Fawcett Newbie

              np. I have been spending alot of time with the testsuite lately...