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

查看:15
本文介绍了在 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 中生成线程不一定是一种黑客攻击,只要它能完成您想要的工作.但是需要非常小心地自行生成线程.代码不应这样编写,例如单个用户可以为每个会话生成无限数量的线程和/或即使在会话被破坏后线程仍继续运行.它迟早会毁掉你的申请.

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-friendly),您还可以使用 Java 1.5 的 Util Concurrent ExecutorServiceScheduledExecutorService 为此.

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

}

另见:

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