Hi Angelo,

It's been several months since I wrote this, so the details are
fuzzy...but here are some snippets of some code that I'm currently
running in production:

/* The following service is registered with Tapestry IOC.  It schedules
jobs via Quartz */
public class SchedulerServiceImpl implements SchedulerService {
    
    public class TapestryIocJobFactory implements JobFactory          {
        ServiceResources _srvcRsrcs;
        Logger _logger;
        
        TapestryIocJobFactory( ServiceResources prmSrvcRsrcs, Logger
prmLogger )  {
            _srvcRsrcs = prmSrvcRsrcs;
            _logger = prmLogger;
        }
        
        public Job newJob( TriggerFiredBundle prmTrgrFiredBndl )   {
            Class c = prmTrgrFiredBndl.getJobDetail().getJobClass();
            return( (Job)_srvcRsrcs.getService( c ) );
        }
    }
    
    public SchedulerServiceImpl( ServiceResources prmSrvcRsrcs, Logger
prmLogger, SystemParameterService prmSystmPrmtrSrvc )           {

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

            Scheduler sched = schedFact.getScheduler();
            
            sched.setJobFactory( new
TapestryIocJobFactory( prmSrvcRsrcs, prmLogger ) );

            sched.start();

            /* The first job to be registered: the job that
             * looks for newly-queued messages to send via smtp.
             */
            JobDetail jobDetail = new JobDetail("sendMailJob",
                    null,
                    SendMailJob.class );
            
            Trigger trigger = TriggerUtils.makeSecondlyTrigger( 10 );
            trigger.setStartTime( new Date() );
            trigger.setName( "sendMailTrigger" );

            sched.scheduleJob(jobDetail, trigger);

            /* The second job to be registered: the job that
             * deletes any pending messages that were never
             * completed.
             */
            jobDetail = new JobDetail("cleanPendingMailJob",
                    null,
                    CleanPendingMailJob.class );
            
            trigger = TriggerUtils.makeDailyTrigger( 2, 3 );
            trigger.setName( "cleanPendingMailTrigger" );

            sched.scheduleJob(jobDetail, trigger);

            /* And so one and so forth, registering more jobs. */

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

The above service is registered in AppModule like this, to force it to load 
when the IOC registry starts up:

    binder.bind( SchedulerService.class, SchedulerServiceImpl.class 
).eagerLoad();

Here's an example of one of the jobs registered above:

public class SendMailJobImpl extends HENJob implements SendMailJob {

    final private MessageService _msgSrvc;

    /**
     * 
     */
    public SendMailJobImpl( Logger prmLogger, org.hibernate.Session prmSn,
                            UserDAO prmUserDAO, MessageService prmMsgSrvc,
                            SystemParameterService prmSystmPrmtrSrvc,
                            PerthreadManager prmThrdMngr, 
HibernateSessionManager prmHbrntSnMngr ) {
        super( prmLogger, prmSn, prmUserDAO, prmSystmPrmtrSrvc, prmThrdMngr, 
prmHbrntSnMngr );
        _msgSrvc = prmMsgSrvc;
    }
    
    @Override
    public void run() {
        /* Code to get a list of emails ready to send, and to send them via 
SMTP. */
    }
    
}

And here's the parent of the above class...all of my quartz jobs are 
descendants of this class:

public abstract class HENJob implements Job {
    
    private final Logger _logger;
    private final Session _sn;
    private final PerthreadManager _perThrdMngr;
    private final HibernateSessionManager _hbrntSnMngr;

    private JobDataMap _jobDataMap = null;

    public HENJob( Logger prmLogger, Session prmSn,
                    PerthreadManager prmThrdMngr, HibernateSessionManager 
prmHbrntSnMngr )  {
        _logger = prmLogger;
        _sn = prmSn;
        _userDAO = prmUserDAO;
        _systmPrmtrSrvc = prmSystmPrmtrSrvc;
        _perThrdMngr = prmThrdMngr;
        _hbrntSnMngr = prmHbrntSnMngr;
    }

    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        try     {
            _jobDataMap = arg0.getMergedJobDataMap();

            run();

            _hbrntSnMngr.commit();
        }   finally             {
            _perThrdMngr.cleanup();
        }
    }
    
    protected abstract String getSystmPrmtrPrfx();
    
    protected abstract void run();

    public JobDataMap getJobDataMap() {
        return _jobDataMap;
    }

    public Logger getLogger() {
        return _logger;
    }

    public Session getSn() {
        return _sn;
    }

}

Good luck!

Andy

On Sat, 2008-10-11 at 02:40 -0700, Angelo Chen wrote:
> Hi Andy,
> 
> Any sample code for this? Thanks.
> 
> Angelo
> 
> 
> Andy Huhn wrote:
> > 
> > Thanks, Howard...ServiceResources did the trick!
> > 
> > On Sat, 2007-12-15 at 06:53 -0800, Howard Lewis Ship wrote:
> >> The Registry is not exposed, a service may be passed its
> >> ServiceResources via a constructor parameter. This is an extension of
> >> ServiceLocator, which includes all the key methods of Registry,
> >> allowing services to be obtained by type or by service id.
> >> 
> >> In addition, starting in 5.0.7., service proxies are serializable,
> >> which helps when integrating with Quartz.  You can store a reference
> >> to a service in a non-transient field of your Job.  I did some Quartz
> >> integration for a project using the 5.0.5 code, and it required
> >> jumping through some serious hoops.
> >> 
> >> In fact, that code code was an example of why I think you are going
> >> down the wrong path.  I had a JobRunner service that tooks mapped
> >> contribution of ScheduledJobs; the ScheduledJob was a wrapper around
> >> some scheduling information and a Runnable object.  Dependencies were
> >> injected into the contribute method(s) and packaged up as Runnable's
> >> that are part of the configuration.  The JobRunner was @EagerLoad.
> >> 
> >> On Dec 14, 2007 9:22 PM, Andy Huhn <[EMAIL PROTECTED]> wrote:
> >> > All,
> >> >
> >> > I'd like to use Quartz with tapestry.  As such, I'd like to get a
> >> > reference to the Registry and pass it into a Tapestry service (so that
> >> I
> >> > can use it in my Quartz jobs to instantiate all of my DAOs and
> >> Hibernate
> >> > Sessions).
> >> >
> >> > I saw some discussion here on the list a few days ago about the
> >> > Registry...it sounds like it's not exposed to services at all.  Is this
> >> > true?  Does anyone have any ideas how I can get a reference to it?
> >> >
> >> > I have also thought about building a completely new registry inside
> >> this
> >> > service for all of the quartz jobs to use, but my guess is that there
> >> > would be quite an impact on the machine's resources.  Does anyone have
> >> a
> >> > good feel for how much memory I can expect to give up if I instantiate
> >> a
> >> > second instance of the Registry?
> >> >
> >> > Thanks,
> >> > Andy
> >> >
> >> > ---------------------------------------------------------------------
> >> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> >> > For additional commands, e-mail: [EMAIL PROTECTED]
> >> >
> >> >
> >> 
> >> 
> >> 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> > 
> > 
> > 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to