2011-01-27 8 views
4

私はSpring quartz Schedulerを使用していますが、私はXMLファイルを使用していません。プログラム全体で構成全体を作成したいと思います。プログラムでトリガオブジェクトを作成するには?

私は次のコードを書いています。

package com.eaportal.service.impl; 

import java.text.ParseException; 
import java.util.Date; 
import java.util.HashMap; 
import java.util.Map; 

import org.quartz.JobDetail; 
import org.springframework.scheduling.SchedulingException; 
import org.springframework.scheduling.quartz.CronTriggerBean; 
import org.springframework.scheduling.quartz.JobDetailBean; 
import org.springframework.scheduling.quartz.SchedulerFactoryBean; 

import com.eaportal.service.intfc.AuctionWinnerService; 

public class NormalAuctionWinnerServiceImpl1 implements AuctionWinnerService { 

    @SuppressWarnings("deprecation") 
    public void declareWinner(int auctionId, Map<String, Object> parameterMap) { 
     System.out.println("INSIDE DECLARE WINNER METHOD."); 
     /** STEP 1 : INSTANTIATE TASK CLASS **/ 
     NormalAuctionWinnerTask1 runMeTask = new NormalAuctionWinnerTask1(); 
     System.out.println("FINISHED STEP 1"); 

     /** STEP 2 : INSTANTIATE JOB DETAIL CLASS AND SET ITS PROPERTIES **/ 
     Map<String,Object> jobDataAsMap = new HashMap<String,Object>(); 
     jobDataAsMap.put("runMeTask",runMeTask); 
     JobDetailBean jdb = new JobDetailBean(); 
     jdb.setJobClass(NormalAuctionWinnerTask1.class); 
     jdb.setJobDataAsMap(jobDataAsMap); 
     System.out.println("FINISHED STEP 2"); 

     /** STEP 3 : INSTANTIATE CRON TRIGGER AND SET ITS PROPERTIES **/ 
     CronTriggerBean ctb = new CronTriggerBean(); 
     Date d1 = new Date(); 
     Date d2 = new Date(); 
     d2.setMinutes(d1.getMinutes()+10); 
     ctb.setStartTime(d1); 
     ctb.setEndTime(d2); 
     ctb.setJobDetail(jdb); 

     try { 
      ctb.setCronExpression("59 * * * * ? *"); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 

     /** STEP 4 : INSTANTIATE SCHEDULER FACTORY BEAN AND SET ITS PROPERTIES **/ 
     SchedulerFactoryBean sfb = new SchedulerFactoryBean(); 
     sfb.setJobDetails(new JobDetail[]{jdb}); 
     try { 
      sfb.start(); 
     } catch (SchedulingException e) { 
      e.printStackTrace(); 
     } 
    } 

} 

トリガー以外のコードは動作していますが、私は設定していません。

ここで問題はXML設定ではschedulerFactoryBeanの 'triggers'プロパティがあり、listを使用してトリガーを設定しています。

ただし、プログラムで同じプロパティを設定することはできません。 Trigger の配列を受け入れるSchedulerFactoryBeanにはsetTriggersメソッドがありますが、作成方法は問題です。

私は最後の4時間はまだ成功の兆候はありません。

誰かが私を助けることができますか?

おかげ

+0

これはあなたの他の質問と少し似ています:http://stackoverflow.com/questions/4794560/quartz-integration-with-spring/ – Ralph

+0

さて、そこには小さな違いがあります。これは、Quartzに対する春のサポートを使用し、もう1つはQuartzをサポートしません。私はあなたの提案を試してみました。他の質問に対しては朗報ですが、ログファイルにエラーはありませんでした。だから、私は春のサポートを使用するように切り替えましたが、今はこれで諦めています。私のアプローチが上記のコードに合っているかどうかを教えてください。トリガープロパティを設定する方法を教えてください。 – cyclecount

答えて

-1

主な問題は、あなたがジョブをスケジュールする必要があることを、次のようになります。

scheduler.scheduleJob(jobDetail, trigger); 

そして、私はそれが春QUARZ Beanのある方法を知っているが、通常のクオーツジョブとトリガーはありません必要があります名前とグループがあります!そして、あなたは、スケジューラを起動する必要があります。scheduler.start();

私はあなたのコード、それは春なしで動作することを少し変更した、そしてすべてはそれがどのように動作するかを示すために、必要とされていないものを除外:

パッケージテストを;

import java.text.ParseException; 
import java.util.Date; 

import org.quartz.CronTrigger; 
import org.quartz.Job; 
import org.quartz.JobDetail; 
import org.quartz.JobExecutionContext; 
import org.quartz.JobExecutionException; 
import org.quartz.Scheduler; 
import org.quartz.SchedulerException; 
import org.quartz.SchedulerFactory; 
import org.quartz.impl.StdSchedulerFactory; 


public class Demo { 

