This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSF and the fly likes Command Link, launch a long proccess Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Command Link, launch a long proccess" Watch "Command Link, launch a long proccess" New topic
Author

Command Link, launch a long proccess

Yeray Santana Borges
Ranch Hand

Joined: Mar 02, 2008
Posts: 46
Hi,

Is possible use a commandlink to launch an asynchronous process?. I have a web page that call an EJB. I need that the user press a button in the jsf web page, this page must call to my ejb and return the control to the user. My ejb have a very long process and the user can't wait until my ejb finalize.

JSF --> call ejb session bean ---> session bean running a long process
|
return the control to the user <---
immediately

I am using sun one 8.2 and tomahawk. Maybe there are a component (icefaces,trinidad ... ) that can i use to call to my facade and just returned to user control while my ejb is doing a long work.

Can you help me?

Thanks


SCJP 6 - SCWCD 5 - SCBCD 5
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Spawn a separate thread for the long running process. If you want user notification, consider ajax poll.
Yeray Santana Borges
Ranch Hand

Joined: Mar 02, 2008
Posts: 46
Hi Bauke,

Is possible open a new thread in my backing bean on a action listener method?

I thougth that open a thread in a j2ee envoroment is very dangerous ... i will try it. Maybe open it in a backed bean method no is a problem beacuse is still outside of ejb container.

thanks
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
You can just create a thread everywhere you want like as you create a class. In this case just create a class extending Thread or implementing Runnable and run it.

It is only dangerous if you code it the bad way.
Yeray Santana Borges
Ranch Hand

Joined: Mar 02, 2008
Posts: 46
Hi again Bauke,

My code its running perfect in this moment. Thanks for your suggestion, in the action listener i create a new thread with my runneable class, and works fine without problems.
Yeray Santana Borges
Ranch Hand

Joined: Mar 02, 2008
Posts: 46
Hi again, one question more about this post,

I have a ejb, that is called from a jsf page and this ejb (session bean) run a long process. Have the web container a time out for long process that take in the ejb container?.


I have this escenario for launch a long process in backgroung from a command link:

JSF command link ---> action listener in the session backed bean for that jsf page --> invoke an application backed bean that open a new thread and the control is returned to the user after myThread.starts()--> the new thread in run method invoke a session ejb bean that execute a long process --> my ejb finish the long process --> myThread finish too.

I dont know if is possible that the web container close the connection opened in myThread due to the process is very long. I think that because the thread is open in an application backed bean, there is no time out and if the proccess spend 2 hours, my thread is running 2 hours too.


Thanks
[ September 08, 2008: Message edited by: Yeray Santana Borges ]
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15950
    
  19

Originally posted by Bauke Scholtz:
You can just create a thread everywhere you want like as you create a class. In this case just create a class extending Thread or implementing Runnable and run it.


Not so. In fact, in EJBs in particular you are forbidden to spawn threads per the EJB spec.

In webapps, usually the best place to spin off a thread is from the init method of a dummy servlet (one that has no GET/POST handlers) and have the transactional parts of the system communicate with that thread. Almost any other approach ends up being one of those "Bad Ways".

You can't spin off a thread in the Get/Post handler of a servlet - or a JSF backing bean (usually) or a Struts Form or Action either. That's because you can't leave anything dangling after the HTTP response has been sent, and since the primary purpose of adding the overhead threading is to run something that takes too long to do inline, you'd be wasting time and effort.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
Consider Paul's rocket mass heater.
 
subject: Command Link, launch a long proccess
 
Similar Threads
What is the authenticated entity in a web app?
JNLP downloads from JSP page
New to JSF : Interaction between JSF and EJB's
can I inject ejb into managed bean?
Rudimentary question about JAAS authenticated entity