TOTD #2: Change the endpoint address on a pre-generated Web services Stub

The client-side stubs generated by JAX-WS (part of Metro) are pre-configured with the endpoint address specified in the WSDL. This allows you to invoke the endpoint directly without any additional configuration. However there may be a need to send the exact same Web service request to an endpoint hosted at a different address. For example, the stubs may be generated from a WSDL hosted in the test environment and the request needs to be sent to an endpoint hosted in production environment that is very likely to be on a different machine.

This tip tells you how to change the endpoint address on a pre-generated stub so that it invokes a different endpoint.

The code to invoke a pre-configured stub (using the endpoint address from the WSDL) looks like:

Hello port = new HelloService().getHelloPort();
String result = port.sayHello("Duke!");

As per the JAX-WS 2.1 specification, each proxy implements javax.xml.ws.BindingProvider interface. In order to invoke an endpoint whose address is different from the one specified in the WSDL, you need to insert the following line between the two lines shown above:

((javax.xml.ws.BindingProvider)port).getRequestContext().put(javax.xml.ws.BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "NEW_ADDRESS_HERE");

Just replace NEW_ADDRESS_HERE with your new endpoint address and the exact same request will be directed to the new endpoint. The WSDL contract must be exactly same though otherwise you may get an exception back.

Please leave suggestions on TOTD that you'd like to see. A complete archive is available here.

Technorati: totd webservices metro soap glassfish

Comments:

Hi, Arun!
I found some limits in your suggestion posted to Change the endpoint address. In your case, the app server where the pre-generated stubs come from must be started. If not, this line "Hello port = new HelloService().getHelloPort();" could not be excuted correctly because the method firstly would access the wsdl pre-defineded and if it failed, then exception was thrown. So, "((javax.xml.ws.BindingProvider)port).getRequestContext().put(javax.xml.ws.BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "NEW_ADDRESS_HERE");" would never be excuted.

I tried another way, and found it was OK and didn't need the original app server be started. It needed to define the new endpoint URL and the QName. For example,

URL endpoint_new = new URL("NEW_ADDRESS_HERE");
QName qname = new QName("http://"+"ORIGINAL_PACKAGE","SERVICENAME");

ORIGINAL_PACKAGE means the package where the service published in, SERVICENAME means the name of the service we need, for example,HelloService.
Put these two line in top of the method, and do not need the "((javax.xml.ws.BindingProvider)port).getRequestContext().put(javax.xml.ws.BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "NEW_ADDRESS_HERE");"

Posted by Qil.Wong on August 23, 2007 at 06:51 PM PDT #

Sorry, I lost some information.

After definition of new endpoint URL and QName, we create service object by:
Hello port = new HelloService(endpoint_new,qname).getHelloPort();

replace "Hello port = new HelloService().getHelloPort();"

Posted by Qil.Wong on August 23, 2007 at 06:56 PM PDT #

Thanks for the tip on TOTD :)

Posted by Arun Gupta on August 27, 2007 at 06:28 AM PDT #

Arun,

I think the commenter above was correct in pointing out the need for the server holding the original WSDL to be online in your example, I think he missed the spirit of your point, which I think was once you instantiate the proxy, you can dynamically change the service endpoint address without reinstantiating the proxy. Your example, is more robust for the situation, where you may be calling many instances of the web service (which could be one instance per customer in a service bureau type environment).

Regards,

John

Posted by John LeJeune on July 15, 2011 at 08:24 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today