Pages

Tuesday, April 20, 2010

Tapestry 5 and Quartz

Using Quartz with tapestry is pretty easy , thanks to the ChenilleKit project. By following the guide in the chenillekit-quartz project you can use quartz to schedule jobs in your application. But in my case, i need to modify the example a little to make it work.

in the Bundle class :

    private void createBundle() {
        try {
            trigger =  new CronTrigger("myCronTrigger", "CronTriggerGroup", "0 42 7/1 ? * *");
        } catch (ParseException e) {
            e.printStackTrace();
        }
       
        jobDetail = new JobDetail("myJob", null, CrawlingJob.class);
        jobDetail.getJobDataMap().put("crawlingJob", crawlingJob);
        jobDetail.getJobDataMap().put("crawler", crawler);
    }

I'm adding the job (crawlingJob) and the actual services (crawler) into the job data map. So in the Job class :

    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("Execute job Scheduled Crawler at : " + new Date());
       
        Crawler crawler = (Crawler) context.getJobDetail().getJobDataMap().get("crawler");
        crawler.updateData(CrawlAjax.URL);       
    }
we only need to extract the service class from the context and then execute the real method. As simple as that. And the AFAIK the minimum library you need to include to make it work is :
  • chenillekit-quartz-1.0.2.jar
  • commons-codec-1.4.jar
  • commons-collections-3.1.jar
  • quartz-all-1.6.5.jar

No comments: