Ruby as Enterprise Glue

How Ruby can glue together a vast number of enterprise resources.

We have derived our servlet from class AbstractServlet. The WEBrick server calls the do_GET method whenever it receives a GET request for a certain URL. Accordingly, it calls do_POST, do_PUT and so on for other HTTP request methods. WEBrick always passes a Request and a Response object to the method it calls. Request objects contain all query parameters and headers that were sent by the client. It's your task to fill the Response object with a body and all headers that should be sent back.

In our case, the servlet logic reads like a pseudo-code specification. We try to read the geographical position of the customer having the ID customer_id from the database. If we cannot find it, we localize the customer's address using the localization service and store the coordinates in the database, so we do not have to localize it again. Next, we turn the coordinates into an XML document. At the end of the method, we set the content type, the HTTP status code and the body.

You do not have to define an initialize method for a servlet, but if you do, it always gets the server instance as its first argument. In our case, we also expect the name of the WSDL to be used to initialize the localization service.

The to_xml method converts a location into an XML document. Too often, developers use raw strings to create XML documents. I recommend never doing that, even for apparently trivial documents. Creating XML documents never is as easy as it seems, because you have to care about difficult concepts, such as well-formedness and character set encodings. Hence, we use Jim Weirich's XmlBuilder class (see Resources) to create the result document.

Now we have a servlet that implements our main logic, but a servlet alone won't cut it. We still have to create an HTTP server that controls it. Listing 9 is everything we need. We specify the port on which the server is listening and map our LocalizationServlet to the path /. In addition, we make the server terminate whenever it receives a SIGINT or SIGTERM signal.

A Final Test Run

It's time for a final test run. Point your favorite browser to http://localhost:4242/?customer_id=1 or use a command-line tool such as wget or curl to test our newly created service:


mschmidt:/tmp $ curl http://localhost:4242/?customer_id=1
<position longitude="97.03" latitude="32.9"/>
mschmidt:/tmp $

That's exactly the result we have expected. We're done!

Conclusion

There's no doubt, regarding enterprise integration, Ruby is ready for prime time. Even in this short article, we were able to cover some of the most important enterprise technologies, such as relational databases, SOAP and HTTP. You also can integrate your existing Java code, access LDAP servers, create XML-RPC services or manipulate XML documents with ease.

Ruby cannot compete in many respects with platforms such as J2EE or .NET, but it doesn't have to, and it doesn't want to. Its strengths are flexibility, maintainability and speed of development. Although the Ruby platform might not be the biggest compared to other dynamic languages, it might well be the one that's growing fastest. And, most important, it's a lot of fun!

Resources for this article: /article/9018.

Maik Schmidt has worked as a software developer for more than a decade. He makes a living from creating complex solutions for mid-size enterprises. Besides his day job he writes book reviews and articles for computer science magazines and contributes code to open-source projects. Recently he has written his first book called Enterprise Integration with Ruby.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I too don't understand

Raimond's picture

I too don't understand why:

ERROR SOAP::WSDLDriverFactory::FactoryError: no ports have soap:address
/usr/lib/ruby/1.8/soap/wsdlDriver.rb:75:in `find_port'
/usr/lib/ruby/1.8/soap/wsdlDriver.rb:37:in `create_rpc_driver'
./loc_service.rb:6:in `initialize'
./servlet.rb:11:in `initialize'
/usr/lib/ruby/1.8/webrick/httpservlet/abstract.rb:23:in `get_instance'
/usr/lib/ruby/1.8/webrick/httpserver.rb:102:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
server.rb:8

Please say me why there wrong?
Thanks!

ERROR SOAP::WSDLDriverFactory

kumpfjn's picture

I get this error from my soap server (from listing 7) but I don't understand why:

ERROR SOAP::WSDLDriverFactory::FactoryError: no ports have soap:address
/usr/lib/ruby/1.8/soap/wsdlDriver.rb:75:in `find_port'
/usr/lib/ruby/1.8/soap/wsdlDriver.rb:37:in `create_rpc_driver'
./loc_service.rb:6:in `initialize'
./servlet.rb:11:in `initialize'
/usr/lib/ruby/1.8/webrick/httpservlet/abstract.rb:23:in `get_instance'
/usr/lib/ruby/1.8/webrick/httpserver.rb:102:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
server.rb:8

Thanks.

ERROR SOAP::WSDLDriverFactory

kumpfjn's picture

I get this error from my soap server (from listing 7) but I don't understand why:

ERROR SOAP::WSDLDriverFactory::FactoryError: no ports have soap:address
/usr/lib/ruby/1.8/soap/wsdlDriver.rb:75:in `find_port'
/usr/lib/ruby/1.8/soap/wsdlDriver.rb:37:in `create_rpc_driver'
./loc_service.rb:6:in `initialize'
./servlet.rb:11:in `initialize'
/usr/lib/ruby/1.8/webrick/httpservlet/abstract.rb:23:in `get_instance'
/usr/lib/ruby/1.8/webrick/httpserver.rb:102:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
server.rb:8

Thanks.

Don't know but probably this

Zahnarztsuche's picture

Don't know but probably this can help you:
http://www.thescripts.com/forum/thread509677.html

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix