08 February 2008

Groovy 01 - SOAP Server

As much as I like BeanShell (bsh), once I found Groovy, I don't think that I've done any new bsh development. I'm going to follow up on my earlier bsh blog entry where I promised to show cool bsh features but first I really want to lay the ground work for an Enterprise Service Bus (ESB) -like tool that I've been playing with called XMLBlaster. Now XMLBlaster doesn't call themselves an ESB. Instead they say they're MOM (Message oriented Middleware) with a lot of features.

They're website says XmlBlaster is a publish/subscribe and point to point MOM server which exchanges messages between publishers and subscribers. The message is described with XML-encoded meta information. Messages may contain everything, GIF images, Java objects, Python scripts, XML data, a word document, plain text. Communication with the server is based on CORBA (using JacORB) or RMI or XmlRpc, clients are free to choose their preferred protocol. Other protocols like email, socket or SOAP may be plugged in.

Like usual, I'm starting to get ahead of myself as I want to talk about Groovy. If you're not familiar with this great language, you must pick up Groovy In Action (GINA) as this is the Groovy Bible.

I have a Groovy SOAP Server as one "end point" or client and a number of other legacy app nodes. I'm currently using Groovy 1.0 but 1.5.4 is the latest. This new version brings features like Java 5 annotations, generics and enums, it provides significant performance gains, new meta-programming capabilities, new and improved tooling support like the new joint Groovy/Java compiler, the new interactive shell, or the Groovy Swing console, and a few other additions in its syntax, like the new Elvis operator or the ability to omit parentheses in methods with named arguments to make DSLs even more readable. Sounds great. I'm convinced so I'm upgrading as I'm writing this entry.

Grab the Groovy download and give it a try.

All installed? Great, let's move along. SOAP is a lightweight protocol intended for exchanging structured information in a decentralized, distributed environment. Groovy has a SOAP implementation based on Xfire which allows you to create a SOAP server and/or make calls to remote SOAP servers using Groovy. Get the Groovy SOAP jar and drop it into the $GROOVY_HOME/lib folder.

[edit 19 Feb 2008]
The Groovy website seems to have a broken link to download the soap jar. Try to get the jar at the link mentioned above but if that fails, try this:
http://momupload.com/files/77499/groovysoap-all-1.0-0.3-snapshot_jdk1.5.0.jar.html
[/edit]

First we're going to make an incredible simple SOAP server and then test it with the browser. Create a file named LogServer.groovy that looks like this or grab a copy here.

---------LogServer.groovy---------

import groovy.net.soap.SoapServer

// define the API
void log(logTime, logMessage) {
// real code goes here but we're just printing to stdout for now
println(logTime + ' : ' + logMessage)
}

// start server on localhost.
// use whatever port you want
def server = new SoapServer("localhost", 6980)
server.setNode("LogServer")
server.start()

-------------------------------------------

One of the great things with Groovy is that you can call Java classes from Groovy and Java can call Groovy. Because Groovy is Java. With this in mind, your SOAP server can invoke any Java legacy app. In the example above, we're just writing to std out.

Start it up by typing: groovy LogServer.groovy

In your browser, enter: http://localhost:6980

And you should see an XFire page of the available webservices at that port:

Available Services:
  • LogServerInterface [wsdl]

  • Generated by XFire ( http://xfire.codehaus.org )
Pretty neat, huh? Click on the Web Service Definition Language (WSDL) link and you'll see all the wsdl that was generated auto-magically from our very simple SOAP server.

Next time we'll write a quick SOAP client and test this server out. Until then, buy the book and read the Groovy.

No comments:

Post a Comment