Gotcha
Gotcha

Reputation: 127

Metro with jax-ws override endpoint address

I'm creating a webservice client using Metro with jax-ws and I want to override the endpoint address.

Using the following example from 2.11.1. BindingProvider.ENDPOINT_ADDRESS_PROPERTY I can do that: http://metro.java.net/guide/How_to_invoke_and_endpoint_by_overriding_endpoint_address_in_the_WSDL.html

//Create service and proxy from the generated Service class.
HelloService service = new HelloService();
HelloPort proxy = service.getHelloPort();

((BindingProvider)proxy).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
                                                    "http://new/endpointaddress");

proxy.sayHello("Hello World!");

But I don't understand why I can't use the service.getHelloPort().sayHello("Hello World!") instead of proxy.sayHello("Hello World!") as the example shows. If I do, the webservice client is using its default endpoint address instead of the one I want to use.

It looks like I'm getting a new instance of HelloPort every time I call getHelloPort()

Can anyone explain this?

Upvotes: 0

Views: 1494

Answers (1)

Helter Scelter
Helter Scelter

Reputation: 715

there is little (read: no) difference between these:

service.getHelloPort().sayHello("Hello World");

and

HelloPort proxy = service.getHelloPort();
proxy.sayHello("Hello World!");

the service.getHelloPort() call will always return a new proxy/port instance. so any time you modify the request context for a given port object that modification is local to the specific port instance.

generally speaking the port instance you get back is re-usable and thread safe as long as you dont modify the request/response contexts. for the code sample you posted, it is modifying the request context to set the endpoint address, so it is advisable to get a new port object either every time you need one, or at the very least get a new object for each thread that needs one. (threadlocal is your friend for this)

Upvotes: 1

Related Questions