Friday, December 5, 2008

Integrating XMPP into JBoss ESB

I am looking for a better way to integrate new providers into JBoss ESB, while I have no success on this journey I can share some thoughts about some cool stuff as I will show you know.

First of all I am using the Ignite opensource APIs and Products. In order to have my own Jabber Server, I installed Openfire, which is pretty easy.

Once I started the service I opened the Admin UI in my browser and did a very simple setup, you can see the Openfire opening the address: http://localhost:9090 :




I added 2 users: edgar and joão(John if english), my objective is that JBoss ESB will hold the buddy "Joao", where I can interact with them via my IM program that supports a XMPP/Jabber protocol, GTalk in windows for instance.

I wanna send some message from my IM program, and I wanna transform this "buddy message" into a ESB Message in the bus.

I am using a strategy to use Schedulers as my listeners, so I created a class that basically uses the Smack API to interact with Xmpp protocol. See my following scheduler:


package org.demo.smackesb;

import org.jboss.soa.esb.ConfigurationException;
import org.jboss.soa.esb.client.ServiceInvoker;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.listeners.message.MessageDeliverException;
import org.jboss.soa.esb.message.Message;
import org.jboss.soa.esb.message.format.MessageFactory;
import org.jboss.soa.esb.schedule.ScheduledEventListener;
import org.jboss.soa.esb.schedule.SchedulingException;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.FromContainsFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Packet;

public class XmppListener implements ScheduledEventListener {

protected boolean started = false;
protected ConnectionConfiguration config;
protected XMPPConnection connection;

public void onSchedule() throws SchedulingException {

}

public void initialize(ConfigTree arg0) throws ConfigurationException {

try {
config = new ConnectionConfiguration("localhost", 5222, "es");

connection = new XMPPConnection(config);

connection.connect();

connection.login("joao", "123", "Home");

PacketFilter filter = new AndFilter(new PacketTypeFilter(
org.jivesoftware.smack.packet.Message.class),
new FromContainsFilter("edgar"));

PacketListener myListener = new MyTracker();

connection.addPacketListener(myListener, filter);

} catch (XMPPException e) {

e.printStackTrace();
}
}

public void uninitialize() {
connection.disconnect();

}

class MyTracker implements PacketListener {

public void processPacket(Packet packet) {

// dispatch messages to ESB from a Jabber Client

org.jivesoftware.smack.packet.Message msg = (org.jivesoftware.smack.packet.Message) packet;

System.out.println("Will forward the Message: " + msg.getBody());

try {

ServiceInvoker invoker = new ServiceInvoker("Extra", "Jabber");

Message message = MessageFactory.getInstance().getMessage();

message.getBody().add(msg.getBody());

invoker.deliverAsync(message);

} catch (MessageDeliverException e) {

e.printStackTrace();
}

}

}

}





Basically, I have my Service transforming incoming buddy message to ESB messages, see my jboss-esb.xml:


I am using Smooks as well to show that you can transform the incoming messages into really business messages to JBoss ESB.

See a screenshot of the demo working:





Download this demo from here, and if you wanna deploy into to your ESB, just drop it into your sample/quickstart folder, and call the command "ant deploy".

Another approach you may keep in mind when you are looking for some solution to integrate Xmpp with Java is Mobicents, which is a technology really nice for such integrations like that.

Hope you enjoy.

No comments: