2012-10-03 3 views
1

Quartz를 사용하여 데이터베이스에서 데이터를 검색하는 데 도움이 필요합니다. 주 클래스의 config.xml에서 최대 절전 모드 속성을 읽고 NULL 포인터 예외가 발생하는 작업 클래스 (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(); 
    } 

} 

석영 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(); 
     } 
     } 
} 

답변

0

당신이 당신의 데이터베이스를 업데이트 만든 예약 된 작업하려고하는 경우 어떻게 스프링 작업 사용에 대한. How to stop jobs scheduled using spring task

그런 다음 당신의 데이터베이스 업데이트를 수행하여 메소드를 호출 여기

은 예입니다.

+0

데이터베이스의 데이터를 업데이트하려고하지 않습니다. 주 클래스의 연결 풀을 작업 클래스 (quartzprocess.java)에 전달하여 데이터를 검색해야한다. 답장을 보내 주셔서 감사합니다. – user1710910

+0

전체 솔루션에서 무엇을 성취하려고하는지 잘 모르겠지만 스프링 태스크를 작성하면 코드에서 작성하는 대신 작동하지 않을까요? 또한 연결을 전달하려는 경우 스프링으로도 빈으로 주입 할 수 있습니다. 네가 좋아하면 나는 예를 든다. – haju

3

Quartz 작업이 Spring에 의해 인스턴스화되지 않고 SpringContext 외부에서 실행되어 내부에서 참조하는 모든 bean이 null이 될 수 있으므로 널 포인터 예외가 발생합니다. 이제 석영 작업 내에서 스프링 빈에 액세스 할 수있는 몇 가지 방법이 있습니다.

1)의 ApplicationContext에서 아래의 빈을 정의

<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> 

아래와 같이 될 것입니다 테스트 클래스의 테스트 class.code 위의 빈 스케줄러를 얻을 :

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); 
} 

스케쥴러 빈은 평범한 방식으로 메인 클래스에 있어야합니다. 스케쥴러가 spring containter에 의해 시작되므로 scheduler.start를 수행 할 필요가 없습니다. 당신의 QuartzProcess 클래스에서

, 당신은 ApplicationContext를 얻을 방법을 아래에 추가 할 필요가 다음 quartzprocess 당신의 xecute 방법에 그런

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; 
    } 

을, 당신은 필요한 콩을 얻기 위해 코드 아래 TEH을 할 필요가

ApplicationContext ctx = getApplicationContext(context); 
    QuartzProcess quartzProcess = (QuartzProcess)ctx.getBean("quartzProcess");