1 Reply Latest reply on Feb 5, 2004 5:18 AM by adrian.brock

    CLIENT_ACKNOWLEDGE not working as expected.

    bh5k

      I am putting together some small sample code for a presentation and one of the questions that I was asked was how a cient could know to wait until all messages were finished, basically making the client part of it synchronous. I was under the impression that if I set the acknowledgement of it to CLIENT_ACKNOWLEDGE, it would do this:

      TopicSession session = conn.createTopicSession(false, TopicSession.CLIENT_ACKNOWLEDGE);

      I can't tell any difference between CLIENT or AUTO? Do I have something configured wrong or should I be looking at an entire different mechanism altogether?


      The Publisher code is :


      Properties env = new Properties();
      env.put(Context.INITIAL_CONTEXT_FACTORY, "javax.naming.InitialContext");
      env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      env.put(Context.PROVIDER_URL, "jnp://localhost:1099");

      InitialContext iniCtx = new InitialContext(env);
      TopicConnectionFactory tcf = (TopicConnectionFactory) iniCtx.lookup("ConnectionFactory");

      TopicConnection conn = tcf.createTopicConnection();
      Topic top = (Topic) iniCtx.lookup("topic/MyTopic");
      TopicSession session = conn.createTopicSession(false, TopicSession.CLIENT_ACKNOWLEDGE);

      TopicPublisher pub = session.createPublisher(top);
      conn.start();
      System.out.println("Publishing message " + System.currentTimeMillis());

      for (int i = 0; i < 10; i++) {
      TextMessage message = session.createTextMessage();
      message.setText("Message number : " + i);

      pub.publish(message, DeliveryMode.PERSISTENT, Message.DEFAULT_PRIORITY, 1800000);

      }

      System.out.println("Published message(s) " + System.currentTimeMillis());

      conn.close();


      The Listener code is :

      Properties env = new Properties();
      env.put(Context.INITIAL_CONTEXT_FACTORY, "javax.naming.InitialContext");
      env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
      InitialContext iniCtx = new InitialContext(env);
      TopicConnectionFactory tcf = (TopicConnectionFactory) iniCtx.lookup("ConnectionFactory");
      conn = tcf.createTopicConnection();
      Topic top = (Topic) iniCtx.lookup("topic/MyTopic");
      TopicSession session = conn.createTopicSession(false, TopicSession.CLIENT_ACKNOWLEDGE);

      TopicSubscriber recv = session.createDurableSubscriber(top, name);
      recv.setMessageListener(this);

      .
      .
      .

      public void onMessage(Message message) {
      TextMessage txtMsg = (TextMessage) message;
      try {
      System.out.println(
      "Receiving messages " + name + " " + System.currentTimeMillis() + " " + txtMsg.getText());

      try {
      Thread.sleep(1000);
      }
      catch (InterruptedException ie) {
      ie.printStackTrace();
      }

      message.acknowledge();
      }
      catch (JMSException e) {
      e.printStackTrace();
      }
      }