    public static class TestJob implements Job{ 
     @Override 
     public void execute(JobExecutionContext arg0) throws JobExecutionException { 
      System.out.println("execute");   
     } 
    } 

    public static void declareWinner() throws SchedulerException, ParseException { 

     JobDetail jobDetail = new JobDetail("job","group",Demo.TestJob.class); 

     CronTrigger trigger = new CronTrigger("trigger","group"); 

     trigger.setStartTime(new Date()); 
     trigger.setEndTime(new Date(new Date().getTime() + 10 * 60 * 1000)); 
     trigger.setCronExpression("* * * * * ? *");  

     /** STEP 4 : INSTANTIATE SCHEDULER FACTORY BEAN AND SET ITS PROPERTIES **/ 
     SchedulerFactory sfb = new StdSchedulerFactory(); 
     Scheduler scheduler = sfb.getScheduler();  

     scheduler.scheduleJob(jobDetail, trigger);  

     scheduler.start();     
    } 

    public static void main (String[] args) throws SchedulerException, Exception { 
     declareWinner(); 
     Thread.sleep(10000); 
    } 

} 
8

私はこれをSpring Scheduling Frameworkで正常に実行できました。

私はこれは非常に古い投稿だと理解していますが、このトピックの内容はかなり不十分なので、ここに入れておくとよいでしょう。

最初の投稿のコードの主な問題点は、がJobDetailオブジェクトとCronTriggerオブジェクトの両方で呼び出されていないことです。 afterProperties関数は、cronを実行する準備が整う前に、入力された値に対して何らかの処理を行います。

また、通常のjobDetailオブジェクトの代わりにMethodInvokingJobDetailFactoryBeanを使用しました。これは、指定されたクラスのcronによって呼び出される関数の柔軟性が向上するためです。ここで

は私のコードです:

package test.spring; 

import org.quartz.JobDetail; 
import org.quartz.Trigger; 
import org.springframework.context.support.GenericApplicationContext; 
import org.springframework.scheduling.SchedulingException; 
import org.springframework.scheduling.quartz.CronTriggerBean; 
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean; 
import org.springframework.scheduling.quartz.SchedulerFactoryBean; 

import java.text.ParseException; 

public class ProgrammaticCron { 

    public static void callWorkFlow() { 
     System.out.println("Abhishek Jain"); 
    } 

