Hi

On 30/11/12 18:48, jaybytez wrote:
We have a service (DefaultMessageListenerContainer) that listens to messages
and when it processes one, we make an HTTP Post to a third party that
requires us to create a stateful session for our interaction.  In the
DefaultMessageListenerContainer we have the min/max Consumers set to 5/10.
So when we send in about 20+ messages, we have what appears to be a
threading issues.  We use the WebClient to:

1) Setup maintain session
2) Call login
3) Receive a cookie
4) Call method
5) Parse response

What winds up happening is that it appears like another thread comes in and
wiping out the login/Cookie of the previous thread before the previous
thread calls the method it is supposed to, so our 3rd party service assumes
we haven't logged in yet.

Here is our code:

     public ResponseModel postSampleModel(RequestModel requestModel) {
         WebClient webClient = null;

         ResponseModel responseModel = null;
         try {
                    //Create webClient in code with threadsafe set to true
             webClient = WebClient.create(httpsUrl, jaxbProviders, true);
                        WebClient.getConfig(webClient).getRequestContext()
                     .put(org.apache.cxf.message.Message.MAINTAIN_SESSION,
Boolean.TRUE);
                                        
                        //create login xml and send request
             RequestModel loginRequest = createLogin();
             postRequest(webClient, loginRequest);
                        
             //send original after the login
             Response response = postRequest(webClient, requestModel);
             InputStream inputStream = (InputStream) response.getEntity();
                        
                        //return the unmarshalled response
             responseModel = ResponseModelUtil.unmarshal(ResponseModel.class,
inputStream);
         } finally {
             if (webClient != null) {
                 webClient.reset();
             }
         }
         return responseModel;
     }

     private Response postRequest(WebClient webClient, RequestModel
requestModel) {

                String xml = RequestModelUtil.marshal(RequestModel.class, 
requestModel);
                
         Response response = webClient.post(xml);

         if (response.getStatus() != 200) {
             LOGGER.error("Request to fastrieve was unsuccessful");
         }
         return response;

     }

In the above case WebClient is completely thread-safe even without setting a thread-safe flag because you create it on the current frame...


So when something like this sits in a DefaultMessageListenerContainer, if a
few instances are running at a time, will the WebClient produce a unique
cookie for each request running in parallel?  What other reason could be
causing this potential threading issue?

I'm not sure right now how org.apache.cxf.message.Message.MAINTAIN_SESSION is implemented, will need to check the code...

Can you experiment with a different approach please, remove a thread safe flag and work with cookies at the WebClient level, example, after a call, do webClient.getResponse().getMetadata().getFirst("Set-Cookie"), and then submit it back with "Cookie".

And if possible, create a simple test project which can help us investigate what might be going wrong with the current approach

Let us how it goes please

Thanks, Sergey



Thanks...jay



--
View this message in context: 
http://cxf.547215.n5.nabble.com/WebClient-multithreading-within-a-DefaultMessageListenerContainer-tp5719525.html
Sent from the cxf-user mailing list archive at Nabble.com.


--
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Reply via email to