在Java EE中手动启动新线程是否安全? [英] Is it safe to manually start a new thread in Java EE?

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

问题描述

对于在会话范围内的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中生成线程不一定是hack,只要它能够完成您想要的工作即可.但是需要非常小心地自行生成线程.不应以这样的方式编写代码:单个用户可以例如在每个会话中产生无限数量的线程,并且/或者即使在会话被销毁后也可以继续运行线程.迟早会炸毁您的应用程序.

Introduction

Spawning threads from within a session scoped managed bean is not necessarily a hack as long as it does the job you want. But spawning threads at its own needs to be done with extreme care. The code should not be written that way that a single user can for example spawn an unlimited amount of threads per session and/or that the threads continue running even after the session get destroyed. It would blow up your application sooner or later.

需要以这样的方式编写代码,以确保例如每个用户在每个会话中都不会产生多个后台线程,并且确保在会话被销毁时该线程将被中断.对于会话中的多个任务,您需要将任务排队. 同样,所有这些线程最好都应由公共线程池提供服务,以便您可以在应用程序级别上限制产生的线程总数.

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.

因此,管理线程是一项非常微妙的任务.这就是为什么您最好使用内置功能,而不是与new Thread()和朋友自己种植自己的.普通的Java EE应用程序服务器提供了一个容器管理的线程池,您可以通过它使用EJB的 @Asynchronous @Schedule .要独立于容器(阅读:对Tomcat友好),您还可以使用Java 1.5的Util Concurrent

Managing threads is thus a very delicate task. That's why you'd better use the built-in facilities rather than homegrowing your own with new Thread() and friends. 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 (read: Tomcat-friendly), you can also use the Java 1.5's Util Concurrent ExecutorService and ScheduledExecutorService for this.

下面的示例假定Java EE 6+具有EJB.

Below examples assume Java EE 6+ with EJB.

@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() {
        // ...
    }

}

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

@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);
    }

}

如果您使用的是JSF实用程序库 OmniFaces ,则如果对托管bean进行注释,则可以更快地完成此操作与 @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)
    }

}

在后台连续更新应用程序范围模型

@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;
    }

}

另请参见:

  • 在使用计时器的JSF托管Bean,用于计划任务
  • See also:

    • Spawning threads in a JSF managed bean for scheduled tasks using a timer
    • 这篇关于在Java EE中手动启动新线程是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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