• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to use thread in bean in jsf

 
hemamalini nithyanandam
Ranch Hand
Posts: 53
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my web application that uses JSF, I have 100 records that is selected from the front end(using check box) and on clicking of a button , those selected 100 records needs to be initiated(it will create survey using RESTAPI call), which calls a method in bean that will make REST api call. At present each record processed in a sequence and each record takes 20 sec on a average. so if I choose nearly 100 records I am getting request time out.. I am planning to call the method in bean using thread.. can you please tell how can I do? Method in the bean is initiatereview that takes reference number as parameter.
 
Tim Holloway
Bartender
Posts: 18417
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never, never, Never spawn a thread in JSF backing beans . Or in any other J2EE request handling code, JSF or not. That is explictly banned by the J2EE standards definition. Failure to comply can have unpredictable consequences, up to and including random crashing of the entire server.

If you have a long-running task such that it cannot be handled in the time period acceptable for an HTTP request/response cycle, you should create a separate resource to create, manage, and run the threads that do that work and have the request processors communicate with that resource. Usually the best place to create this resource is in a ServletContextListener, since it's not part of the request processing subsystem and is therefore not subject to the ban on creating threads.

Note that it is just as much a sin for a request processor to wait on a thread as it is for it to spawn its own threads, so the communications between request processor (in the backing bean) and service thread should have a way to test for completion and while the process is still running, the request processor should do something like display a "please wait" message.
 
hemamalini nithyanandam
Ranch Hand
Posts: 53
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can i create a separate class to create and manage the thread and call that class which implements thread to process the job from the bean? can you please provide me an example how to do with ServletContextListener,?
 
Tim Holloway
Bartender
Posts: 18417
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create classes to manage and implement the long-running process, but they cannot be controlled by the JSF bean or any other other object that runs under the HTTP processing thread. They have to be under the control of the external "engine" thread created in the ServletContextListener.

I don't have any samples lying around at the moment, but here's the basic idea:

1. In the ServletContextListener's start method, instantiate the engine object and invoke its thread start method, which will cause the engine's run() method to in invoked in the normal way of threads.

2. The run() method can initialize whatever data is needed and then put the thread to sleep.

3. Also in the engine should be some sort of synchronized request method. This method would be what the JSF action invokes when it need the engine's services and would generally contain whatever run parameters the engine needs. The request method would store the parameters in the engine bean, wake up the engine thread, and return the the (JSF) caller.

4. The engine process can now run independently of the JSF request. At some later point(s) in time, additional JSF requests may invoke some sort of status, control, or cancel method(s) as needed to monitor and/or control the long-running process.

5. Once the process has completed, the thread goes back to sleep. A status method call from a JSF object will (presumably) return a status of "complete" to notify the caller of that fact, the JSF caller may invoke an engine method to obtain the results of the processing.

6. The ServletContextListener should also implement code in its shutdown method that invokes a method on the processing engine to tell its thread to terminate itself. If you don't terminate the thread, the webapp cannot shutdown, which in turn will usually hang the webapp server, which won't terminate its JVM until all threads are terminated.

That's the basics. More elaborate solutions allow maintaining a request queue (such as a synchronized List) that allows multiple users to stack up processing requests and may pair that with multiple instances of processing threads so that the requests don't have to be delayed waiting for previous requests to finish processing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!