ApplicationContext和Spring批处理的问题

发布于 2024-12-05 10:05:09 字数 3264 浏览 1 评论 0原文

我正在使用 Spring Batch,我已经完成了批处理作业,并配置了 xml 文件, 我还将所有 Quartz 配置放入该 xml 文件中(触发器、schedulerFactoryBean 和 jobDetail);这是一个java项目,我正在尝试加载应用程序上下文,作为主类中的独立项目;据文档所述,这应该使 Quartz 开始运行并正在执行此操作,问题是当作业使用触发器运行并调用服务时,就像所有 Autowired beans 尚未加载一样,所以给出我遇到了 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 context:

 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>

i'm working with Spring batch, i've done the batch job, configured with an xml file,
i also put all the Quartz configuration in that xml file, (the trigger, schedulerFactoryBean and jobDetail); this is a java project, and i'm trying to load the application context, as an stand alone in a main class; as far as the documentation says, this should make Quartz to start running and is doing it, the problem is when the job runs with the trigger and calls the service, is like all the Autowired beans hadn’t had been loaded, so is giving me an NullpointerException…
this is the code that the job calls after the trigger is fired, and when the JobParametersBuilder is created is when everything crash, Quartz still running though...

could someone helpme with this?

//class called by the job

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

simple main method calling App context:

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

          }

error line:

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

this are the Quartz beans on the xml file:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

血之狂魔 2024-12-12 10:05:09

尝试使用 org.springframework.scheduling.quartz.JobDetailBean 和 jobDataAsMap 来获取作业类 DI

Ex:

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

Try org.springframework.scheduling.quartz.JobDetailBean along with jobDataAsMap for job class DI

Ex:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文