You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jan Behrens <ja...@daxcolog.de> on 2004/07/15 14:14:06 UTC

[OT] Best practice for background service

Hi list,

I am coding an app where I rely on a background service to check regularly
for new mail. I want to instantiate my service component (the one checking
for mail) when the context is loaded and have it running in a background
thread. I have done only very limited coding with threads so far :(

What I plan to do is to create a controller servlet that is loaded on
startup and that creates instances of all my services. All services extend
Thread and are started by invoking the run() method when the controller
servlet starts. Would that work? How would I then set the intervall on which
my mail service checks for new mail? Could this be done using
sleep(interval)?

I wonder whether anyone has tips on this for a newbie or if there is such a
thing as a best practice on this.

TIA, Jan


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


Re: [OT] Best practice for background service

Posted by Bryan Hunt <ad...@revoltingdigits.com>.
better idea , integrate the spring framwork and use the built in support 
for quartz scheduling.

http://www.springframework.org/docs/reference/index.html

http://www.springframework.org/docs/reference/scheduling.html

--b
*

*
Jan Behrens wrote:

>Hi list,
>
>I am coding an app where I rely on a background service to check regularly
>for new mail. I want to instantiate my service component (the one checking
>for mail) when the context is loaded and have it running in a background
>thread. I have done only very limited coding with threads so far :(
>
>What I plan to do is to create a controller servlet that is loaded on
>startup and that creates instances of all my services. All services extend
>Thread and are started by invoking the run() method when the controller
>servlet starts. Would that work? How would I then set the intervall on which
>my mail service checks for new mail? Could this be done using
>sleep(interval)?
>
>I wonder whether anyone has tips on this for a newbie or if there is such a
>thing as a best practice on this.
>
>TIA, Jan
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>
>  
>

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


RE: [OT] Best practice for background service

Posted by Marco Mistroni <mm...@waersystems.com>.
Hello,
	If u r familiar with JMX, there is a Timer service
That does exactly what you want (emit notifications at certain
Interval)....

Regards
	marco

-----Original Message-----
From: news [mailto:news@sea.gmane.org] On Behalf Of Bill Siggelkow
Sent: 15 July 2004 14:29
To: user@struts.apache.org
Subject: Re: [OT] Best practice for background service

Jan,
Bryan's recommendation of Spring and Quartz sounds good though I have 
not had a chance to work with these yet. If you want to "roll your own" 
I suggest you look at the java.util.Timer and java.util.TimerTask 
objects -- they work well for these type of services. See 
http://java.sun.com/j2se/1.4.2/docs/api/java/util/TimerTask.html.

Jan Behrens wrote:

> Hi list,
> 
> I am coding an app where I rely on a background service to check
regularly
> for new mail. I want to instantiate my service component (the one
checking
> for mail) when the context is loaded and have it running in a
background
> thread. I have done only very limited coding with threads so far :(
> 
> What I plan to do is to create a controller servlet that is loaded on
> startup and that creates instances of all my services. All services
extend
> Thread and are started by invoking the run() method when the
controller
> servlet starts. Would that work? How would I then set the intervall on
which
> my mail service checks for new mail? Could this be done using
> sleep(interval)?
> 
> I wonder whether anyone has tips on this for a newbie or if there is
such a
> thing as a best practice on this.
> 
> TIA, Jan


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


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


RE: [OT] Best practice for background service

Posted by Daniel Perry <d....@netcase.co.uk>.
Nope, you dont need to start another process - i use a struts plugin to load
and initialise Quartz.  It will run in other threads, but this will all be
done for you behind the scenes.

The code for my struts plugin is below (if it's of any help).  Add the
following entry to the struts-config-default.xml in the plug-in section to
load it:

<plug-in className="com.netcase.pdp.service.SchedulerService"></plug-in>

It runs a "Job" every 30 mins.

This job takes the form:

public class RemoveOldProvisionalTrainingJob implements StatefulJob {
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// code to do actual work here
}
}


Note that i'm implementing StatfulJob - this stops it running two of the
same job at the same time.

Daniel.


---------------------SchedulerService.java-------------------------
package com.netcase.pdp.service;

import java.util.Date;

import javax.servlet.ServletException;

import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;

import
com.netcase.pdp.service.scheduledjobs.RemoveOldProvisionalTrainingJob;

/**
 * @author Daniel Perry
 *
 */
public class SchedulerService implements PlugIn {

	Scheduler sched;

	public void init(ActionServlet servlet, ModuleConfig moduleConf)
			throws ServletException {
		System.out.println("Starting scheduler service...");

		SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
		try {
			sched = schedFact.getScheduler();
			sched.start();

				JobDetail jobDetail = new JobDetail(
						"removeOldProvisionalTrainingJob",
						Scheduler.DEFAULT_GROUP,
						RemoveOldProvisionalTrainingJob.class);

				// new trigger - repeat every 30 mins starting in 5 mins time
				// (delay for startup)
				SimpleTrigger trigger = new SimpleTrigger("30MinTrigger",
						Scheduler.DEFAULT_GROUP, new Date(new Date().getTime()
								+ (5L* 60L * 1000L)), null,
						SimpleTrigger.REPEAT_INDEFINITELY, 30 * 60L * 1000L);

				sched.scheduleJob(jobDetail, trigger);


		} catch (SchedulerException ex) {
			ex.printStackTrace();
		}

	}

	public void destroy() {
		try {
			sched.shutdown();
		} catch (SchedulerException ex) {
			ex.printStackTrace();
		}
		sched = null;
	}

}
-------------------------------------------------------------------



> -----Original Message-----
> From: Marco Mistroni [mailto:mmistroni@waersystems.com]
> Sent: 15 July 2004 14:57
> To: 'Struts Users Mailing List'
> Subject: RE: [OT] Best practice for background service
>
>
> Hello,
> 	Sorry for 'OT' for asking questions about Quartz..
> Is it so that I have to start a separate 'process' for Quartz to run?
> So, at the end I will have my application server running as well as a
> Quartz process running 'outside' the application server?
>
> Regards
> 	marco
>
> -----Original Message-----
> From: Daniel Perry [mailto:d.perry@netcase.co.uk]
> Sent: 15 July 2004 14:48
> To: Struts Users Mailing List
> Subject: RE: [OT] Best practice for background service
>
> Quartz is very easy to use.  No need for thread programming.
>
> But "Job" classes are created as and when they are needed (so no
> initialisation and shared object).
>
> Create a struts plug-in which initialises quartz, and sets up the jobs
> (very
> little code needed).
>
> Daniel.
>
> > -----Original Message-----
> > From: news [mailto:news@sea.gmane.org]On Behalf Of Bill Siggelkow
> > Sent: 15 July 2004 14:29
> > To: user@struts.apache.org
> > Subject: Re: [OT] Best practice for background service
> >
> >
> > Jan,
> > Bryan's recommendation of Spring and Quartz sounds good though I have
> > not had a chance to work with these yet. If you want to "roll your
> own"
> > I suggest you look at the java.util.Timer and java.util.TimerTask
> > objects -- they work well for these type of services. See
> > http://java.sun.com/j2se/1.4.2/docs/api/java/util/TimerTask.html.
> >
> > Jan Behrens wrote:
> >
> > > Hi list,
> > >
> > > I am coding an app where I rely on a background service to
> > check regularly
> > > for new mail. I want to instantiate my service component (the
> > one checking
> > > for mail) when the context is loaded and have it running in a
> background
> > > thread. I have done only very limited coding with threads so far :(
> > >
> > > What I plan to do is to create a controller servlet that is loaded
> on
> > > startup and that creates instances of all my services. All
> > services extend
> > > Thread and are started by invoking the run() method when the
> controller
> > > servlet starts. Would that work? How would I then set the
> > intervall on which
> > > my mail service checks for new mail? Could this be done using
> > > sleep(interval)?
> > >
> > > I wonder whether anyone has tips on this for a newbie or if
> > there is such a
> > > thing as a best practice on this.
> > >
> > > TIA, Jan
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


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


RE: [OT] Best practice for background service

Posted by Marco Mistroni <mm...@waersystems.com>.
Hello,
	Sorry for 'OT' for asking questions about Quartz..
Is it so that I have to start a separate 'process' for Quartz to run?
So, at the end I will have my application server running as well as a 
Quartz process running 'outside' the application server?

Regards
	marco

-----Original Message-----
From: Daniel Perry [mailto:d.perry@netcase.co.uk] 
Sent: 15 July 2004 14:48
To: Struts Users Mailing List
Subject: RE: [OT] Best practice for background service

Quartz is very easy to use.  No need for thread programming.

But "Job" classes are created as and when they are needed (so no
initialisation and shared object).

Create a struts plug-in which initialises quartz, and sets up the jobs
(very
little code needed).

Daniel.

> -----Original Message-----
> From: news [mailto:news@sea.gmane.org]On Behalf Of Bill Siggelkow
> Sent: 15 July 2004 14:29
> To: user@struts.apache.org
> Subject: Re: [OT] Best practice for background service
>
>
> Jan,
> Bryan's recommendation of Spring and Quartz sounds good though I have
> not had a chance to work with these yet. If you want to "roll your
own"
> I suggest you look at the java.util.Timer and java.util.TimerTask
> objects -- they work well for these type of services. See
> http://java.sun.com/j2se/1.4.2/docs/api/java/util/TimerTask.html.
>
> Jan Behrens wrote:
>
> > Hi list,
> >
> > I am coding an app where I rely on a background service to
> check regularly
> > for new mail. I want to instantiate my service component (the
> one checking
> > for mail) when the context is loaded and have it running in a
background
> > thread. I have done only very limited coding with threads so far :(
> >
> > What I plan to do is to create a controller servlet that is loaded
on
> > startup and that creates instances of all my services. All
> services extend
> > Thread and are started by invoking the run() method when the
controller
> > servlet starts. Would that work? How would I then set the
> intervall on which
> > my mail service checks for new mail? Could this be done using
> > sleep(interval)?
> >
> > I wonder whether anyone has tips on this for a newbie or if
> there is such a
> > thing as a best practice on this.
> >
> > TIA, Jan
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


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


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


RE: [OT] Best practice for background service

Posted by Jan Behrens <ja...@daxcolog.de>.
Thanks to all for their answers,

I think I will look into doing this with java.util.Timer & TimerTask for now
and keep Spring and Quartz in mind to be looked at later. I now that it
usually takes quite a bit of time to get to terms with a new framework (for
me that is, of course) and I just can't spare the time right now.

Cheers & thanks again.

Jan

-----Original Message-----
From: Daniel Perry [mailto:d.perry@netcase.co.uk] 
Sent: Thursday, July 15, 2004 3:48 PM
To: Struts Users Mailing List
Subject: RE: [OT] Best practice for background service


Quartz is very easy to use.  No need for thread programming.

But "Job" classes are created as and when they are needed (so no
initialisation and shared object).

Create a struts plug-in which initialises quartz, and sets up the jobs (very
little code needed).

Daniel.

> -----Original Message-----
> From: news [mailto:news@sea.gmane.org]On Behalf Of Bill Siggelkow
> Sent: 15 July 2004 14:29
> To: user@struts.apache.org
> Subject: Re: [OT] Best practice for background service
>
>
> Jan,
> Bryan's recommendation of Spring and Quartz sounds good though I have 
> not had a chance to work with these yet. If you want to "roll your 
> own" I suggest you look at the java.util.Timer and java.util.TimerTask 
> objects -- they work well for these type of services. See 
> http://java.sun.com/j2se/1.4.2/docs/api/java/util/TimerTask.html.
>
> Jan Behrens wrote:
>
> > Hi list,
> >
> > I am coding an app where I rely on a background service to
> check regularly
> > for new mail. I want to instantiate my service component (the
> one checking
> > for mail) when the context is loaded and have it running in a 
> > background thread. I have done only very limited coding with threads 
> > so far :(
> >
> > What I plan to do is to create a controller servlet that is loaded 
> > on startup and that creates instances of all my services. All
> services extend
> > Thread and are started by invoking the run() method when the 
> > controller servlet starts. Would that work? How would I then set the
> intervall on which
> > my mail service checks for new mail? Could this be done using 
> > sleep(interval)?
> >
> > I wonder whether anyone has tips on this for a newbie or if
> there is such a
> > thing as a best practice on this.
> >
> > TIA, Jan
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


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


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


RE: [OT] Best practice for background service

Posted by Daniel Perry <d....@netcase.co.uk>.
Quartz is very easy to use.  No need for thread programming.

But "Job" classes are created as and when they are needed (so no
initialisation and shared object).

Create a struts plug-in which initialises quartz, and sets up the jobs (very
little code needed).

Daniel.

> -----Original Message-----
> From: news [mailto:news@sea.gmane.org]On Behalf Of Bill Siggelkow
> Sent: 15 July 2004 14:29
> To: user@struts.apache.org
> Subject: Re: [OT] Best practice for background service
>
>
> Jan,
> Bryan's recommendation of Spring and Quartz sounds good though I have
> not had a chance to work with these yet. If you want to "roll your own"
> I suggest you look at the java.util.Timer and java.util.TimerTask
> objects -- they work well for these type of services. See
> http://java.sun.com/j2se/1.4.2/docs/api/java/util/TimerTask.html.
>
> Jan Behrens wrote:
>
> > Hi list,
> >
> > I am coding an app where I rely on a background service to
> check regularly
> > for new mail. I want to instantiate my service component (the
> one checking
> > for mail) when the context is loaded and have it running in a background
> > thread. I have done only very limited coding with threads so far :(
> >
> > What I plan to do is to create a controller servlet that is loaded on
> > startup and that creates instances of all my services. All
> services extend
> > Thread and are started by invoking the run() method when the controller
> > servlet starts. Would that work? How would I then set the
> intervall on which
> > my mail service checks for new mail? Could this be done using
> > sleep(interval)?
> >
> > I wonder whether anyone has tips on this for a newbie or if
> there is such a
> > thing as a best practice on this.
> >
> > TIA, Jan
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


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


Re: [OT] Best practice for background service

Posted by Bill Siggelkow <bi...@bellsouth.net>.
Jan,
Bryan's recommendation of Spring and Quartz sounds good though I have 
not had a chance to work with these yet. If you want to "roll your own" 
I suggest you look at the java.util.Timer and java.util.TimerTask 
objects -- they work well for these type of services. See 
http://java.sun.com/j2se/1.4.2/docs/api/java/util/TimerTask.html.

Jan Behrens wrote:

> Hi list,
> 
> I am coding an app where I rely on a background service to check regularly
> for new mail. I want to instantiate my service component (the one checking
> for mail) when the context is loaded and have it running in a background
> thread. I have done only very limited coding with threads so far :(
> 
> What I plan to do is to create a controller servlet that is loaded on
> startup and that creates instances of all my services. All services extend
> Thread and are started by invoking the run() method when the controller
> servlet starts. Would that work? How would I then set the intervall on which
> my mail service checks for new mail? Could this be done using
> sleep(interval)?
> 
> I wonder whether anyone has tips on this for a newbie or if there is such a
> thing as a best practice on this.
> 
> TIA, Jan


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