1

Quartz を使用してデータベースからデータを取得する際に助けが必要です。メイン クラスの config.xml から休止状態のプロパティを読み込んでおり、それらのプロパティを使用して、Null Pointer Exception を取得しているジョブ クラス (Quartz Process.java) からデータを取得しようとしました。

問題を解決するために私を助けてください。感謝と前進

これは私のメインクラスです:

@Component("TestProgram")

public class TestProgram
{
        static ClassPathXmlApplicationContext applicationContext=null;

    public void testMethod() throws SchedulerException
    {
        JobDetail job = new JobDetail();
        job.setName("Retriving The Master Details");
        job.setJobClass(QuartzProcess.class);


        SimpleTrigger trigger = new SimpleTrigger();
        trigger.setName("Trigger For Retriving The Master Details");
        trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
        trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
        trigger.setRepeatInterval(5000);

        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
        scheduler.scheduleJob(job, trigger);
    }

    public static void main(String[] args) throws Exception
    {
        String conf[] = {"Config.xml"};
        applicationContext= new ClassPathXmlApplicationContext(conf);
        TestProgram unittest=applicationContext.getBean(TestProgram.class);     
        unittest.testMethod();
    }

}

Quartz Process.java

@Component("QuartzProcess")

public class QuartzProcess implements Job
{
    @Autowired
    private MasterService MasterService;


    @Override   
        public void execute(JobExecutionContext jec) throws JobExecutionException
        {
         try
         {


             List<MasterVO> MasterVO=MasterService.findAll();
             System.out.println("MasterVO..."+MasterVO);
             for(int index=0;index<MasterVO.size();index++)
                 System.out.println(MasterVO.get(index));



         }
         catch(Exception e)
         {
             e.printStackTrace();
         }
        }   
}
4

2 に答える 2

3

Quartz ジョブは Spring によってインスタンス化されておらず、springContext の外部で実行されているため、Null ポインター例外が発生しているため、内部で参照しているすべての Bean が null になります。現在、クォーツ ジョブ内の春の豆にアクセスする方法がいくつかあります。

1) applicationContext で以下の Bean を定義します。

<bean id="scheduler"class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="configLocation">
     <value>classpath:quartz.properties</value>
  </property>
  <property name="applicationContextSchedulerContextKey">
    <value>applicationContext</value>
  </property>

テスト クラスで上記の Bean スケジューラを取得します。テスト クラスのコードは次のようになります。

public void testMethod() throws SchedulerException
{
    JobDetail job = new JobDetail();
    job.setName("Retriving The Master Details");
    job.setJobClass(QuartzProcess.class);


    SimpleTrigger trigger = new SimpleTrigger();
    trigger.setName("Trigger For Retriving The Master Details");
    trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
    trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
    trigger.setRepeatInterval(5000);


    scheduler.scheduleJob(job, trigger);
}

必要なスケジューラ Bean を通常の方法でメイン クラスに入れます。スケジューラはSpringコンテナによって開始されるため、scheduler.startを実行する必要はありません。

QuartzProcess クラスでは、以下のメソッドを追加して applicationContext を取得する必要があります。

    public ApplicationContext getApplicationContext(JobExecutionContext context) throws Exception {
        ApplicationContext applicationContext = null;
        applicationContext = (ApplicationContext) context.getScheduler().getContext().get(APPLICATION_CONTEXT_KEY);
        if (applicationContext == null) {
            throw new JobExecutionException("No application context available in scheduler context for key \""
                                            + APPLICATION_CONTEXT_KEY
                                            + "\"");
        }
        return applicationContext;
    }

次に、quartzprocess の xecute メソッドで、必要な Bean を取得するために以下のコードを実行する必要があります

  ApplicationContext ctx = getApplicationContext(context);
  QuartzProcess quartzProcess = (QuartzProcess)ctx.getBean("quartzProcess");
于 2012-10-03T15:31:00.333 に答える
0

データベースを更新するスケジュールされたジョブを作成しようとしている場合は、SpringTaskを使用してみてください。

次に例を示します。 春のタスクを使用してスケジュールされたジョブを停止する方法

次に、データベースの更新を実行するメソッドを呼び出すだけです。

于 2012-10-03T11:23:17.713 に答える