JSF中的线程? [英] Threads in JSF?

查看:164
本文介绍了JSF中的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JSF的新手,我需要使用Threads进行谷歌地图。我正在为谷歌地图使用primefaces,但我需要在后台执行一个线程以从数据库获取lat和long,然后在地图中绘制标记。

I new in JSF, and I need use Threads for google maps. I am using primefaces for google maps, but I need excute a thread in background to get lat and long from data base and then graphic the markers in the map.

推荐答案

您的问题不是特定于JSF,而是特定于Web应用程序。那么,如何在Java Web应用程序中异步执行任务?绝对不是通过创建自己的线程。

Your question is not specific to JSF, but rather to web applications in general. So, how to perform tasks asynchronously in a Java web applications? Definitely NOT by creating your own threads.

Java Web应用程序在应用程序服务器(例如jBoss)中运行。应用程序服务器负责为您管理Java线程。例如,它将为每个进入的Web请求使用单独的线程。应用程序服务器创建一个线程池并重用这些线程,因为始终创建新线程有点昂贵。这就是为什么你不应该创建自己的,特别是如果它为每个Web请求完成,因为它将直接影响可伸缩性。

A Java web application runs in an application server (for example jBoss). It is the responsibility of the application server to manage Java threads for you. For instance, it will use a separate thread for each web request that comes in. The application server creates a pool of threads and reuses those threads since it is somewhat expensive to create new ones all the time. That's why you should not create your own, especially if it's done for every web request since it will directly impact scalability.

为了异步执行任务,你可以使用ejb @Asynchronous注释(假设应用程序在Java EE容器中运行,如jBoss,但不是Tomcat)。

In order to execute tasks asynchronously, you can use the ejb @Asynchronous annotation (assuming the app is running in a Java EE container like jBoss, but not Tomcat).

import javax.ejb.Singleton;

@Singleton
public class AsyncBean {

    @Asynchronous
    public void doSomethingAsynchronously() {
       // when this EJB is injected somewhere, and this method is called, it will return to the caller immediately and its logic will run in the background
    }

}

如果应用程序未在Java EE容器中运行,请查看这个答案很好地为网络应用程序中的异步处理提供了一些其他选项。

If the app is not running in a Java EE container, take a look at this answer which nicely lays out some other options for async processing in web apps.

这篇关于JSF中的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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