Involking a Thread from a JSP Page!

Hi all,i just want invoke a thread(which will do synchronization of two database) from a JSP page on clicking of perticulat btn.how to call a thread from JSP page?thanks in advancePhoeniox
[223 byte] By [Phoenioxa] at [2007-11-27 8:15:51]
# 1
Just fscking don't. And JSPs don't have buttons to click. HTML files do. And the request should go to a servlet, which then triggers whichever business logic you need.You don't need a thread anywhere.
CeciNEstPasUnProgrammeura at 2007-7-12 20:00:41 > top of Java-index,Java Essentials,Java Programming...
# 2
I think you need the Super JSP design pattern.
cotton.ma at 2007-7-12 20:00:41 > top of Java-index,Java Essentials,Java Programming...
# 3
You can do that. Look here http://www.onjava.com/pub/a/onjava/2003/06/11/jsp_progressbars.html
curry_monstera at 2007-7-12 20:00:41 > top of Java-index,Java Essentials,Java Programming...
# 4

> Just fscking don't. And JSPs don't have buttons to

> click. HTML files do. And the request should go to a

> servlet, which then triggers whichever business logic

> you need.

>

It is understood that... JSPs don't have buttons to click. I am talking abt the page form which i wan to call the thread....

> You don't need a thread anywhere.

ya i know it is possible to do that without using thread...

since the operation will takes lot of time do the req operation... i want to do that within a thread....

Phoenioxa at 2007-7-12 20:00:41 > top of Java-index,Java Essentials,Java Programming...
# 5
> I think you need the Super JSP design pattern.I used to work for a company where this was the dominant design pattern. I left.
sabre150a at 2007-7-12 20:00:41 > top of Java-index,Java Essentials,Java Programming...
# 6
> You can do that. Look here> > http://www.onjava.com/pub/a/onjava/2003/06/11/jsp_prog> ressbars.htmlThanks a lot....Phoeniox
Phoenioxa at 2007-7-12 20:00:41 > top of Java-index,Java Essentials,Java Programming...
# 7

I suppose your case is like this:

- click button

- send request

- start thread

- return response

- still execute thread

- finish thread

So what happens if something goes wrong in the part after you returned the response? How will you notify your client?

Peetzorea at 2007-7-12 20:00:41 > top of Java-index,Java Essentials,Java Programming...
# 8

You probably need some kind of background job management system. I'd recomend using one thread for the whole web-app and queue synchronize requests on it. This prevents a lot of heavy database activities happening at once (which would almost certainly cost you overall throughput).

Use a ContextListener to create the queue (probably a LinkedBlockingQueue) and start the service thread. The contextDestroyed method interrupts the thread and shuts it down. The listener puts the queue object into the application context attributes so the servlet can find it.

Your servlet adds an item to the queue (an Object specifying synchronization details), and if the service thread isn't busy that wakes it up.

Alternatively you can use a ThreadPoolExecutor (which does essentially the same thing). It's probably OTT though, and prevents extra refinements, like openning a connection and keeping it open until there's nothing on the queue (use poll()).

You need to think though, about whether the user needs to be able to find out if his job is complete, and how you will manage that. Perhaps you want to place your queue entry in session and including an outcome status field in it.

ps

You may want to think about serializing the request list to a file when the web app closes, and reloading it when it opens.

Message was edited by:

malcolmmc

malcolmmca at 2007-7-12 20:00:41 > top of Java-index,Java Essentials,Java Programming...