配置 ManagedExecutorService 实例

可配置 ManagedExecutorService 实例以使用所指定线程上下文来运行异步任务。Java™ EE 应用程序最好避免直接管理它们自己的线程;因此,ManagedExecutorService 扩展 JSE ExecutorService 来提供一种方法以在应用程序服务器环境内启动异步任务。还可配置 ManagedExecutorService 以将与 Java EE 应用程序相关的各种线程上下文传播至异步任务的线程。

关于此任务

ManagedExecutorService 是在 <concurrent-1.0> 功能部件下提供的,并且在 server.xml 文件中启用,如下所示:
<featureManager>
		<feature>concurrent-1.0</feature>
</featureManager>

ManagedExecutorService 所执行任务的线程的上下文传播由上下文服务管理。服务器创建上下文服务的缺省实例 (DefaultContextService),并将其配置为至少传播 classloaderContextjeeMetadataContextsecurityContext。如果已创建 ManagedExecutorService 但未引用特定上下文服务实例或直接在其中配置上下文服务实例,那么会使用此缺省上下文服务实例。有关更多信息,请参阅“配置线程上下文服务实例”主题。

缺省受管执行程序实例 (DefaultManagedExecutorService) 以 java:comp/DefaultManagedExecutorService 形式提供,并使用缺省上下文服务实例来捕获和传播线程上下文。

注意: Alpha 当前存在以下限制:调用 Future.get 前,所提交任务可能不会运行。

过程

server.xml 文件中的示例配置:

示例

受管执行程序服务实例可插入到应用程序组件中(通过使用 @Resource),或使用资源环境引用 (resource-env-ref) 进行查找。不管如何获取实例,都可按可互换方式将其用作 javax.enterprise.concurrent.ManagedExecutorService 或其 java.util.concurrent.ExecutorSerivce 超类。

  • 使用 @Resource 以插入为 java.util.concurrent.ExecutorService 的示例:
    @Resource(lookup="concurrent/execSvc1")
    ExecutorService execSvc1;
    
    ...
    
    // submit task to run 
    Future<Integer> future1 = execSvc1.submit(new Callable<Integer>() { 
    		public Integer call() throws Exception { 
    	  	  // java:comp lookup is possible because <jeeMetadataContext> is configured 
    				DataSource ds = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/ds1");
    				... make updates to the database 
    				return updateCount; 
    	} 
    });  
    Future<Integer> future2 = execSvc1.submit(anotherTaskThatUpdatesADatabase);  
    
    numUpdatesCompleted = future1.get() + future2.get();
  • 使用 @Resource 以插入为 javax.enterprise.concurrent.ManagedExecutorService 的示例:
    @Resource(lookup="concurrent/execSvc1")
    ManagedExecutorService execSvc1;
    
    ...
    
    // submit task to run 
    Future<Integer> future1 = execSvc1.submit(new Callable<Integer>() { 
    		public Integer call() throws Exception { 
    	  	  // java:comp lookup is possible because <jeeMetadataContext> is configured 
    				DataSource ds = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/ds1");
    				... make updates to the database 
    				return updateCount; 
    	} 
    });  
    Future<Integer> future2 = execSvc1.submit(anotherTaskThatUpdatesADatabase);  
    
    numUpdatesCompleted = future1.get() + future2.get();
  • web.xml 文件中的 java.util.concurrent.ExecutorService<resource-env-ref> 示例:
    <resource-env-ref>
    		<resource-env-ref-name>concurrent/execSvc2</resource-env-ref-name>
    		<resource-env-ref-type>java.util.concurrent.ExecutorService</resource-env-ref-type>
    </resource-env-ref>
  • web.xml 文件中的 javax.enterprise.concurrent.ManagedExecutorService<resource-env-ref> 示例:
    <resource-env-ref>
    		<resource-env-ref-name>concurrent/execSvc2</resource-env-ref-name>
    		<resource-env-ref-type>javax.enterprise.concurrent.ManagedExecutorService</resource-env-ref-type>
    </resource-env-ref>
  • 使用资源环境引用的示例查询:
    ExecutorService execSvc2 = 
        (ExecutorService) new InitialContext().lookup("java:comp/env/concurrent/execSvc2");
    
    futures = execSvc2.invokeAll(Arrays.asList(task1, task2, task3));
  • 使用资源环境引用并强制转型为 ManagedExecutorService 的查找示例:
    ManagedExecutorService execSvc2 = 
        (ManagedExecutorService) new InitialContext().lookup("java:comp/env/concurrent/execSvc2");
    
    futures = execSvc2.invokeAll(Arrays.asList(task1, task2, task3));

用于指示主题类型的图标 任务主题

信息中心的条款和条件 | 反馈


时间戳记图标 最近一次更新时间: Wednesday, 2 September 2015
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=phil&product=was-libcore-mp&topic=twlp_config_managedexecutor
文件名:twlp_config_managedexecutor.html