Both runtime publishing and client lookup of Web services are accomplished by one Java class in WSO2 SOA Enablement Server, org.systinet.wasp.webservice.Registry.
You publish a Web service by creating a public class that imports Registry and contains the following code:
Wasp.startServer("http"); Registry.publish("/MyService", new MyServiceImplementation());
The first line is a static method that starts the WSO2 SOA Enablement Server. The second line publishes the Web service. (See the HelloService example)
This is a simple String return to demonstrate Runtime Publishing. To run it, you must have your environment variables set correctly (JAVA_HOME, WASP_HOME, CLASSPATH, PATH).
The core of a Web service is its implementation class. The HelloService implementation class contains only one real method, a String hello(...) that returns a message.
For brevity, the publishing method has also been included in the implementation class as shown in Example 46.
Example 46. HelloService Implementation and Publishing
// Copyright WSO2 Inc. All rights reserved. // Use is subject to license terms. package example.basics.publishing.runtime; import org.systinet.wasp.Wasp; import org.systinet.wasp.webservice.Registry; public class HelloServiceImpl { static String servicePath = "/HelloService"; public String hello(String message) { return "Hello, " + message + "!"; } public static void main(String[] args) throws Exception { // initialize WASP and start server Wasp.startServer("http://localhost:6060"); // publish HelloService on path set in servicePath Registry.publish( servicePath, new HelloServiceImpl()); System.out.println("HelloService is published on " + servicePath); } }
With the correct environment variables set, compile and run this class to begin WSO2 SOA Enablement Server. You should get the display HelloService is published on /HelloService.
For completeness, we include a simple client here to look up the HelloService, although client lookup is dealt with in more detail in Dynamic Proxy Invocation. On the client side, an interface is usually used to allow the Web service to be accessed as a local class as shown in Example 47.
Example 47. HelloService Interface
// Copyright WSO2 Inc. All rights reserved. // Use is subject to license terms. package example.basics.invocation; public interface HelloService implements HelloService { String hello(String message); }
The client only performs one critical action, Registry.lookup as shown in Example 48.
Example 48. HelloServiceClient
// Copyright WSO2 Inc. All rights reserved. // Use is subject to license terms. package example.basics.invocation; import org.systinet.wasp.webservice.Registry; public class HelloClient { private static final String WSDL_URL = "http://localhost:6060/HelloService/wsdl"; public static void main(String[] args) throws Exception { HelloService proxy = (HelloService) Registry.lookup(WSDL_URL, HelloService.class); System.out.println(proxy.hello("world")); } }
Compile and run the client. (If doing this from the command prompt, remember to open a new command prompt and make sure the environment is set correctly.) You should get the display Hello, Hello service!!
For more information on clients using Registry.lookup, see Dynamic Proxy Invocation.
In Registry: Publish and Lookup, we considered the simplest use of Registry.publish: to publish a service in the runtime given its path and implementation class.
More advanced runtime publishing scenarios are possible with the following Runtime.publish(...) methods:
publish(ServiceEndpoint serviceEndpoint), for publishing services that have been custom configured with ServiceEndpoint.
getServiceEndpoint(path) and getServiceEndpoint(path, version) for publishing a persistently deployed service bound to the specified path (endpoint version may be specified as well).
getServiceEndpoints() for publishing all endpoints deployed on a server
Registry.publish can also be used with the ServiceEndpoint class as a parameter. This allows you to custom configure the service before publishing it, as described in ServiceEndpoint.
The code in Example 49 is for configuring and publishing a new service endpoint with an interceptor (see Transport Interceptors).
Example 49. Configuring and Publishing New Service Endpoint with Interceptor
... ServiceEndpoint serviceEndpoint = ServiceEndpoint.create(SIMPLESERVICE_URI, new SimpleService2()); serviceEndpoint.getInterceptors().insert(new MyInterceptor(), Interceptors.DIRECTION_IN); Registry.publish(serviceEndpoint); ...
In Example 50 you can see how to use Registry.pubish(ServiceEndpoint endpoint) to publish a service with a custom WSDL. Note the use of WSDL_CONTEXT to define the base context used for resolving WSDL imports.
Example 50. Publishing Services with Custom WSDL
WSDLReader reader = WSDLFactory.newInstance().newWSDLReader(); Definition wsdl = reader.readWSDL(WSDL_CONTEXT, WSDL_LOCATION); ServiceEndpoint endpoint = ServiceEndpoint.create(SERVICE_PATH, new Service()); endpoint.setWSDL(wsdl); Registry.publish(endpoint);
![]() | Note |
---|---|
For a conceptual discussion of service endpoints, please see WS Components and Lifecycle. |
The server typically includes both Web services published by runtime and those persistently deployed by deployment tools. Both are accessible to runtime publishing by the Registry.getServiceEndpoint/Endpoints(...) methods.
Using getServiceEndpoint(path) or getServiceEndpoint(path, version) gets the service endpoint bound to a specific path.
In some cases you may need an array of all the Web services published or deployed to a server. This is returned by the getServiceEndpoints() method.
Services are unpubished in runtime by one of these methods:
unpublish(path) or unpublish(path, version) for removing a service endpoint by its path (version optional)
unpublish(ServiceEndpoint serviceEndpoint) for removing a service endpoint by its instance name.
In all cases, when a service endpoint is removed by Registry.unpublish(...), the service instance is also removed if there are no other endpoints that reference it. For more on ServiceEndpoint and service instances, see Service Settings.
See the org.systinet.wasp.webservice.Registry Javadoc for a listing of all Registry methods.