In the previous chapter we wrote a simple class with no sign that it was a Web service. Now we will take the class and publish it as a Web service. In WSO2 SOA Enablement Server for Java, a Web service can be made from this class in two ways:
Runtime publishing is used for local publication of a service on an embedded server. The lifecycle of the service ends when the embedded server shuts down. Persistently deployed services are deployed remotely on a running server. They are persistent because they are automatically re-published when the server is shut down and then restarted.
Working with runtime publishing is simpler than with persistent deployment. It is recommended for running WSO2 SOA Enablement Server for Java from within an application, when using a previously developed service, and for rapid development of lightweight applications in general. For heavy-duty applications, we strongly recommend persistent deployment.
With these means of turning a Java class into a Web service, all the basic work is done for you by WSO2 SOA Enablement Server for Java. In most cases it is not even necessary to call the Java2WSDL tool explicitly, because WSO2 SOA Enablement Server for Java automatically creates the WSDL for you.
Each WSO2 SOA Enablement Server contains a local registry of Web services. This registry is accessible via org.systinet.wasp.webservice.Registry.
You publish a Web service by creating an instance and setting a Web service path. Here we use the Web service from the previous chapter, Writing Web Services:
Registry.publish("/HelloService", new HelloServiceImpl());
Before you can publish, you have to initialize and run the WSO2 SOA Enablement Server for Java server. This is a simple call:
Wasp.startServer("http://localhost:6060");
This is shown in Example 3.
Example 3. HelloServer
// Copyright WSO2 Inc. All rights reserved. // Use is subject to license terms. package example.basics.publishing.runtime; import example.basics.webservices.HelloServiceImpl; import org.systinet.wasp.Wasp; import org.systinet.wasp.webservice.Registry; public class HelloServer { public static void main(String[] args) throws Exception { // initialize WSO2 SOA Enablement Server for Java and start a server like 'localhost' Wasp.startServer("http://localhost:6060"); // publish HelloServiceImpl on path like '/HelloService' Registry.publish("/HelloService", new HelloServiceImpl()); System.out.println( "Service is published on http://localhost:6060/HelloService/wsdl'"); } }
In Example 3 we publish this service in an embedded server using a main() method. This method contains only the two methods we mentioned above, plus an output method.
Compile and run the server. (If doing this from the command prompt, remember to make sure the environment is set correctly.)
For a client invocation of this Web service, please see Web Service Clients.
If you have only one class and want to deploy it persistently, you need to run the server and then run the Overview. Our examples deploy the service featured in the previous chapters and in Example 3, assuming the compiled service resides in WASP_HOME/src/example/basics/webservices.
In order to deploy a single class, the Deploy tool may be used:
Deploy --target http://localhost:6060 --classpath ../src --class example.basics.webservices.HelloServiceImpl --uri /HelloService
In this example we are accessing a local HTTP server, localhost:6060. The deployed class is example.basics.webservices.HelloServiceImpl. (make sure it is in your CLASSPATH before you run the example). The required URI of the Web service is /HelloService.
For a client invocation of this Web service, please see Web Service Clients.
You may also wish to see Deploy Tool and Undeploy.