[jboss-user] [JBoss Seam] - Re: seam and batch/queue-processing

kenglover do-not-reply at jboss.com
Mon Oct 1 12:48:55 EDT 2007


Maybe this will help...

I needed to schedule a regular recurring job from startup to shutdown.  This job polls out LDAP servers and keeps a list of the ones that are up.

There are 2 components, a controller, and the asynchronous job. The controller runs at startup and schedules the job, keeping a timer handle in the application scope.

The async job compares the timer handle from the controller and terminates if it does not work (to ensure we only have one job running).  Note that a crash or kill of the JBoss server may leave an extra scheduled job, which is why the official one is checked for.

Here is the controller:

  | @Name("LDAPWatcherController")
  | @Startup
  | @Scope(ScopeType.APPLICATION)
  | public class LDAPWatcherController {
  | 
  | 	@In(create=true)
  | 	private LDAPWatcher LDAPWatcher;
  | 	
  | 	@Logger
  | 	private Log log;
  | 	
  |     private TimerHandle timerHandle;
  | 	
  | 	@Create
  | 	public void startup() throws Exception
  | 	{
  | 		log.debug("Starting checkLDAP timer");
  | 		Timer timer = LDAPWatcher.checkLDAP( new Long(60000));
  | 		timerHandle = timer.getHandle();
  | 	}
  | 	
  | 	@Destroy
  | 	public void shutdown()
  | 	{
  | 		log.debug("Shutting down timed event " + timerHandle.toString());
  | 		timerHandle.getTimer().cancel();
  | 	}
  | 	
  | 	public Timer getTimerHandle()
  | 	{
  | 		return timerHandle.getTimer();
  | 	}
  | 	
  | }
  | 

Here is the LDAP Watcher (async job).  The LDAPWatcher that is implemented is a simple interface class that defines the public method with an @Asynchronous above it.

  | @Stateless
  | @Name("LDAPWatcher")
  | public class LDAPWatcherImpl implements LDAPWatcher {
  | 	@Logger
  | 	private Log log;
  | 	
  | 	@In
  | 	private Timer timer;
  | 	
  | 	public Timer checkLDAP(Long interval) {
  | 		log.info("Checking LDAPs " + this.toString());
  | 
  | 		// Make sure this is the "official" job and that we do not have too many of these running.
  | 		LDAPWatcherController appStart = (LDAPWatcherController) Contexts.getApplicationContext().get("LDAPWatcherController");
  | 		log.debug("My Info :" + this.timer.getInfo().toString());
  | 		log.debug("App Info:" + appStart.getTimerHandle().getInfo().toString());
  | 		if (this.timer.getInfo() != appStart.getTimerHandle().getInfo())
  | 		{
  | 			log.info("I am not the timer that should be running.  Stopping myself...");
  | 			this.timer.cancel();
  | 			return null;
  | 		}
  | 		
  | 		// Now on with the work
  |  	       [... snip ...]
  |  		
  | 		return null;
  | 	}
  | 
  | }
  | 



View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4090341#4090341

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4090341



More information about the jboss-user mailing list