X
Business

Publishing Web services from the desktop

Mustang supports both consuming and publishing Web services out of the box. Lee Chuk-Munn shows how easy it is to publish Web services.
Written by Lee Chuk-Munn, Contributor
In a previous article, I demonstrated how easy it is to consume web services with Mustang. As you know, Mustang is the project name for the next version of Java Standard Edition 6. Mustang supports both consuming and publishing Web services out of the box. In this article we turn our attention to publishing web services.

Prior to Mustang, publishing Web services was a server-side affair. With Mustang, any Java application can expose part or all of its functionalities as Web service(s); this includes desktop Java applications. Question: Why would you want to Web service-enable your desktop application?

You have developed a Java word processor. You might want to develop a feature where others can view a document while it is being edited. One simple way to do that is to use the trend du jour: Web services. The service would be available while you are editing your document, but when you close the word processor, the service magically disappears. Convenient.

Here is what you need to do if you wish to publish a Web service:

  1. Write your Web service. Annotate the class and any method with the @WebService and @WebMethod annotation, respectively.
  2. Run wsgen to generate the Web service artifacts like the schema file and WSDL.
  3. Integrate the Web service class into your application and publish it.

For the purpose of this discussion, we will implement a simple UUID generator. What is a UUID (eg. 550e8400-e29b-11d4-a716-446655440000)? A UUID is a universally unique identifier. It is a 128-bit quantity that is typically used in software components to uniquely identify themselves on the network. The domain space of UUID is said to be so big that it is almost impossible to generate two same UUID unintentionally.

Writing the Web Service
The following is a listing of our Web service. We use the UUID class in the JDK to generate UUIDs.

package pkg;
import java.lang.*;
import java.util.*;
import javax.jws.*;
@ WebService
public class UUIDGenerator {
   @ WebMethod
   public String getUUID() {
      return ( UUID.randomUUID(). toString());
   }
}

The Web service is a simple POJO (Plain Old Java Object). It has a single method called @getUUID. We turn the POJO into a Web service by first annotating the class with @WebService annotation. Now for every method that we wish to be a Web service, we annotate the method with @WebMethod. Note that Web service methods must be public. There are other annotations like @WebParam and @WebResult to further refine your Web service. I will leave this as an exercise for the reader to pursue.

Generate Web Service Artifacts
Compile the UUIDGenerator class. Run wsgen to generate the Web service artifacts like so:

wsgen -cp . -s src pkg.UUIDGenerator

The cp specifies the location of your Web service. If you are interested in looking at the Java sources that are generated, use the -s option to indicate the directory where the sources are kept. wsgen also generates the schema and the WSDL file for the Web service.

Publishing the Service
Publishing Web services is extremely simple. Endpoint.publish() method publishes our Web service to the world. The following code snippet shows how this is done:

UUIDGenerator uuid = new UUIDGenerator ();
   //Create and publish the endpoint
   Endpoint endpoint = Endpoint.publish("http://localhost:8080/uuid", uuid);

The service is now accessible at http://localhost:8080/uuid and the WSDL document is available at http://localhost:8080/uuid?WSDL.

You can set the number of threads handling your Web service. This method requires a few more lines of code as shown below:

UUIDGenerator uuid = new UUIDGenerator();
   //Create an endpoint wit the web service
   Endpoint endpoint = Endpoint.create( uuid);
   //Create a thread pool of 5 threads
   ExecutorService executor = Executors.newFixedThreadPool(5);
   //Pass the thread pool to the endpoint
   endpoint.setExecutor(executor);
   //Publish the service
   endpoint.publish("http://localhost:8080/uuid");

Verify that the UUID Web service is indeed working by writing a client. I will set this as an exercise for the reader. You can look at my previous article for pointers.

Lee Chuk-Munn has been programming in the Java language since 1996, when he first joined Sun Microsystems in Hong Kong. He currently works as a senior developer consultant and technology evangelist for Technology Outreach at Sun in Singapore. Chuk's focus is in Java APIs, Java EE, Java SE, and Java ME. Chuk graduated in 1987 from the Royal Melbourne Institute of Technology in Melbourne, Australia, where his favorite subject was compiler theory.

Editorial standards