You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Geoff Callender <ge...@attglobal.net> on 2004/09/12 01:25:42 UTC

How to run threads off ApplicationServlet

Geoff Callender <geocal <at> attglobal.net> writes:

> Awesome, Eric.  
> 
> Looking at the API it looks like the ApplicationServlet
> can get to the Global object with getEngine().getGlobal() ,
> so it should be a piece of cake for it to publish its results
> where other web pages can poll them.
> 
> Many thanks,
> 
> Geoff


For anyone who needs to do it, here's what I found about how to run 
threads off Tapestry's single servlet, so that they start up and shut 
down with your web server.

1. Make sure your thread class has run() and destroy() methods.  Eg.

public class RepeatingTask extends Thread {

	public void run() {
		while (true) {
			System.out.println("Awake.  Going back to sleep.");
			try {
				Thread.sleep(10000);
			}
			catch (InterruptedException e) {
				throw new RuntimeException(e);
			}
		}
	}
	
	public void destroy() {
	}
}

The destroy() method can be empty, but without it you'll get exceptions 
later.

2. Subclass org.apache.tapestry.ApplicationServlet.  
In the init(ServletConfig config) method, instantiate and run your 
threads.  In the destroy() method, destroy your threads. 
Eg. 

public class MyApplicationServlet extends ApplicationServlet {
	private RepeatingTask _repeatingTask;

	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		_repeatingTask = new RepeatingTask();
		_repeatingTask.start();
	}
	
	public void destroy() {
		_repeatingTask.destroy();
		super.destroy();
	}
}

3. Add a line to your ".application" file to tell Tapestry to use your new 
servlet instead of the default one.  Eg. 

	<property name="org.apache.tapestry.specification-path" 
value="myPackage.MyApplicationServlet"/>

4. Replace the line in your web.xml file that says which servlet to load up. 
Eg.

Replace this:
    <servlet-class>org.apache.tapestry.ApplicationServlet</servlet-class>
With this:
    <servlet-class>myPackage.MyApplicationServlet</servlet-class>

That's all there is to it.

What I still couldn't figure out, though, was how to get the global class 
from inside the thread.  Even if the thread has a reference back to the 
servlet, it doesn't help because you can't use 
getEngine(RequestContext context).getGlobal() without a context.



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: How to run threads off ApplicationServlet

Posted by Benjamin Tomasini <bt...@neteverything.com>.
That sounds like a good solution.

I have done async processes outside of a servlet container and have had
good luck with it, too.  It can be nice and modular.

Have fun!

On Sun, 2004-09-12 at 07:24, Geoff Callender wrote:
> Benjamin Tomasini <btomasini <at> neteverything.com> writes:
> 
> > 
> > What happens if your servlet container creates two instances of the
> > servlet?  What if you have a failover servlet container on the same box
> > or on multiple boxes?  Are you intending to have multiple instances of
> > your RepeatingTask running?
> > 
> 
> Benjamin,
> 
> They are all good questions.  These and other issues have ended up 
> pushing me to run the threads in a process outside of the web server, 
> so none of those issues come up.  
> 
> Perhaps Quartz has a role to play in this, but I'm starting to think they'll 
> need to respond to some commands, so I may need to build a custom 
> GUI to control them, perhaps via simple sockets that the tasks read 
> whenever they get the chance.
> 
> As for how they publish their findings, since they can't use the beautifully 
> lightweight solution of saving them as an object in Tapestry's global 
> object, I've resigned myself to using the database.
> 
> Thanks for your input on all of this.  Got more?
> 
> Geoff
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
-- 
Benjamin Tomasini
NetEverything, Inc.
1-877-270-1391


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: How to run threads off ApplicationServlet

Posted by Geoff Callender <ge...@attglobal.net>.
Benjamin Tomasini <btomasini <at> neteverything.com> writes:

> 
> What happens if your servlet container creates two instances of the
> servlet?  What if you have a failover servlet container on the same box
> or on multiple boxes?  Are you intending to have multiple instances of
> your RepeatingTask running?
> 

Benjamin,

They are all good questions.  These and other issues have ended up 
pushing me to run the threads in a process outside of the web server, 
so none of those issues come up.  

Perhaps Quartz has a role to play in this, but I'm starting to think they'll 
need to respond to some commands, so I may need to build a custom 
GUI to control them, perhaps via simple sockets that the tasks read 
whenever they get the chance.

As for how they publish their findings, since they can't use the beautifully 
lightweight solution of saving them as an object in Tapestry's global 
object, I've resigned myself to using the database.

Thanks for your input on all of this.  Got more?

Geoff


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: How to run threads off ApplicationServlet

Posted by Benjamin Tomasini <bt...@neteverything.com>.
What happens if your servlet container creates two instances of the
servlet?  What if you have a failover servlet container on the same box
or on multiple boxes?  Are you intending to have multiple instances of
your RepeatingTask running?

On Sat, 2004-09-11 at 19:25, Geoff Callender wrote:
> Geoff Callender <geocal <at> attglobal.net> writes:
> 
> > Awesome, Eric.  
> > 
> > Looking at the API it looks like the ApplicationServlet
> > can get to the Global object with getEngine().getGlobal() ,
> > so it should be a piece of cake for it to publish its results
> > where other web pages can poll them.
> > 
> > Many thanks,
> > 
> > Geoff
> 
> 
> For anyone who needs to do it, here's what I found about how to run 
> threads off Tapestry's single servlet, so that they start up and shut 
> down with your web server.
> 
> 1. Make sure your thread class has run() and destroy() methods.  Eg.
> 
> public class RepeatingTask extends Thread {
> 
> 	public void run() {
> 		while (true) {
> 			System.out.println("Awake.  Going back to sleep.");
> 			try {
> 				Thread.sleep(10000);
> 			}
> 			catch (InterruptedException e) {
> 				throw new RuntimeException(e);
> 			}
> 		}
> 	}
> 	
> 	public void destroy() {
> 	}
> }
> 
> The destroy() method can be empty, but without it you'll get exceptions 
> later.
> 
> 2. Subclass org.apache.tapestry.ApplicationServlet.  
> In the init(ServletConfig config) method, instantiate and run your 
> threads.  In the destroy() method, destroy your threads. 
> Eg. 
> 
> public class MyApplicationServlet extends ApplicationServlet {
> 	private RepeatingTask _repeatingTask;
> 
> 	public void init(ServletConfig config) throws ServletException {
> 		super.init(config);
> 		_repeatingTask = new RepeatingTask();
> 		_repeatingTask.start();
> 	}
> 	
> 	public void destroy() {
> 		_repeatingTask.destroy();
> 		super.destroy();
> 	}
> }
> 
> 3. Add a line to your ".application" file to tell Tapestry to use your new 
> servlet instead of the default one.  Eg. 
> 
> 	<property name="org.apache.tapestry.specification-path" 
> value="myPackage.MyApplicationServlet"/>
> 
> 4. Replace the line in your web.xml file that says which servlet to load up. 
> Eg.
> 
> Replace this:
>     <servlet-class>org.apache.tapestry.ApplicationServlet</servlet-class>
> With this:
>     <servlet-class>myPackage.MyApplicationServlet</servlet-class>
> 
> That's all there is to it.
> 
> What I still couldn't figure out, though, was how to get the global class 
> from inside the thread.  Even if the thread has a reference back to the 
> servlet, it doesn't help because you can't use 
> getEngine(RequestContext context).getGlobal() without a context.
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
-- 
Benjamin Tomasini
NetEverything, Inc.
1-877-270-1391


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org