0

spring 3.1 を使用して Quartz アプリケーションを作成しました。1 つの xml ファイル Spring-Quartz.xml を作成しました

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


<bean id="runMeTask" class="com.grit.quartz.RunMeTask" />

<!-- Spring Quartz -->
<bean name="runMeJob" class="org.springframework.scheduling.quartz.JobDetailBean">

    <property name="jobClass" value="com.grit.quartz.RunMeJob" />

    <property name="jobDataAsMap">
        <map>
            <entry key="runMeTask" value-ref="runMeTask" />
        </map>
    </property>

</bean>

<!-- <bean id="runMeJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 
    <property name="targetObject" ref="runMeTask" /> <property name="targetMethod" 
    value="printMe" /> </bean> -->

<!-- Simple Trigger, run every 5 seconds -->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">

    <property name="jobDetail" ref="runMeJob" />
    <property name="repeatInterval" value="5000" />
    <property name="startDelay" value="1000" />

</bean>

<!-- Cron Trigger, run every 5 seconds -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">

    <property name="jobDetail" ref="runMeJob" />
    <property name="cronExpression" value="0/1 * * * * ?" />

</bean>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="jobDetails">
        <list>
            <ref bean="runMeJob" />
        </list>
    </property>

    <property name="triggers">
        <list>
            <ref bean="simpleTrigger" />
        </list>
    </property>

    <property name="quartzProperties">
    <props>
        <prop key="org.quartz.threadPool.threadCount">15</prop>
    </props>
</property>   

</bean>

このファイルをスタンドアロンアプリケーションとして実行しているとき

new ClassPathXmlApplicationContext("Spring-Quartz.xml");

その正常に動作します。

しかし、このアプリケーションを Tomcat にデプロイするときに、このアプリケーションを開始する必要があります。

このために、私は ServletContextListener を作成し、コンテキストで初期化しました i Call new ClassPathXmlApplicationContext("Spring-Quartz.xml");

正常に動作していますが、Tomcat をシャットダウンした後も Schedular は引き続き動作しています。

4

3 に答える 3

0

Quartz Job SchedulerTime Intervalをスケジュールすることによるジョブ自動化のオープンソースです。トリガーが起動されるたびにジョブを実行できるようにします。トリガー リスナーは、指定された発火時刻に発火します。

Spring を使用した QuartzInitializerListener
DBS トリガーでレコードが変更されるたびに、 DB データを保持する sms Dto クラスを見つけて送信する

public class EmpDto {
    private Integer id;
    private String name;
    private Integer age;
    private Integer salary;
    private String address;

    private  List<EmpDto> empList;.....

特定の日時にジョブ/タスクを実行するようにスケジュールします。コンポーネント/クラスはJOBインターフェイスと @override execute() を実装する必要があります。トリガーが発生したときに ececute() メソッド コードがスケジューラによって実行されるようにします。

public class Quartz_JOB implements Job{

    private EmpDao edao; // configure in applicationContex.xml

    public EmpDao getEdao() {   return edao;    }
    public void setEdao(EmpDao edao) {  this.edao = edao;   }
    public static Integer size;

    public void execute(JobExecutionContext context) throws JobExecutionException {
        Date time = context.getFireTime();
        Date next_trigger_time =context.getNextFireTime();
        System.out.println("### Current Trigget Time : "+time+"### Next Trigger Time : "+next_trigger_time);

        JobKey job_key = context.getJobDetail().getKey();
        JobDataMap dataMap = context.getMergedJobDataMap(); // Get all the keys from Listener.
        System.out.println("Instance " + job_key + " of DumbJob says: " + jobSays + ", and val is: " + record_Size);
        System.out.println("Trigger Data : "+curr_Record_Size);

        System.out.println("#######Empdao : "+edao);

    }
    String jobSays;
    int record_Size;
    int curr_Record_Size;

    public int getCurr_Record_Size() {  return curr_Record_Size;    }
    public void setCurr_Record_Size(int curr_Record_Size) {
        this.curr_Record_Size = curr_Record_Size;
    }   
}

web.xml

<listener>
    <listener-class>org.quartz.ee.servlet.QuartzInitializerListener</listener-class>
</listener>

ウェブリスナー

@WebListener
public class QuartzListener extends QuartzInitializerListener {
     Scheduler scheduler = null;
     Integer size = null;

     @Override
     public void contextInitialized(ServletContextEvent servletContext) {
          System.out.println("Context Initialized");

          try {
      // JobDetails used to create instances of a class which Implement <<job>>
      // <<JobBuilder>> used to define/build instances of <<JobDetails>>. Which define istances of job.
             JobDetail job1 = JobBuilder
                                .newJob(Quartz_JOB.class)
                                .withIdentity("Job_ID", "Group1")
                                .usingJobData("jobSays", "Hello World!")
                                .usingJobData("recird_Size", 3)
                                .build();

      // <<Trigger>> a component that defines the scheduled 'Time Interval'. So that the given job to execute.
      // << TriggerBuilder>> used to create Trigger Instance.
             Trigger trigger = TriggerBuilder
                                .newTrigger()
                                .withIdentity("Trigger_ID", "Group1")
                                .forJob("Job_ID", "Group1")
                                .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
                                .usingJobData("curr_Record_Size", edto.getEmpList().size())
                                .build();



            // Setup the Job and Trigger with Scheduler & schedule jobs
            scheduler = new StdSchedulerFactory().getScheduler();
            scheduler.start();
            scheduler.scheduleJob(job1, trigger);                   
         }catch (SchedulerException e) {
              e.printStackTrace();
         }
      }
      @Override
      public void contextDestroyed(ServletContextEvent servletContext) {
          System.out.println("Context Destroyed");
           try {
              scheduler.shutdown();
              System.out.println("Quartz stoped");
           }catch (SchedulerException e) {
                 e.printStackTrace();
           }
      }
}

トリガーが発生するたびに、その静的変数保持レコード サイズで execute() メソッドが呼び出されます。異なる場合は現在のサイズを取得し、電子メールを送信します (別のクラスに電子メール ロジックを記述し、サイズを渡して execute() からそのメソッドを呼び出します)。

トウモロコシの発現を生成するには

于 2015-07-07T13:44:57.050 に答える
0

Destroy メソッドで、scheduler.shutdown()メソッドを呼び出します。

于 2013-07-17T08:34:50.330 に答える