XFire Messaging
June 20th, 2005XFire’s new service model and channel features are something I’ve been meaning to write about for a while (this post prompted me to get off my behind – although I’m sure its not quite what the author had in mind). Warning: Currently this stuff in only in CVS and does not have a lot of documentation. With that said, we’re doing a release soon and I’m starting to work on documentation.
Message Channels
The best way to grok channels is to look at some code.
LocalTransport transport = new LocalTransport();
Channel channel1 = transport.createChannel("urn:xfire:local:Peer1");
channel1.open();
Channel channel2 = transport.createChannel("urn:xfire:local:Peer2");
channel2.setEndpoint(new DocumentEndpoint());
// Document to send
Element root = new Element("root");
root.appendChild("hello");
Document doc = new Document(root);
MessageContext context = new MessageContext();
OutMessage msg = new OutMessage("urn:xfire:local:Peer2");
msg.setSerializer(new DocumentSerializer());
msg.setBody(doc);
channel1.send(context, msg); Thread.sleep(1000);
channel1.send(context, msg); Thread.sleep(1000);
channel1.close(); channel2.close();
What happens here is we’re using the LocalTransport to send messages to another channel. Each channel has a unique uri (not url) which identifies them. In this example, the DocumentEndpoint receives messages on the channel and prints them to stdout for us to see. This is all completely stream based so we don’t have to passing around documents if we don’t want to, keeping memory low.
public class DocumentEndpoint
implements ChannelEndpoint
{
public void onReceive(MessageContext context, InMessage msg)
{
StaxBuilder builder = new StaxBuilder();
try
{
Document doc = builder.build(msg.getXMLStreamReader());
System.out.println(doc.toXML());
}
catch (XMLStreamException e)
{
e.printStackTrace();
}
}
}
Upon running this you’ll see this come out on your console:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><root>hello</root></soap:Body></soap:Envelope> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><root>hello</root></soap:Body></soap:Envelope>
This is really handy for just sending messages across the wire, routing them around, using as a soap stack base :-), etc. Right now we have local, http, and xmpp support in cvs. JMS support is coming Real Soon Now.
I’ll also be working with a student in the Google summer of code program to implement Reliable Messaging over top of this. One can see how easy it would be. Just create a new ReliableMessagingChannel which wraps an existing transport and sends/resends messages as needed.
June 21st, 2005 at 1:05 am
It is planned to bring back RSS from CVS feature for codehaus projects? (it is 404 from quite some time)