|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TODAY'S TOP SOA & WEBSERVICES LINKS Product Review Creating Web Services Using GLUE
Creating Web Services Using GLUE
By: Shannon Ma
Jul. 24, 2003 12:00 AM
GLUE, by The Mind Electric, is a framework for developing and publishing Web services. It is simple, easy, and fast to create Web services. GLUE supports SOAP1.2, WSDL1.1, and UDDI v2. It comes in two editions: GLUE standard is free, and GLUE Professional has more advanced features. The Mind Electric hosts a Yahoo group for developers to post questions and share knowledge. This article provides an introduction to how to use GLUE, including publishing and invoking Web services, working on SOAP messages, using SOAP over JMS, publishing EJBs as Web services, and publishing and inquiry using UDDI.
A Simple Web Service
public interface OnlineBookStore Suppose we have a class, OnlineBook StoreImpl, that implements the interface. For simplicity, the implementation will return three books if the keywords are "java web service" (see Listing 1; the code for this article is online at www.sys-con.com/webservices/sourcec.cfm). Now we want to publish this class as a Web service. Using GLUE, we only need two lines. First, we start up an HTTP Web server on local host using port 8000, and give an application name "book". Second, we publish an instance of the class as a web service with the name "Online BookStore".
electric.server.http.HTTP.startup GLUE will generate the WSDL (Web Service Description Language) file automatically and you can access it from http://localhost:8000/book/OnlineBookStore.wsdl. The WSDL file for our service is in Listing 2. Invoking a Web service is equally simple. First, get the URL of the WSDL of the service and bind to the service by passing the WSDL and the interface class. Then, invoke the method just like a normal method invocation.
String wsdl =
Working with SOAP Messages This time we'll perform a keyword search on books using Amazon.com's Web services. Amazon.com Web services offer applications that range from retrieving information about a set of products to adding an item to a shopping cart. The WSDL file of the Amazon.com Web services can be found at http://soap.amazon.com/schemas2/AmazonWebServices.wsdl. 1. Make a SOAP connection: We can get the endpoint of the Web services from the WSDL file and create a logic connection to the endpoint. In our example, the endpoint is http://soap.amazon.com/onca/soap.
String endpoint = "http://soap.amazon.com/onca/soap"; 2. Create a SOAP message, and add a "SOAPAction" to the HTTP header: The value of the SOAP action is also read from WSDL.
electric.soap.SOAPMessage request = 3. Create a SOAP envelope and set some general namespaces.
electric.xml.Element envelope = request.addEnvelope(); 4. Create a SOAP body: Set the message name (KeywordSerarchRequest), SOAP encoding style, and namespace.
electric.xml.Element body = request.addBody();
5. Put the keyword search parameters into the message: Under the "KeywordRequest" element, specify the following: 6. Invoke the Web service by calling invoke on the SOAPConnection: Pass the SOAP message we just created and get a list of books matching the keyword "java web services".
electric.soap.SOAPMessage response = Listing 4 is the generated "KeywordSearchRequest" SOAP request message. The complete code for the SOAP message is in Listing 5.
SOAP over JMS Publishing a Web service using JMS is similar to publishing one using HTTP. Start the SonicMQ Broker, do the following, and the Web service will be ready over JMS:
electric.server.jms.JMS.startup( "jms:///book" ); It creates a queue named "GlueQueue/ book" in SonicMQ. To invoke the service, just bind to the service using JMS and call the method.
String wsdl = "jms:///book/OnlineBookStore.wsdl"; The example above uses synchronous messaging. JMS also provides asynchronous messaging that loosely couples the sending and receiving of the messages. To receive SOAP messages asynchronously, we need to add another method with a different signature to our interface OnlineBookStore .
public void keywordSearch You may have noticed that the new method is different from the previous method in that it has an extra parameter of type Async and the return type is void. Now we add the implementation of the method in our class OnlineBookStoreImpl. Publishing the Web service is the same for both synchronous and asynchronous messaging. The difference is the invoking. Contrary to the invoking of a synchronous method, when you invoke an asynchronous method there is no response returned as the result of the method call. Instead, after binding to the Web service we need to create an instance of Async and pass it to the asynchronous method. Now we can do something else, then call getResponse on Async to get the result (see Listing 7).
Publishing EJBs as Web Services We will show, in the example, how to publish a stateless session bean that runs on WebLogic App Server. First we'll create a GLUE application to run in WebLogic using the 'newapp' command-line utility. > newapp -h ejb2ws Now we have a Web application called ejb2ws. Next we'll create an XML file called ejb2ws.xml and save it under ejb2ws\WEB-INF\services. The file will look like Listing 8, assuming that we have an EJB with the JNDI name OnlineBookStore and a home interface named OnlineBookStoreHome. The ejb2ws.xml file provides the StatelessSessionBeanService with information on both the BEA WebLogic Server and the stateless session bean. We can add the following to instruct WebLogic to load the GLUE application to config.xml of the domain of the application server, assuming that our ejb2ws application is under d:\myapplication.
Starting the WebLogic Server will expose the stateless session bean as a Web service.
Using UDDI The following code snippet demonstrates publishing a service through UDDI. For simplicity, we use GLUE's UDDI server.
electric.server.http.HTTP.startup Now we are ready to publish our online book service. Remember that a valid user is required when publishing a service (Listing 9 is the code to create a user). 1. Create a UDDI client connecting to the UDDI server.
String inquiryURL = 2. Define the business and add some contact information.
electric.uddi.Business business = 3. Categorize the business as bookstore (code 451211) using the NAICS 2002 code (North American Industry Classification System) taxonomy.
electric.uddi.Category cat = 4. TModel provides information about a Web service specification, categorization specification, or identifier specification. Here we create a TModel categorized as "wsdlSpec" using the "uddi-org:types" taxonomy, which means that it points to the relevant WSDL file.
electric.uddi.TModel tModel = The complete code is shown in Listing 10. As I mentioned earlier, another big use for UDDI is to perform inquiries. The example here demonstrates an inquiry using GLUE by connecting to the XMethods' UDDI server. XMethods is a "virtual laboratory" for developers, listing publicly available Web services and showcasing new ways of using this particular technology. 1. Create a UDDI client connecting to the XMmethods UDDI server.
String inquiryURL = "http://uddi.xmethods.net/inquire"; 2. Search a list of businesses containing the word "bookservice", and get the TModel of the first one found.
electric.uddi.FindTModels findTModels = 3. Get information for the services with bindings to implement the TModel, and get the service of the first match.
electric.uddi.FindServices findServices = 4. Get the first binding of the service and obtain the endpoint address and URL of the WSDL.
electric.uddi.Binding binding = service.getBindings()[ 0 ]; The results we get are:
wsdlURL = http://hosting.msugs.ch/cheeso9/books/books.asmx? The complete code is shown in Listing 11.
Summary
References SOA WORLD LATEST STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||