开发者

problems with ApplicationContext and Spring batch

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&g开发者_如何学Ct;
        </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>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