Thursday, July 17, 2008

Dynamic Invoking of Web Service Methods

When creating a JAX-WS client a Class like the following is created,

public class ServiceName extends Service {
private final static URL SERVICENAME_WSDL_LOCATION;
static {
URL url = null;
try {
url = new URL("http://IP:PORT/CONTEXTPATH/ServiceName?WSDL");
} catch (MalformedURLException e) {
e.printStackTrace();
}
SERVICENAME_WSDL_LOCATION = url;
}

public ServiceName(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}

public ServiceName() {
super(SERVICENAME_WSDL_LOCATION, new QName("http://PACKAGENAME/", "SERVICENAME"));
}

Anyone can see this class in netbeans from file section going through build->generated->wsimport->client and then under the given package name. Naturally, when we create an instance of this service using IDE generated code, what happens is IDE puts the second constructor for it,

ServiceName service = new ServiceName();

which takes the static wsdl url. And now look at the first constructor, if we create service instance by using first constructor in our code then, wsdlLocation and serviceName parameters can be drawn from a configuration xml file. So, if we have done this in our project then if sevice wsdl location changed then we don't have to recompile the project by creating new JAX-WS client with providing the new wsdl location. We just have to change the wsdl url location in the configuration file. And getting the port from service then we can access the methods under that web service.

No comments: