java - How do I autmatically set a JAX-WS HTTP header that varies on each request -
i want set dynamically generated http header on each soap jax-ws request.
if wanted set same http header on each jax-ws request use technique here, i.e.
public class myapplicationclass { // inject instance of service's port-type. @webserviceref(echoservice.class) private echoporttype port; // method invoke web service operation , send transport headers on request. public void invokeservice() { // set map contain request headers. map<string, object> requestheaders = new hashmap<string, object>(); requestheaders.put(“myheader1”, “this string value”); requestheaders.put(“myheader2”, new integer(33)); requestheaders.put(“myheader3”, new boolean(true)); // set map property on requestcontext. bindingprovider bp = (bindingprovider) port; bp.getrequestcontext().put(com.ibm.websphere.webservices.constants.request_transport_properties, requestheaders); // invoke web services operation. string result = port.echostring(“hello, world!”); } }
however, here want use different http header each request. want include x-requestid
header or similar random value, receiving server can distinguish between requests duplicated on timeout either java client or (worse) inline http proxy.
moreover, jax-ws retries same call, don't want regenerate header (obviously).
note application covered in equivalent of port.echostring
(lots of calls web service). can't manually change header in front of each such call because:
- they share same binding provider, , not thread-safe (i.e. user change header, user b change header, user call, user b call, , same header passed)
- this require modification on code.
what want add class serialises each request, add header @ serialisation time.
questions related not duplicates:
- java web service client, adding http headers - not use jax-ws binding, i.e. each call has made manually
- how add header soap request? - soap headers not http headers
as unique value aspect, can use jdk's uuid class create guid:
requestheaders.put("x-requestid", java.util.uuid.randomuuid().tostring());
as clarified thread safety concern, based on jax-ws specification (jsr-224) section 9.3 i'd suggest using jax-ws client handler handler spec identifies thread safe mechanism: messagecontext:
9.3.3 handler implementation considerations
handler instances may pooled jax-ws runtime system. instances of specific handler considered equivalent jax-ws runtime system , instance may chosen handle particular message. different handler instances may used handle each message of mep. different threads may used each handler in handler chain, each message in mep or combination of two. handlers should not rely on thread local state share information. handlers should instead use message context, see section 9.4.
you can write 1 central handler class , attach bindingprovider
's handler chain avoid changing places invoke service operation across application. can add handler handler chain programmatically or via @handlerchain annotation companion @webserviceref
this post describes using handler framework's messagecontext
set outbound http headers want. in case want set x-requestid
uuid
value discussed above.
Comments
Post a Comment