powered by Jive Software

PUBSUB: Receiver not getting updated items

I am trying to write a simple pubsub application using Smack 3.1.0 and Openfire 3.6.4.

The publish code is as follows (basically connects to a local server, submits two items with unique itemId):

static Item createItem() { SimplePayload payload = new SimplePayload( "message", "pubsub:test:book", "<title>Lord of the Rings " + Long.toString(System.currentTimeMillis()) + " </title>"); String itemId = Long.toString(System.currentTimeMillis()); Item<SimplePayload> item = new Item<SimplePayload>(itemId, payload); System.out.println("Item: " + item.toXML()); return item; } static Node getNode( PubSubManager manager, List<Node> nodes, String nodeName ) throws XMPPException { for (Node node:nodes) {
      if (node.getId().contentEquals(nodeName)){
           return node;
      } } // needed first time only to configure node ConfigureForm form = new ConfigureForm(FormType.submit); form.setPersistentItems(false); form.setDeliverPayloads(true); form.setNotifyRetract(true); form.setPublishModel(PublishModel.open); form.setAccessModel(AccessModel.open); return manager.createNode(nodeName, form);
} public static void main(String[] args) { // connect as publisher ConnectionConfiguration config = new ConnectionConfiguration("localhost", 5222); XMPPConnection connection = new XMPPConnection(config); connection.connect(); connection.login("blacknblue", "blacknblue", "localhost")      // create node (comment second line and uncomment third to just get already created node PubSubManager pubSubManager = new PubSubManager(connection, "pubsub.113j64ba"); Node myNode; String nodeName = "TestNode4"; try {
      List<Node> nodes = pubSubManager.getNodes();
      myNode = getNode(pubSubManager, nodes, nodeName); } catch (XMPPException e) { // needed first time only to configure node ConfigureForm form = new ConfigureForm(FormType.submit); form.setPersistentItems(false); form.setDeliverPayloads(false); form.setNotifyRetract(true); form.setPublishModel(PublishModel.open); form.setAccessModel(AccessModel.open); form.setSubscribe(true); myNode = pubSubManager.createNode(nodeName, form); } List<Item> items = new LinkedList(); items.add(createItem()); try {
      Thread.sleep(500); } catch (InterruptedException e1) {
      e1.printStackTrace(); } items.add(createItem()); for (Item item: items) {
      myNode.send(item); } connection.disconnect();
}

The Console output shows:

Item: Lord of the Rings 1271366774967

Item: Lord of the Rings 1271366775468

items added: 2

The Receiver code is as follows:

public static void main(String[] args) {
        // connect as receiver
        ConnectionConfiguration config = new ConnectionConfiguration("localhost", 5222);
        XMPPConnection connection = new XMPPConnection(config); connection.connect(); connection.login("blacknred", "blacknred", "localhost");         // start listening for published items
        PubSubManager manager = new PubSubManager(connection, "pubsub.113j64ba");         String nodeName = "TestNode4";
        Node eventNode;
        eventNode = manager.getNode(nodeName);
        eventNode.addItemEventListener(new ItemEventListener()
        { @Override public void handlePublishedItems(ItemPublishEvent e) {
        for (Item<SimplePayload> item : e.getItems()) {
            System.out.println(item.getPayload().toXML());
        } }
        }
        );
         eventNode.subscribe("blacknred@113j64ba/localhost");         // do not die on me!
        while(true){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
             System.out.println("Interrupted: " + e.getMessage());
             break;
            }
        }
      connection.disconnect(); }

The Receiver Console output only shows one of the items:

Lord of the Rings 1271366775468

Also when I re-run the Publisher, The Receiver does not get the new items. When I re-run the Receiver, I still get the same item as above. What is happening to the other items that the Publisher submits? Why are they not getting to the receiver end.

Any assistance to clarify this issue is appreciated.

TIA

If you are running the receiver after the publisher, then that would explain why you are only receiving the second item published. Events are sent live and thus only those published after subscribing will be received. The exception to this is that the last item published will always be published to new subscribers, which is why you always get this item when you run your receiver.

As for running your publisher multiple times, since your publisher attempts to create the node each time, it should be failing due to the node already existing. This would mean that the subsequent items are not actually published.

That makes a little sense (atleast as to why the last item is being received the first time the Receiver is run).

In the publisher, however, I have written a function that gets the node if already present, otherwise creates a new node.

     myNode = getNode(pubSubManager, nodes, nodeName);

and then

      myNode.send(item);

where

**static** Node getNode( PubSubManager manager, List<Node> nodes, String nodeName ) **throws** XMPPException {
     **for** (Node node:nodes) {
          **if** (node.getId().contentEquals(nodeName)){
               **return** node;
          }
     }
     // needed first time only to configure node
     ConfigureForm form = **new** ConfigureForm(FormType.submit);
     form.setPersistentItems(**false**);
     form.setDeliverPayloads(**true**);
     form.setNotifyRetract(**true**);
     form.setPublishModel(PublishModel.open);
     form.setAccessModel(AccessModel.open);      **return** manager.createNode(nodeName, form);
}

So the subsequent times I run the Publisher, when I do the trace, it always returns the node, and it looks like it is sending the items to the same node as previous run. Debug Trace os smack also confirm that there is no error related to the publish. But the receiver doesn’t get the events.

Try replacing this code in your publisher

 Node myNode;
 String nodeName = "TestNode4";
 **try** {
      List<Node> nodes = pubSubManager.getNodes();
      myNode = getNode(pubSubManager, nodes, nodeName);
 } **catch** (XMPPException e) {
 // needed first time only to configure node

 ConfigureForm form = **new** ConfigureForm(FormType.submit);
 form.setPersistentItems(**false**);
 form.setDeliverPayloads(**false**);
 form.setNotifyRetract(**true**);
 form.setPublishModel(PublishModel.open);
 form.setAccessModel(AccessModel.open);
 form.setSubscribe(**true**);

 myNode = pubSubManager.createNode(nodeName, form);
 }

With this

 String nodeName = "TestNode4";
 Node myNode = pubSubManager.getNode(nodeName);

if (myNode == null)
{

    ConfigureForm form = **new** ConfigureForm(FormType.submit);
    form.setPersistentItems(**false**);
    form.setDeliverPayloads(**false**);
    form.setNotifyRetract(**true**);
    form.setPublishModel(PublishModel.open);
    form.setAccessModel(AccessModel.open);
    form.setSubscribe(**true**);

    myNode = pubSubManager.createNode(nodeName, form);

}


Get rid of your getNode() method altogether since it will no longer be necessary.

That worked. Thanks. But I’m surprised as to why the previous method was not working.

Hi Volna,

I am also doing the same work(Publish and subscribing). I found your coding at http://www.igniterealtime.org/community/message/202571#202571.

When i run that coding,

1.The publishing program works fine., Output in the console window:

Item: Lord of the Rings 1276166963609
Item: Lord of the Rings 1276166964109

2.The receiver program display like this:

**Error line: **eventNode = manager.getNode(nodeName);

Error is:

Exception in thread “main” item-not-found(404)
at org.jivesoftware.smackx.packet.SyncPacketSend.getReply(SyncPacketSend.java:53)
at org.jivesoftware.smackx.packet.SyncPacketSend.getReply(SyncPacketSend.java:61)
at org.jivesoftware.smackx.pubsub.PubSubManager.getNode(PubSubManager.java:143)
at rm.main(rm.java:37)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

  • Is any settings needed in openfire for pubsub program?
  • where and how to see the created node?

So What can i do for solving this problem,

Please reply.

Thanks.