2016-05-01 8 views
0

私はIntelliJ Ideaで作業しており、Tomcatをローカルサーバーとして使用しています。 私は5秒でMySQLデータベースにデータを挿入しようとしています。 applicationContext.xmlファイルでCron Triggerを使用してアプリケーションのスケジュールを設定します。applicationContext.xmlはCronTriggerSchedulerを呼び出しますが、アプリケーションはスケジュールされていません

applicationcontext.xmlファイルで、私はCurrencyAddJob.javaを呼び出すcronトリガとしてdummycodeを持っています。すでにクラスをスケジュールしています。しかし、私はCronTriggerScheduler.javaと呼ばれる新しいJavaクラスを作成し、私のアプリケーションを実行すると、アプリケーションはスケジュールされていません。

applicationContext.xmlを:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns="http://www.springframework.org/schema/beans" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 


    <context:annotation-config/> 

    <context:component-scan base-package="com.honorius.billing.*"> 
    </context:component-scan> 

    <context:property-placeholder location="classpath:com"/> 



<bean id="honoriusDataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
     <property name="url" value="jdbc:mysql://url adress here"/> 
     <property name="username" value="root"/> 
     <property name="password" value="password"/> 
     <property name="jmxEnabled" value="true"/> 
     <property name="initialSize" value="10"/> 
     <property name="maxActive" value="2000"/> 
     <property name="maxIdle" value="500"/> 
     <property name="minIdle" value="10"/> 
     <property name="suspectTimeout" value="60"/> 
     <property name="timeBetweenEvictionRunsMillis" value="30000"/> 
     <property name="minEvictableIdleTimeMillis" value="60000"/> 
     <property name="validationQuery" value="select 1 from dual;"/> 
     <property name="testOnConnect" value="true"/> 
     <property name="testWhileIdle" value="true"/> 
    </bean> 

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> 
     <constructor-arg ref="honoriusDataSource"/> 
    </bean> 


    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
     <property name="applicationContextSchedulerContextKey" value="applicationContext"/> 

     <property name="overwriteExistingJobs" value="true"/> 
     <property name="jobDetails"> 
      <list> 
       <ref bean="dailyCountJob"/> 
      </list> 
     </property> 
     <property name="triggers"> 
      <list> 
       <ref bean="dailyCountJobCronTrigger"/> 
      </list> 
     </property> 
    </bean> 

    <bean id="mySched" class="com.honorius.billing.CronTriggerScheduler" init-method="init"></bean> 

</beans> 

CurrencyAddJob.java:

package com.honorius.billing.job; 

import com.honorius.billing.dao.impl.CurrenciesDao; 
import com.honorius.billing.service.AsynchronousRunnerService; 
import com.honorius.billing.service.task.AddCurrencyTask; 
import com.honorius.billing.dao.model.Currencies; 
import com.honorius.billing.service.CurrencyService; 
import org.quartz.JobExecutionContext; 
import org.quartz.JobExecutionException; 
import org.quartz.SchedulerException; 
import org.springframework.context.ApplicationContext; 
import org.springframework.scheduling.quartz.QuartzJobBean; 

public class CurrencyAddJob extends QuartzJobBean { 

    private transient CurrenciesDao currenciesDao; 
    private transient CurrencyService currencyService; 
    private transient AsynchronousRunnerService asynchronousRunnerService; 


    @Override 
    protected void executeInternal(JobExecutionContext jobExecutionContext) 
      throws JobExecutionException { 


     // spring quartz initializes from applicationContext 
     ApplicationContext applicationContext = null; 
     try { 
      applicationContext = (ApplicationContext) jobExecutionContext 
        .getScheduler().getContext().get("applicationContext"); 
     } catch (SchedulerException e1) { 
      e1.printStackTrace(); 
     } 
     if (applicationContext != null) { 
      currenciesDao = applicationContext.getBean(CurrenciesDao.class); 
     } 
     //if (currencyService == null) { 
     // currencyService = (CurrencyService) applicationContext.getBean("currencyService"); 
     //} 
     if (currenciesDao == null) { 
      currenciesDao = (CurrenciesDao) jobExecutionContext.getJobDetail() 
        .getJobDataMap().get("CurrenciesDao"); 
     } 
     if (asynchronousRunnerService == null) { 
      assert applicationContext != null; 
      asynchronousRunnerService = (AsynchronousRunnerService) applicationContext.getBean("asynchronousRunnerService"); 
     } 


     //// TODO: INSERT TO CURRENCY TABLE 

     Currencies curr = new Currencies(); 
     //curr.setId(Long.valueOf(10)); 
     curr.setId(null); 
     curr.setIso("Suc"); 
     curr.setName("Honorius"); 


     asynchronousRunnerService.runAsynchronously(new AddCurrencyTask(currenciesDao, curr)); 
    } 
} 

