ApplicationContext和Spring批处理的问题

时间:2011-09-19 22:12:50

标签: quartz-scheduler spring-batch

我正在使用Spring批处理,我已完成批处理作业,配置了xml文件, 我还将所有Quartz配置放在那个xml文件中(触发器,schedulerFactoryBean和jobDetail);这是一个java项目,我正在尝试加载应用程序上下文,作为一个独立的主类;据文档说,这应该使Quartz开始运行并且正在这样做,问题是当作业使用触发器运行并调用服务时,就像所有Autowired bean都没有被加载一样,所以给出了我是一个NullpointerException ...  这是触发器触发后作业调用的代码,当创建JobParametersBuilder时,一切都崩溃了,Quartz仍在运行......

有人可以帮忙吗?

//作业调用的类

public class MainJobClass {

    private static Logger log = Logger.getLogger(MainJobClass.class);

    @Autowired
    private SimpleJobLauncher launcher;
    @Autowired
    private Job job;

    public void executeJob(){

        try{

            log.info("***** Staring job......");

            JobParametersBuilder builder = new JobParametersBuilder();
            builder.addDate("date", new Date());
            builder.addString("sendEmailJob", "Send email to approvers");
            JobParameters parameters = builder.toJobParameters();

            launcher.run(job, parameters);


        }catch(Exception e){
            log.error("Error on executing job"+e.fillInStackTrace());
        }
    }

    public void setLauncher(SimpleJobLauncher launcher) {
        this.launcher = launcher;
    }

    public void setJob(Job job) {
        this.job = job;
    }

调用App上下文的简单主方法:

 public static void main(String[] args){      
ApplicationContext context  = new ClassPathXmlApplicationContext("/com/ge/grt/email/grt_email_send.xml");

          }

错误行:

INFO [DefaultQuartzScheduler_Worker-1] (MainJobClass.java:29) - ***** Staring job......
ERROR [DefaultQuartzScheduler_Worker-1] (MainJobClass.java:40) - Error on executing jobjava.lang.NullPointerException

这是xml文件中的Quartz bean:

<!-- Scheudler Factory bean, the job will run when the context is loaded -->
    <bean id="schedulerFactoryBean"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="beanTrigger"></ref>
            </list>
        </property>
    </bean>

    <!-- definition of the trigger -->
    <!-- defining the execution date: (once every week on monday at 8:00 AM) -->
    <bean id="beanTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="jobDetail" />
         <property name="misfireInstructionName" value="MISFIRE_INSTRUCTION_FIRE_ONCE_NOW"/> 
<!--         <property name="cronExpression" value="0 0 8 ? * MON" /> -->
        <property name="cronExpression" value="0 0/1 * * * ?" />
    </bean>


<!-- definiton of job detail bean -->
    <bean id="jobDetail"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="mainJobClass" />
        <property name="targetMethod" value="executeJob" />
        <property name="concurrent" value="false"></property>
    </bean>

1 个答案:

答案 0 :(得分:0)

尝试org.springframework.scheduling.quartz.JobDetailBean以及作业类DI的jobDataAsMap

例如:

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html#scheduling-quartz-jobdetail