    public static void main (String[] args) { 
     try { 
      GenericApplicationContext applicationContext = new GenericApplicationContext(); 

      MethodInvokingJobDetailFactoryBean jdfb = new MethodInvokingJobDetailFactoryBean(); 
      jdfb.setTargetClass(ProgrammaticCron.class); 
      jdfb.setTargetMethod("callWorkFlow"); 
      jdfb.setName("Trial program"); 
      jdfb.afterPropertiesSet(); 
      JobDetail jd = (JobDetail)jdfb.getObject(); 

      CronTriggerBean ctb = new CronTriggerBean(); 
      ctb.setJobDetail(jd); 
      ctb.setName("Daily cron"); 
      ctb.setJobName(jd.getName()); 
      try { 
       ctb.setCronExpression("59 * * * * ? *"); 
      } catch (ParseException e) { 
       e.printStackTrace(); 
      } 

      ctb.afterPropertiesSet(); 

      SchedulerFactoryBean sfb = new SchedulerFactoryBean(); 
      sfb.setJobDetails(new JobDetail[]{(JobDetail)jdfb.getObject()}); 
      sfb.setTriggers(new Trigger[]{ctb}); 
      sfb.afterPropertiesSet(); 
      try { 
       sfb.start(); 
      } catch (SchedulingException e) { 
       e.printStackTrace(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

afterProperties()は重要であり、それは次のようであるSchedulerFactoryBeanafterProperties実装から理解することができる。

//--------------------------------------------------------------------- 
// Implementation of InitializingBean interface 
//--------------------------------------------------------------------- 

public void afterPropertiesSet() throws Exception { 
    if (this.dataSource == null && this.nonTransactionalDataSource != null) { 
     this.dataSource = this.nonTransactionalDataSource; 
    } 

    if (this.applicationContext != null && this.resourceLoader == null) { 
     this.resourceLoader = this.applicationContext; 
    } 

    // Create SchedulerFactory instance. 
    SchedulerFactory schedulerFactory = (SchedulerFactory) 
      BeanUtils.instantiateClass(this.schedulerFactoryClass); 

    initSchedulerFactory(schedulerFactory); 

    if (this.resourceLoader != null) { 
     // Make given ResourceLoader available for SchedulerFactory configuration. 
     configTimeResourceLoaderHolder.set(this.resourceLoader); 
    } 
    if (this.taskExecutor != null) { 
     // Make given TaskExecutor available for SchedulerFactory configuration. 
     configTimeTaskExecutorHolder.set(this.taskExecutor); 
    } 
    if (this.dataSource != null) { 
     // Make given DataSource available for SchedulerFactory configuration. 
     configTimeDataSourceHolder.set(this.dataSource); 
    } 
    if (this.nonTransactionalDataSource != null) { 
     // Make given non-transactional DataSource available for SchedulerFactory configuration. 
     configTimeNonTransactionalDataSourceHolder.set(this.nonTransactionalDataSource); 
    } 


    // Get Scheduler instance from SchedulerFactory. 
    try { 
     this.scheduler = createScheduler(schedulerFactory, this.schedulerName); 
     populateSchedulerContext(); 

     if (!this.jobFactorySet && !(this.scheduler instanceof RemoteScheduler)) { 
      // Use AdaptableJobFactory as default for a local Scheduler, unless when 
      // explicitly given a null value through the "jobFactory" bean property. 
      this.jobFactory = new AdaptableJobFactory(); 
     } 
     if (this.jobFactory != null) { 
      if (this.jobFactory instanceof SchedulerContextAware) { 
       ((SchedulerContextAware) this.jobFactory).setSchedulerContext(this.scheduler.getContext()); 
      } 
      this.scheduler.setJobFactory(this.jobFactory); 
     } 
    } 

    finally { 
     if (this.resourceLoader != null) { 
      configTimeResourceLoaderHolder.remove(); 
     } 
     if (this.taskExecutor != null) { 
      configTimeTaskExecutorHolder.remove(); 
     } 
     if (this.dataSource != null) { 
      configTimeDataSourceHolder.remove(); 
     } 
     if (this.nonTransactionalDataSource != null) { 
      configTimeNonTransactionalDataSourceHolder.remove(); 
     } 
    } 

    registerListeners(); 
    registerJobsAndTriggers(); 
     } 

お気づきのとおり、このようなすべてのタスクスケジューラーを取得し、トリガーにジョブを登録することは、この機能の一部として行われます。