のweb.xml:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

<display-name>Honorius Batch Application</display-name> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:applicationContext.xml</param-value> 
</context-param> 

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

それは5秒で完璧に、すべての作品新しいデータが挿入されていますCurrencyTableに関連付けられます。しかし、applicationContext.xmlが更新され、CronTriggerSchedler.javaが作成されると、CurrencyAddJob.javaのapplicationContext変数とasynchronousRunnerService変数がデバッグモードで「null」になります。 CurrecnyAddJob.javaが呼び出された後、アプリケーションがループでスタックします。

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns="http://www.springframework.org/schema/beans" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

    <context:annotation-config/> 

    <context:component-scan base-package="com.honorius.billing.*"> 
    </context:component-scan> 

    <context:property-placeholder location="classpath:com"/> 

<bean id="honoriusDataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
     <property name="url" value="jdbc:mysql://url address here"/> 
     <property name="username" value="root"/> 
     <property name="password" value="password"/> 
     <property name="jmxEnabled" value="true"/> 
     <property name="initialSize" value="10"/> 
     <property name="maxActive" value="2000"/> 
     <property name="maxIdle" value="500"/> 
     <property name="minIdle" value="10"/> 
     <property name="suspectTimeout" value="60"/> 
     <property name="timeBetweenEvictionRunsMillis" value="30000"/> 
     <property name="minEvictableIdleTimeMillis" value="60000"/> 
     <property name="validationQuery" value="select 1 from dual;"/> 
     <property name="testOnConnect" value="true"/> 
     <property name="testWhileIdle" value="true"/> 
    </bean> 

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> 
     <constructor-arg ref="honoriusDataSource"/> 
    </bean> 
    <bean id="mySched" class="com.honorius.billing.CronTriggerScheduler" init-method="init"></bean> 

</beans> 

CronTriggerScheduler.java:

public class CronTriggerScheduler { 

    public void init(){ 
     scheduler(); 
    } 
    public static void scheduler(){ 

     try { 
      Scheduler scheduler; 
      scheduler = new StdSchedulerFactory().getScheduler(); 
      scheduler.start(); 

      JobDetail job = JobBuilder.newJob(CurrencyAddJob.class) 
        .withIdentity("DailyJobs", "group").build(); 

      Trigger trigger = TriggerBuilder 
        .newTrigger() 
        .withIdentity("dailyJobCronTrigger", "group") 
        .withSchedule(
          CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) 
        .build(); 


      scheduler.scheduleJob(job, trigger); 
     } catch (SchedulerException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

どのように私はCronTriggerScheduler.javaを使用して、私のapplciationをスケジュールすることができ

はapplicationContext.xmlを更新します。 助けていただければ幸いです。

+0

なぜSpringのコンテキストからSchedulerFactoryBeanを削除し、CronTriggerSchedulerの中にスケジューラを手動で作成していますか? –

+0

あなたの仕事をスケジュールすることができます。回答の2番目のオプションを参照してください。http://stackoverflow.com/questions/36554345/spring-batch-rerun-from-first-step/36561038#36561038 – surya

+0

@NazaretK私はそれを削除するCronTriggerSchedulerので、私は2番目の変更したいデータベースからの入力に依存する – honorius03

答えて

0

ジョブBean(CurrencyAddJob)で依存関係注入が機能するには、スケジューラをSpringで作成する必要があります。 SchedulerFactoryBeanをxmlに保持しますが、ジョブやトリガーは使用しないでください。

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
    <property name="applicationContextSchedulerContextKey" value="applicationContext"/> 
</bean> 

次に、それをCronTriggerSchedulerに注入します。

public class CronTriggerScheduler { 

    @Autowired 
    private Scheduler scheduler; 

    public void init(){ 
     scheduleJob(); 
    } 

    private void scheduleJob() { 

     try { 

      JobDetail job = JobBuilder.newJob(CurrencyAddJob.class) 
        .withIdentity("DailyJobs", "group").build(); 

      Trigger trigger = TriggerBuilder 
        .newTrigger() 
        .withIdentity("dailyJobCronTrigger", "group") 
        .withSchedule(
          CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) 
        .build(); 

      scheduler.scheduleJob(job, trigger); 

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

注:ジョブを静的に作成するメソッドを静的にする必要はありません。

+0

"dailyCountJob"と "dailyCountJobCronTrigger"をapplicationContext.xmlで解決できません。どうすればエラーを解決できますか? – honorius03

+0

完全に取り除くだけです。プログラムでジョブを作成しているので、xmlで宣言する必要はありません。私はそれを編集します。私は "しかし、仕事やトリガーなし"と述べたが、xmlから削除するのを忘れてしまった。 –

関連する問題