You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "Warren, Jared S" <js...@jcp.com> on 2013/05/21 22:31:46 UTC

Thread safe http request headers?

I need to include a unique value in an http header field for each request from my CXF client to the web service provider.

It's my understanding that I can do this when I initialize a port:

            ServiceControllerAdapter svc = adapter.getServiceControllerAdapter();
            Client client = ClientProxy.getClient(svc);
            client.getRequestContext().put("thread.local.request.context", "true");

Then I can use an object pool to pool the adapters, and do something like this each time I need one with unique headers:

            ServiceControllerAdapter port = (ServiceControllerAdapter) portPool.borrowObject();

            Map<String, List<String>> headers = new HashMap<String, List<String>>();
            List<String> mdcIdList = new LinkedList<String>();

            String mdcId = this.getUniqueId();
            mdcIdList.add(mdcId);
            headers.put("MDCID", mdcIdList);

            Client client = ClientProxy.getClient(port);
            client.getRequestContext().put(org.apache.cxf.message.Message.PROTOCOL_HEADERS, headers);

            response = port.process(request);

            client.getRequestContext().put(org.apache.cxf.message.Message.PROTOCOL_HEADERS, Collections.EMPTY_MAP);

            portPool.returnObject(port);


(Note: for reasons beyond the scope of this note, I'm stuck on Apache CXF 2.2.10.)

Can anyone validate that this should work?

Thanks!
jared





The information transmitted is intended only for the person or entity to
which it is addressed and may contain confidential and/or privileged 
material.  If the reader of this message is not the intended recipient,
you are hereby notified that your access is unauthorized, and any review,
dissemination, distribution or copying of this message including any 
attachments is strictly prohibited.  If you are not the intended
recipient, please contact the sender and delete the material from any
computer.


RE: Thread safe http request headers?

Posted by "Warren, Jared S" <js...@jcp.com>.
Thanks for the clarification.

:)


-----Original Message-----
From: Freeman Fang [mailto:freeman.fang@gmail.com] 
Sent: Tuesday, May 21, 2013 8:27 PM
To: users@cxf.apache.org
Subject: Re: Thread safe http request headers?

Hi,

As you already have port(the proxy)  pool, which means there won't be multiple thread access same proxy at the same time, you actually needn't set thread.local.request.context.

If there's only one proxy, you need set thread.local.request.context, using code like
((BindingProvider)port).getRequestContext().put("thread.local.request.context", "true");
to make it thread safe
-------------
Freeman(Yue) Fang

Red Hat, Inc. 
FuseSource is now part of Red Hat
Web: http://fusesource.com | http://www.redhat.com/
Twitter: freemanfang
Blog: http://freemanfang.blogspot.com
http://blog.sina.com.cn/u/1473905042
weibo: @Freeman小屋

www.camelone.org : The open source integration conference: 

On 2013-5-22, at 上午4:31, Warren, Jared S wrote:

