This question already has an answer here:
- inject bean reference into a Quartz job in Spring? 20 answers
I have a java spring mvc
web application which use Spring 3.2
and Tomcat 8
.
I use quartz 2.2
to schedule a task.
My controller class is:
@Controller
public class StatusController implements Job
{
@Autowired
WebContentDefinitionService webContentDefinitionService;
public void execute(JobExecutionContext arg0) throws JobExecutionException
{
System.out.println("Starting Job");
try
{
webContentDefinitionService.deletePurgedContents();
webContentDefinitionService.moveContentsToPurged();
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("Executed Job");
}
}
and I have a QuartzInitializer class as follows:
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.annotation.WebListener;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.ee.servlet.QuartzInitializerListener;
import org.quartz.impl.StdSchedulerFactory;
@WebListener
public class QuartzListener extends QuartzInitializerListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
super.contextInitialized(sce);
ServletContext ctx = sce.getServletContext();
StdSchedulerFactory factory = (StdSchedulerFactory) ctx.getAttribute(QUARTZ_FACTORY_KEY);
try {
Scheduler scheduler = factory.getScheduler();
JobDetail jobDetail = JobBuilder.newJob(StatusController.class).build();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("simple").withSchedule(
CronScheduleBuilder.cronSchedule("0 0/5 * * * ?")).startNow().build();
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
} catch (Exception e) {
ctx.log("There was an error scheduling the job.", e);
}
}
}
If I only use the print statements in my controller class, they get printed in the console without any issue the the specified time. But when I try to call a method from any of my service classes, a null pointer exception is thrown. The methods which I call from the service classes are infact not at all getting accessed from the controller class , which I have confirmed using debug mode. And those methods are working fine elsewhere.
My porm.xml file is:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>BasicQuartz</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>BasicQuartz</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
I am not sure why I am unable to access the service class methods. Is it because I am missing some anotations
??