java – Glassfish 4 – 使用并发API创建托管线程

我正在尝试使用新的Concurrency API注入
ManagedThreadFactory并按照
the Oracle tutorial使用它.

这是我正在谈论的一个例子:

@Singleton
@Startup
public class Demo {
    @Resource(name="concurrent/__DefaultManagedThreadFactory") ManagedThreadFactory threadFactory;

    @PostConstruct
    public void startup() {
        threadFactory.newThread(
            new Runnable() {
                @Override
                public void run() {
                    System.out.println("Do something.");
                }
            }
        ).start();
    }
}

我正在使用Glassfish插件在Eclipse中开发.当我在进行更改后重新发布时,我总是在服务器日志中获得此行.每次调用start()时都会出现一次:

SEVERE: java.lang.IllegalStateException: Module (my application) is disabled

它实际上并没有抛出IllegalStateException,只是报告在Glassfish中抛出(并捕获)了一个.应用程序正常部署,但没有任何线程启动.如果我随后重新发布并第二次,“错误”消失,线程按预期启动.

当我尝试将应用程序部署到“真正的”Glassfish设置(没有Eclipse)时,它总是报告成功部署,并且日志不包含“错误”.但它仍然没有启动线程(即使重复部署).

我正确使用并发API吗?可能是配置问题?对于记录,如果我使用ManagedExcecutorService,我会得到相同的行为.

为了记录,几个月前在这里问了这个问题:Can I start a ManagedThread in a Singleton Enterprise Java Bean?,但它没有得到真正的回答,我没有声誉做任何事情,只是再问一遍.抱歉!

更新:This answer Per-Axel Felth作品.谢谢!我对该解决方案进行了一些重构,试图将解决方法代码与原始应用程序逻辑隔离开来:

@Singleton
@Startup
public class Demo {

    @Resource(name="java:comp/DefaultManagedThreadFactory") ManagedThreadFactory threadFactory;
    @EJB private ConcurrencyInitializer concurrencyInitializer;
    @EJB private Demo self;

    @PostConstruct
    public void startup() {
        self.startThread();
    }

    @Asynchronous
    public void startThread() {
        //This line applies the workaround
        concurrencyInitializer.init();

        //Everything beyond this point is my original application logic
        threadFactory.newThread(
            new Runnable() {
                @Override
                public void run() {
                    System.out.println("Do something.");
                }
            }
        ).start();            
    }

}
/**
 * A utility class used to get around a bug in Glassfish that allows
 * Concurrency resources (ManagedThreadFactory, ManagedExecutorService, etc)
 * to be injected before they are ready to be used. 
 * 
 * Derived from solution by Per-Axel Felth in: https://stackoverflow.com/questions/23900826/glassfish-4-using-concurrency-api-to-create-managed-threads
 */
@Singleton
public class ConcurrencyInitializer {
    /**
     * The number of milliseconds to wait before try to 
     */
    public static final long RETRY_DELAY = 500L;

    /**
     * The maximum number of concurrency attempts to make before failing
     */
    public static final int MAX_RETRIES = 20;

    /**
     * Repeatedly attempts to submit a Runnable task to an injected ManagedExecutorService
     * to trigger the readying of the Concurrency resources.
     * 
     * @return true if successful (Concurrency resources are now ready for use),
     *         false if timed out instead
     */
    public boolean init() {
        final AtomicBoolean done = new AtomicBoolean(false);
        int i = 0;

        try {
            while (!done.get() && i++ < MAX_RETRIES) {
                executorService.submit(new Runnable() {
                    @Override
                    public void run() {
                        done.set(true);
                    }
                });
                Thread.sleep(RETRY_DELAY);
            }
        } catch(InterruptedException e) {
            //Do nothing.
        } 

        return done.get();
    }
}

最佳答案 它与Glassfish错误有关.我不久前遇到了同样的错误并构建了一个解决方法.事实是,线程工厂注入正常,但如果你“太早”使用它,你最终会得到一个IllegalStateException.

我的解决方法代码如下所示.它使用一个注入的执行器服务来检测何时加载app并且并发工具可用,然后在方法init中执行实际的启动逻辑.

@Singleton
@Startup
public class Demo {

    @Resource(name = "concurrent/__DefaultManagedThreadFactory")
    ManagedThreadFactory threadFactory;
    @Resource
    ManagedExecutorService executorService;
    @EJB
    Demo me;

    @PostConstruct
    public void startup() {

        me.waitAndInitialize();
    }

    @Asynchronous
    public Future<?> waitAndInitialize() {
        try {
            final AtomicInteger done = new AtomicInteger(0);
            int i = 0;

            while (done.intValue() == 0 && i < 20) {
                System.out.println("Is executor service up?");

                i++;

                executorService.submit(
                        new Runnable() {

                            @Override
                            public void run() {
                                int incrementAndGet = done.incrementAndGet();
                                System.out.println("Run by executorservice");
                            }
                        });
                Thread.sleep(500);
            }

            if (done.intValue() == 0) {
                Logger.getAnonymousLogger().severe("Waited a long time for the ExecutorService do become ready, but it never did. Will not initialize!");
            } else {
                init();
            }
        } catch (Exception e) {
            Logger.getAnonymousLogger().log(Level.SEVERE, "Exception in waitAndInitialize: " + e.getMessage(), e);
        }

        return new AsyncResult<>(null);
    }

    private void init() {
        threadFactory.newThread(
                new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("Do something.");
                    }
                }
        ).start();
    }
}
点赞