Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 弹簧&x2B;jaxws:有没有比静态变量更好的方法来保持全局状态?_Spring_Cxf_Jax Ws - Fatal编程技术网

Spring 弹簧&x2B;jaxws:有没有比静态变量更好的方法来保持全局状态?

Spring 弹簧&x2B;jaxws:有没有比静态变量更好的方法来保持全局状态?,spring,cxf,jax-ws,Spring,Cxf,Jax Ws,我有一个webservice(cxf),它在每次调用耗时的方法时创建一个新线程,将ID返回给客户端,作为令牌传递给另一个web方法以检查特定线程的执行状态: public String doWork(){ String id = /***randomnly generates an ID****/ executor.execute(**some runnable**); // need to save the guy above return id; } publ

我有一个webservice(cxf),它在每次调用耗时的方法时创建一个新线程,将ID返回给客户端,作为令牌传递给另一个web方法以检查特定线程的执行状态:

public String doWork(){
    String id = /***randomnly generates an ID****/
    executor.execute(**some runnable**);

   // need to save the guy above
   return id;
}

public String checkStatus(String id){
    /* Here I would basically access to an hashmap
       of tasks, pick the one identified by that ID
       and check its state */
}
正如您在上面看到的,我的问题是保留对任务的引用,以便跟踪其执行情况。我的想法如下:

@Webservice
public class MyService{
    private static HashMap<String, Future> futures = new HashMap<String,Future>();
@Webservice
公共类MyService{
私有静态HashMap futures=newhashmap();

但是有更好的方法吗?此外,这种选择可能有哪些缺点?

另一种存储全局状态的方法是使用数据库。由于不知道应用程序的所有细节,唯一想到的是静态变量可能会因其使用方式而出现线程安全问题。

有助于了解javaeeservlet的底层编程模型的一些知识,JAX-WS等都是基于该模型构建的

要在会话期间保留与客户端关联的信息,可以在方法体中获取会话并在其中存储信息:

Message message = PhaseInterceptorChain.getCurrentMessage();
HttpServletRequest request = (HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST);
HttpSession  session = request.getSession(true);
session.setAttribute("id", id);
在随后的方法调用中,您可以以相同的方式检索会话数据


一个非常基本的(古老的)教程:。

不,我不想使用DB…Jesus servlets确实有一个应用程序上下文,我可以在其中保存数据…jax ws或spring不允许这样做吗?