consumerCon.connect();
addLoggingListeners("CONSUMER", consumerCon);
consumerCon.login("consumer", "consumer");
final ConsumerMessageListener listener1 = new ConsumerMessageListener();
consumerCon.getChatManager().addChatListener(new ChatManagerListener() {
public void chatCreated(Chat chat, boolean createdLocally) {
chat.addMessageListener(listener1);
}
});
//now create the producer
XMPPConnection producerCon = new XMPPConnection(config);
System.out.println("Connecting producer and consumer");
producerCon.connect();
addLoggingListeners("PRODUCER", producerCon);
producerCon.login("producer", "producer");
//create the chat and send some messages
Chat chat = producerCon.getChatManager().createChat("consumer", new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.out.println("Got XMPP message from chat " + chat.getParticipant() + " message - " + message.getBody());
}
});
for (int i = 0; i < 10; i++) {
System.out.println("Sending message: " + i);
Message message = new Message("consumer");
message.setType(Message.Type.chat);
message.setBody("Hello from producer, message # " + i);
chat.sendMessage(message);
}
//make sure the consumer has time to receive all the messages...
Thread.sleep(sleepTime);
//create an identical 2nd consumer
XMPPConnection lastguyCon = new XMPPConnection(config);
lastguyCon.connect();
addLoggingListeners("LASTGUY", consumerCon);
lastguyCon.login("consumer", "consumer");
final ConsumerMessageListener listener2 = new ConsumerMessageListener();
lastguyCon.getChatManager().addChatListener(new ChatManagerListener() {
public void chatCreated(Chat chat, boolean createdLocally) {
chat.addMessageListener(listener2);
}
});