> I need to include a unique value in an http header field for each request from my CXF client to the web service provider.
> 
> It's my understanding that I can do this when I initialize a port:
> 
>            ServiceControllerAdapter svc = adapter.getServiceControllerAdapter();
>            Client client = ClientProxy.getClient(svc);
>            client.getRequestContext().put("thread.local.request.context", "true");
> 
> Then I can use an object pool to pool the adapters, and do something like this each time I need one with unique headers:
> 
>            ServiceControllerAdapter port = (ServiceControllerAdapter) portPool.borrowObject();
> 
>            Map<String, List<String>> headers = new HashMap<String, List<String>>();
>            List<String> mdcIdList = new LinkedList<String>();
> 
>            String mdcId = this.getUniqueId();
>            mdcIdList.add(mdcId);
>            headers.put("MDCID", mdcIdList);
> 
>            Client client = ClientProxy.getClient(port);
>            client.getRequestContext().put(org.apache.cxf.message.Message.PROTOCOL_HEADERS, headers);
> 
>            response = port.process(request);
> 
>            client.getRequestContext().put(org.apache.cxf.message.Message.PROTOCOL_HEADERS, Collections.EMPTY_MAP);
> 
>            portPool.returnObject(port);
> 
> 
> (Note: for reasons beyond the scope of this note, I'm stuck on Apache CXF 2.2.10.)
> 
> Can anyone validate that this should work?
> 
> Thanks!
> jared
> 
> 
> 
> 
> 
> The information transmitted is intended only for the person or entity to
> which it is addressed and may contain confidential and/or privileged 
> material.  If the reader of this message is not the intended recipient,
> you are hereby notified that your access is unauthorized, and any review,
> dissemination, distribution or copying of this message including any 
> attachments is strictly prohibited.  If you are not the intended
> recipient, please contact the sender and delete the material from any
> computer.
> 


The information transmitted is intended only for the person or entity to
which it is addressed and may contain confidential and/or privileged 
material.  If the reader of this message is not the intended recipient,
you are hereby notified that your access is unauthorized, and any review,
dissemination, distribution or copying of this message including any 
attachments is strictly prohibited.  If you are not the intended
recipient, please contact the sender and delete the material from any
computer.


Re: Thread safe http request headers?

Posted by Freeman Fang <fr...@gmail.com>.
Hi,

As you already have port(the proxy)  pool, which means there won't be multiple thread access same proxy at the same time, you actually needn't set thread.local.request.context.

If there's only one proxy, you need set thread.local.request.context, using code like
((BindingProvider)port).getRequestContext().put("thread.local.request.context", "true");
to make it thread safe
-------------
Freeman(Yue) Fang

Red Hat, Inc. 
FuseSource is now part of Red Hat
Web: http://fusesource.com | http://www.redhat.com/
Twitter: freemanfang
Blog: http://freemanfang.blogspot.com
http://blog.sina.com.cn/u/1473905042
weibo: @Freeman小屋

www.camelone.org : The open source integration conference: 

On 2013-5-22, at 上午4:31, Warren, Jared S wrote:

> I need to include a unique value in an http header field for each request from my CXF client to the web service provider.
> 
> It's my understanding that I can do this when I initialize a port:
> 
>            ServiceControllerAdapter svc = adapter.getServiceControllerAdapter();
>            Client client = ClientProxy.getClient(svc);
>            client.getRequestContext().put("thread.local.request.context", "true");
> 
> Then I can use an object pool to pool the adapters, and do something like this each time I need one with unique headers:
> 
>            ServiceControllerAdapter port = (ServiceControllerAdapter) portPool.borrowObject();
> 
>            Map<String, List<String>> headers = new HashMap<String, List<String>>();
>            List<String> mdcIdList = new LinkedList<String>();
> 
>            String mdcId = this.getUniqueId();
>            mdcIdList.add(mdcId);
>            headers.put("MDCID", mdcIdList);
> 
>            Client client = ClientProxy.getClient(port);
>            client.getRequestContext().put(org.apache.cxf.message.Message.PROTOCOL_HEADERS, headers);
> 
>            response = port.process(request);
> 
>            client.getRequestContext().put(org.apache.cxf.message.Message.PROTOCOL_HEADERS, Collections.EMPTY_MAP);
> 
>            portPool.returnObject(port);
> 
> 
> (Note: for reasons beyond the scope of this note, I'm stuck on Apache CXF 2.2.10.)
> 
> Can anyone validate that this should work?
> 
> Thanks!
> jared
> 
> 
> 
> 
> 
> The information transmitted is intended only for the person or entity to
> which it is addressed and may contain confidential and/or privileged 
> material.  If the reader of this message is not the intended recipient,
> you are hereby notified that your access is unauthorized, and any review,
> dissemination, distribution or copying of this message including any 
> attachments is strictly prohibited.  If you are not the intended
> recipient, please contact the sender and delete the material from any
> computer.
>