You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Landslide <xi...@landslide.com> on 2008/03/01 21:52:43 UTC

Is it thread safe to use an instance variable @WebServiceProvider?

Is it thread safe to use an instance variable "quote" as below?

import javax.xml.ws.Provider;
import javax.xml.ws.Service;
import javax.xml.ws.ServiceMode;
import javax.xml.ws.WebServiceProvider;

@WebServiceProvider(portName="stockQuoteReporterPort"
serviceName="stockQuoteReporter")
@ServiceMode(value="Service.Mode.MESSAGE")
public class  StockQuoteReporterProvider implements Provider<SOAPMessage>
{
private Quote quote;

public StockQuoteReporterProvider()
  {
       quote = new Quote();
  }

public SOAPMessage invoke(SOAPMessage request)
  {
  quote.setIntProperty(123);

  SOAPBody requestBody = request.getSOAPBody();
  if(requestBody.getElementName.getLocalName.equals("getStockPrice"))
    {
    MessageFactory mf = MessageFactory.newInstance();
    SOAPFactory sf = SOAPFactory.newInstance();

    SOAPMessage response = mf.createMessage();
    SOAPBody respBody = response.getSOAPBody();
    Name bodyName = sf.createName("getStockPriceResponse");
    respBody.addBodyElement(bodyName);
    SOAPElement respContent = respBody.addChildElement("price");
    respContent.setValue("123.00");
    response.saveChanges();
    return response;
    }
    ...
  }
}
-- 
View this message in context: http://www.nabble.com/Is-it-thread-safe-to-use-an-instance-variable-%40WebServiceProvider--tp15780744p15780744.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Is it thread safe to use an instance variable @WebServiceProvider?

Posted by Willem Jiang <wi...@gmail.com>.
No , the instance variable "quote" is not thread safe.
If you still need the quote object , I suggest you to make it to be a 
thread local object.
Here is the code snippet:

public class  StockQuoteReporterProvider implements Provider<SOAPMessage> {

  private ThreadLocal<Quote> quote = new ThreadLocal<Quote>();

  private Quote getQuote() {
    if (quote.get() == null) {
       quote.set(new Quote());
    }
    return quote.get();
  }

  
  public SOAPMessage invoke(SOAPMessage request){
    getQuote().setIntProperty(123);

    SOAPBody requestBody = request.getSOAPBody();

    ...
  }
}

Willem


Landslide wrote:
> Is it thread safe to use an instance variable "quote" as below?
>
> import javax.xml.ws.Provider;
> import javax.xml.ws.Service;
> import javax.xml.ws.ServiceMode;
> import javax.xml.ws.WebServiceProvider;
>
> @WebServiceProvider(portName="stockQuoteReporterPort"
> serviceName="stockQuoteReporter")
> @ServiceMode(value="Service.Mode.MESSAGE")
> public class  StockQuoteReporterProvider implements Provider<SOAPMessage>
> {
> private Quote quote;
>
> public StockQuoteReporterProvider()
>   {
>        quote = new Quote();
>   }
>
> public SOAPMessage invoke(SOAPMessage request)
>   {
>   quote.setIntProperty(123);
>
>   SOAPBody requestBody = request.getSOAPBody();
>   if(requestBody.getElementName.getLocalName.equals("getStockPrice"))
>     {
>     MessageFactory mf = MessageFactory.newInstance();
>     SOAPFactory sf = SOAPFactory.newInstance();
>
>     SOAPMessage response = mf.createMessage();
>     SOAPBody respBody = response.getSOAPBody();
>     Name bodyName = sf.createName("getStockPriceResponse");
>     respBody.addBodyElement(bodyName);
>     SOAPElement respContent = respBody.addChildElement("price");
>     respContent.setValue("123.00");
>     response.saveChanges();
>     return response;
>     }
>     ...
>   }
> }
>