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