Enterprise Integration Zone is brought to you in partnership with:

Graduated in computer science at the University of Oldenburg, Germany. Develops software for over 20 years now. Likes to program in Java. Wrote and contributed Maven plugins and implemented initially Maven support for IntelliJ IDEA. Ralf is a DZone MVB and is not an employee of DZone and has posted 18 posts at DZone. You can read more from them at their website. View Full User Profile

How To Dynamically Change a Web Service Endpoint URL with Metro

02.08.2013
| 6021 views |
  • submit to reddit
A really cool feature of GlassFish Metro (and maybe other webservice stacks) is the ability to change the webservice endpoint URL at runtime.

In general, you typically generate the code via JAX-WS maven plugin (or ant task), specifying the endpoint URL in the generation properties. This is a fast and reliable way to generate webservices. The downside is, that it seems you can't change the endpoint URL at runtime ...
Well, you can. And it is pretty simple.

The example in this posting is taken from the Metro FAQ.

You can use BindingProvider.ENDPOINT_ADDRESS_PROPERTY to set the endpoint address in your client application code.

Sample:

 //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!");  
With the property key

BindingProvider.ENDPOINT_ADDRESS_PROPERTY
you can also get the currently used endpoint url at runtime. This may be helpful for debugging purposes.

Sample:
 LOG.info("Using endpoint URL: "  
        + ((BindingProvider)proxy).getRequestContext().get(  
            BindingProvider.ENDPOINT_ADDRESS_PROPERTY)); 

Published at DZone with permission of Ralf Quebbemann, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Steven Goldsmith replied on Fri, 2013/02/08 - 10:58am

It's also handy to inject the resource JEE style in an MDB using ejb-jar.xml for example:


<env-entry>
  <env-entry-name>endpoint</env-entry-name>
  <env-entry-type>java.lang.String</env-entry-type>
  <env-entry-value>http://somehost:8080/MyService/MyImpl</env-entry-value>
</env-entry>

@Resource(name = "endpoint")
private String endpoint;

  public final String getEndpoint() {
  return endpoint;
  }

  MyService service = new MyService();
  // Create port
  port = service.getMyPort();
  final BindingProvider bp = (BindingProvider) port;
  // Add endpoint binding
  bp.getRequestContext().put(
  BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
  getEndpoint());




Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.