有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

jakarta ee在哪里可以配置Java ee 6中@Asynchronous调用后面的线程池?

我最近了解到,只需添加@Asynchronous注释,就可以轻松地生成any session bean method Asynchronous

例如

@Asynchronous
public Future<String> processPayment(Order order) throws PaymentException {
    ... 
}

我知道JavaEE7添加了Concurrency Utilities,但是在JavaEE6中,@Asyncronous方法的线程池配置在哪里?有没有办法设置超时?它是固定线程池吗?缓存的一个?优先考虑什么?它在容器中的某个地方是可配置的吗


共 (2) 个答案

  1. # 1 楼答案

    尽管我发现的解决方案仅在JavaEE7/GlassFish 4.1上进行了测试,但我认为它应该适用于GlassFish 3。我也是

    java上有一个JIRAentry。net中列出了不同的设置。由于Oracle将停止该网站,我将在此处引用相关帖子(添加格式):

    The configuration is in domain.xml, for example,

    <ejb-container>
      <property name="thread-core-pool-size" value="10"></property>
      <property name="thread-max-pool-size" value="100"></property>
      <property name="thread-queue-capacity" value="20"></property>
      <property name="thread-keep-alive-seconds" value="600"</property>
      <property name="allow-core-thread-timeout" value="false"></property>
      <property name="prestart-all-core-threads" value="false"></property>
    </ejb-container>
    

    All of the above properties are optional. Their default values:

    thread-core-pool-size: 16
    thread-max-pool-size: 32
    thread-queue-capacity: Integer.MAX_VALUE
    thread-keep-alive-seconds: 60
    allow-core-thread-timeout: false
    prestart-all-core-threads: false
    

    通过该线程,我还发现了一个blog post,它解释了核心池和最大池大小是如何工作的。引用要点:

    In the past SUN declared correctly: "That is exactly how it is supposed to behave. First the threads grow to coreSize, then the queue is used, then if the queue fills up then the number of threads expands from coreSize to maxSize. Hence if you use an unbounded queue the last part never happens. This is all described in the documentation. If you want an unbounded queue but more threads then increase the core size. Otherwise consider whether a bounded queue is more suitable to your needs."

  2. # 2 楼答案

    我认为可以通过从带注释的@timeout方法调用Future.cancel(boolean)来实现超时。需要保留对异步方法返回的未来的引用,单例ejb可以用于此目的

    @Stateless
    public class AsyncEjb {
    
        @Resource
        private SessionContext sessionContext;
    
        @Asynchronous
        public Future<String> asyncMethod() {
    
            ...
            //Check if canceled by timer
            if(sessionContext.wasCancelCalled()) {
                ...
            }
            ...
    
        }
    }
    
    @Singleton
    public class SingletonEjb {
        @EJB
        AsyncEjb asyncEjb;
    
        Future<String> theFuture;
    
        public void asyncMethod() {
    
            theFuture = asyncEjb.asyncMethod();
    
            //Create programatic timer
            long duration = 6000;
            Timer timer =
            timerService.createSingleActionTimer(duration, new TimerConfig());
    
        }
    
        //Method invoked when timer runs out
        @Timeout
        public void timeout(Timer timer) {
            theFuture.cancel(true);
        }
    }
    

    编辑(以下新增内容):

    在glassfish中,您可以通过在管理控制台中设置以下属性来配置ejb池

    • 初始和最小池大小
    • 最大池大小
    • 池大小调整数量
    • 池空闲超时

    Tuning the EJB Pool