Is it safe to manually start a new thread in Java EE?

前端 未结 3 1220
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 10:12

I could not find a definitive answer to whether it is safe to spawn threads within session-scoped JSF managed beans. The thread needs to call methods on the stateless EJB in

3条回答
  •  醉话见心
    2020-11-22 10:32

    I tried this and works great from my JSF managed bean

    ExecutorService executor = Executors.newFixedThreadPool(1);
    
    @EJB
    private IMaterialSvc materialSvc;
    
    private void updateMaterial(Material material, String status,  Location position) {
    
        executor.execute(new Runnable() {
            public void run() {
                synchronized (position) {
                    // TODO update material in audit? do we need materials in audit?
                    int index = position.getMaterials().indexOf(material);
                    Material m = materialSvc.getById(material.getId());
                    m.setStatus(status);
                    m = materialSvc.update(m);
                    if (index != -1) {
                        position.getMaterials().set(index, m);
                    }
    
                }
            }
        });
    
    }
    
    @PreDestroy
    public void destory() {
        executor.shutdown();
    }
    

提交回复
热议问题