2017-08-01 24 views
0

让我明确的议程第一:共享的ApplicationContext与执行人

  1. 我有1000个请求数据。
  2. 我会读取所有1000个请求,并且我会将1000请求提交给执行者。
  3. 每个任务都会碰到soap web服务并获得响应。

问:

  1. 我有共同的,这将是相同的所有线程应用程序上下文。
  2. 在bean.xml文件中,我有我想用来创建肥皂请求的protoype bean。
  3. 如果我使用共享应用程序上下文并获取proptype bean,那么它会导致共享应用程序上下文变量的任何同步问题。

下面是示例代码:

import java.io.ObjectInputStream.GetField; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
class AppContext 
{ 
    ApplicationContext sharedContext = new ClassPathXmlApplicationContext("Beans.xml"); 

    public static ApplicationContext getAppContext() 
    { 
     if(sharedContext!=null) 
     return sharedContext; //will this cause any isseu while accessing by multiple threads 
    } 

} 

public class Testing { 





    public static void main(String args[]) 
    { 

     //here I tried to submit the task using ExecutorService and want to use the same application context 
     //can I pass the prototypeBean in all the task with out synchronization issue? 
     //because My appcontext is static so will it cause any issue while accessing my multiple threads 

     ExecutorService service=Executors.newFixedThreadPool(10); 
     service.submit(new LoopTaskA(AppContext.getAppContext().getBean("myProtoTypeBean"))); 
     service.submit(new LoopTaskA(AppContext.getAppContext().getBean("myProtoTypeBean"))); 
     service.submit(new LoopTaskA(AppContext.getAppContext().getBean("myProtoTypeBean"))); 
     service.submit(new LoopTaskA(AppContext.getAppContext().getBean("myProtoTypeBean"))); 
     service.shutdown(); 



    } 
} 

回答

0

这取决于你Runnables做。如果他们是无状态bean,并且不与其他Runnable s共享/修改相同的变量/引用,那么通常你就是安全的。 getBean()将返回一个新的实例,如果豆的范围是原型

请注意泳池的大小,确保设定合理的泳池大小(请参阅this)。此外,请确保您的工作线程提出Web服务请求已设置适当的超时。

+0

在我的Runnable中,我将使用prototype bean打击web服务。 我所有的任务都是从相同的应用程序上下文获取原型bean 所以我的同一个应用程序上下文会导致任何问题? 请帮忙 代码和我上面提到的一样 –

+0

通过不同的线程从相同的应用程序上下文获取工作bean没有问题。但是,如果你的工作者bean共享/修改相同的变量/引用,那么就存在一个问题。 – isah

+0

你是什么意思分享相同的变量,我想你的意思是共享同一个应用程序contex singleton bean? 所以如果它的原型bean服务于应用程序上下文,所以我没有任何问题 –