Themes in JSF?

I am new to JSF and I need to use Threads for Google Maps. I am using simple fonts for google maps, but I need to stream in the background to get lat and long time from the database and then graphically display the markers on the map.

+3


source to share


2 answers


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

A Java web application runs on an application server (like jBoss). The application server is responsible for Java thread control for you. For example, it will use a separate thread for each incoming web request. The application server creates a pool of threads and reuses these threads as it is somewhat expensive to create new ones. This is why you shouldn't create your own, especially if it's done for every web request, as it directly affects scalability.

To execute tasks asynchronously, you can use ejb @Asynchronous annotation (assuming the application 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
    }

}

      

If your application is not running in a Java EE container, check out this answer which perfectly describes some of the other options for asynchronous processing in web applications.

+2


source


JSF is completely unrelated to your problem. In this case, JSF will act as a simple HTML generator. Your specific problem is how to prepare data asynchronously and use it in your web application.

You can create a thread manually when the application is launched in a class that implements the interface ServletContextListener

, for example:

public class ApplicationListener implements ServletContextListener {
    ExecutorService executor;

    public ApplicationListener() {
        executor = Executors.newSingleThreadExecutor();
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        Runnable task = new Runnable() {
            @Override
            public void run() {
                //process the data here...
            }
        }
        executor.submit(task);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        executor.shutdownNow();
    }
}

      



Improve the design above to meet your requirements. Note that creating threads on the application server should only be done if you know what you are doing .

Another implementation would be to use a different processing application (let's call it Data Processor), which by default will run in a separate thread and environment. Then stream your web application using this Data Processor via cache or nosql application like EhCache, Infinispan or Hazelcast.

+1


source







All Articles