在JSF托管bean中启动新线程是否安全? [英] Is it safe to start a new thread in a JSF managed bean?

查看:313
本文介绍了在JSF托管bean中启动新线程是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到一个明确的答案,是否在会话范围的JSF管理bean中产生线程是安全的。线程需要调用无状态EJB实例上的方法(这是依赖注入到托管bean)。

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 instance (that was dependency-injected to the managed bean).

后台是我们有一个报表需要一个长时间生成。这导致HTTP请求超时,由于服务器设置,我们不能更改。所以想法是开始一个新的线程,让它生成报告并临时存储它。同时,JSF页面显示一个进度条,轮询托管bean直到生成完成,然后发出第二个请求以下载存储的报告。

The background is that we have a report that takes a long time to generate. This caused the HTTP request to time-out due to server settings we can't change. So the idea is to start a new thread and let it generate the report and to temporarily store it. In the meantime the JSF page shows a progress bar, polls the managed bean till the generation is complete and then makes a second request to download the stored report. This seems to work, but I would like to be sure what I'm doing is not a hack.

推荐答案

简介

从一个会话作用域托管bean中生成线程不一定是一个黑客,只要它做你想要的工作。但是生成线程本身需要非常小心。代码不应以这样的方式编写:单个用户可以例如每会话产生无限量的线程和/或即使在会话被销毁之后线程仍然运行。

The code needs to be written that way that you can ensure that an user can for example never spawn more than one background thread per session and that the thread is guaranteed to get interrupted whenever the session get destroyed. For multiple tasks within a session you need to queue the tasks.

代码需要以这样的方式编写,您可以确保用户可以永远生成不止一个后台线程每个会话,并且线程被保证在会话被销毁时被中断。对于会话中的多个任务,您需要将任务排队。

Also, all those threads should preferably be served by a common thread pool so that you can put a limit on the total amount of spawned threads at application level. The average Java EE application server offers a container managed thread pool which you can utilize via among others EJB's @Asynchronous and @Schedule. To be container independent, you can also use the Java 1.5's Util Concurrent ExecutorService and ScheduledExecutorService for this.

此外,所有这些线程最好由一个公共线程池提供服务,这样您可以对应用程序级别上生成的线程的总量设置限制。平均的Java EE应用程序服务器提供了一个容器管理的线程池,您可以通过EJB的 @Asynchronous @Schedule 。要成为容器独立的,您还可以使用Java 1.5的Util Concurrent ExecutorService ScheduledExecutorService

Below examples assume Java EE 6+ with EJB.

示例假设带有EJB的Java EE 6+。

@Named @RequestScoped // Or @ViewScoped public class Bean { @EJB private SomeService someService; public void submit() { someService.asyncTask(); // ... (this code will immediately continue without waiting) } }





@Stateless
public class SomeService {

    @Asynchronous
    public void asyncTask() {
        // ...
    }

}



在页面加载时异步获取模型



Asynchronously fetch the model on page load

@Named
@RequestScoped // Or @ViewScoped
public class Bean {

    private Future<List<Entity>> asyncEntities;

    @EJB
    private EntityService entityService;

    @PostConstruct
    public void init() {
        asyncEntities = entityService.asyncList();
        // ... (this code will immediately continue without waiting)
    }

    public List<Entity> getEntities() {
        try {
            return asyncEntities.get();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new FacesException(e);
        } catch (ExecutionException e) {
            throw new FacesException(e);
        }
    }
}





@Stateless
public class EntityService {

    @PersistenceContext
    private EntityManager entityManager;

    @Asynchronous
    public Future<List<Entity>> asyncList() {
        List<Entity> entities = entityManager
            .createQuery("SELECT e FROM Entity e", Entity.class)
            .getResultList();
        return new AsyncResult<>(entities);
    }

}

实用程序库 OmniFaces ,如果您使用 @Eager

In case you're using JSF utility library OmniFaces, this could be done even faster if you annotate the managed bean with @Eager.

@Singleton
public class BackgroundJobManager {

    @Schedule(hour="0", minute="0", second="0", persistent=false)
    public void someDailyJob() {
        // ... (runs every start of day)
    }

    @Schedule(hour="*/1", minute="0", second="0", persistent=false)
    public void someHourlyJob() {
        // ... (runs every hour of day)
    }

    @Schedule(hour="*", minute="*/15", second="0", persistent=false)
    public void someQuarterlyJob() {
        // ... (runs every 15th minute of hour)
    }

    @Schedule(hour="*", minute="*", second="*/30", persistent=false)
    public void someHalfminutelyJob() {
        // ... (runs every 30th second of minute)
    }

}



/ h2>

Continuously update application wide model in background

@Named
@RequestScoped // Or @ViewScoped
public class Bean {

    @EJB
    private SomeTop100Manager someTop100Manager;

    public List<Some> getSomeTop100() {
        return someTop100Manager.list();
    }

}





@Singleton
@ConcurrencyManagement(BEAN)
public class SomeTop100Manager {

    @PersistenceContext
    private EntityManager entityManager;

    private List<Some> top100;

    @PostConstruct
    @Schedule(hour="*", minute="*/1", second="0", persistent=false)
    public void load() {
        top100 = entityManager
            .createNamedQuery("Some.top100", Some.class)
            .getResultList();
    }

    public List<Some> list() {
        return top100;
    }

}





查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