在带有tomcat 8的spring mvc Web应用程序中尝试使用石英2.2调度程序调用服务类方法时获取空指针异常 [英] Getting null pointer exception while trying to call a service class method using a quartz 2.2 scheduler in a spring mvc web application with tomcat 8

查看:75
本文介绍了在带有tomcat 8的spring mvc Web应用程序中尝试使用石英2.2调度程序调用服务类方法时获取空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个java spring mvc Web应用程序,该应用程序使用Spring 3.2Tomcat 8. 我使用quartz 2.2安排任务. 我的控制器类是:

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");

    }

}

,我有一个QuartzInitializer类,如下所示:

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

}

如果仅在我的控制器类中使用print语句,则它们将在控制台中打印,并且在指定的时间没有任何问题.但是,当我尝试从我的任何服务类中调用方法时,都会引发空指针异常.我从服务类中调用的方法实际上根本无法从控制器类中访问,我已经使用调试模式确认了这一点.这些方法在其他地方都可以正常工作.

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.

我的porm.xml文件是:

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>

我不确定为什么无法访问服务类方法.是因为我缺少一些anotations ??

I am not sure why I am unable to access the service class methods. Is it because I am missing some anotations??

推荐答案

尝试添加SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); 在您的

public void execute(JobExecutionContext arg0) throws JobExecutionException

这应该有效.

这篇关于在带有tomcat 8的spring mvc Web应用程序中尝试使用石英2.2调度程序调用服务类方法时获取空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