Quartz job in Spring isn't started

I'm using Spring's SchedulerFactoryBean to schedule a job dynamically. Here is my Spring configuration:

@Configuration
public class SpringConfigBean {
    @Bean
    public DataSource dataSource() throws NamingException{
        InitialContext ctx = new InitialContext();
        DataSource source = (DataSource) ctx.lookup(dataSourceName);
        return source;
    }

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() throws NamingException {
        SchedulerFactoryBean schedulerFactoryBean = null;
        try {
            schedulerFactoryBean = new SchedulerFactoryBean();
            schedulerFactoryBean.setDataSource(dataSource());
            schedulerFactoryBean.setQuartzProperties(com.orca.integration.plugins.vs.broadpeak.admin.utils.Configuration.getQuartzProperties());
            schedulerFactoryBean.setAutoStartup(false);
        } catch (SchedulingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return schedulerFactoryBean;
    }

}

Here is my job:

@Component
public class MyJob extends QuartzJobBean {

    @Override
    public void executeInternal(JobExecutionContext context) throws JobExecutionException {
        System.out.println("Start job...");
}
}

This is where I'm dynamically scheduling the task:

@Component
public class MyManager {
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    public void myMethod(String text, Calendar startDate) {
        logger.debug("BroadpeakDistributionManager.startRecordingChannel(); Parameters: recording channel ID=" + recordingChannel.getExternalId());

//      StartRecordJob.scheduleMe(text, startDate, "Start");

        JobDetailBean jobDetailBean = new JobDetailBean();
        jobDetailBean.setJobClass(MyJob.class);
        jobDetailBean.setName("name");
        jobDetailBean.afterPropertiesSet();

        SimpleTriggerBean simpleTriggerBean = new SimpleTriggerBean();
        simpleTriggerBean.setJobDetail(jobDetailBean);
        simpleTriggerBean.setName("Name");
        simpleTriggerBean.setStartTime(startDate.getTime());
        try {
            simpleTriggerBean.afterPropertiesSet();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        schedulerFactoryBean.setTriggers(new Trigger[] {simpleTriggerBean});
        schedulerFactoryBean.start();
    }
}

The job isn't started. What am I doing wrong?

Answers


I don't have any experience with programatically configuration with Spring. So I wrote this XML Spring application context configuration integrating it with Quartz.

Take a look:

<!-- You can inject other sping beans here -->
<bean id="myJob" class="your.package.here.MyJob "/>

<!-- Define your target method -->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <property name="targetObject" ref="myJob" />
  <property name="targetMethod" value="executeInternal" />
</bean>

<!-- trigger using unix expression -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail" ref="jobDetail" />
    <!-- run every morning at 6 AM -->
    <property name="cronExpression" value="0 0 6 * * ?" />
</bean>

Aren't you missing the @EnableScheduling annotation?

Something like this :

@Configuration
@EnableScheduling
public class SpringConfigBean {
...
}

Need Your Help

How to make a reaction when a user shake's the iPhone?

iphone xcode motion

I'm creating an app where when the user shake's the iPhone an animation plays, I'm using this code:

Insert query into MYSQL database

php html mysql sql forms

I'm new to php so I've been trying to make an php page which adds info to Mysql database. But when ever I press submit on the form it directs to the php page and nothing happens. Blank page.