我正在使用quartz来安排作业,为此我在初始化上下文时正在对数据库执行数据库选择操作,但是它给出了空指针异常。 在调试时,它不会在JobSchedulingDAOImpl类中进行选择查询。 如何解决此问题,以便它将执行选择操作而不是给出空指针异常。
注意:如果我没有提供 jobsList = jobSchedulingDAO.getJobsList(); 我没有得到空指针异常。它不会进入getJobsList()方法。
记录详细信息
Launching defaultServer (WebSphere Application Server 8.5.5.7/wlp-1.0.10.cl50720150827-0437) on Java HotSpot(TM) 64-Bit Server VM, version 1.8.0_60-b27 (en_US)
[AUDIT ] CWWKE0001I: The server defaultServer has been launched.
[AUDIT ] CWWKE0100I: This product is licensed for development, and limited production use. The full license terms can be viewed here: https://public.dhe.ibm.com/ibmdl/export/pub/software/websphere/wasdev/license/base_ilan/ilan/8.5.5.7/lafiles/en.html
[ERROR ] CWWKF0001E: A feature definition could not be found for servlet-3.1.0
[AUDIT ] CWWKZ0058I: Monitoring dropins for applications.
[WARNING ] SRVE9967W: The manifest class path slf4j-api-1.7.7.jar can not be found in jar file file:/C:/Users/diwakar_b/.m2/repository/org/ehcache/ehcache/3.1.1/ehcache-3.1.1.jar or its parent.
[WARNING ] SRVE9967W: The manifest class path slf4j-api-1.7.7.jar can not be found in jar file file:/C:/Users/diwakar_b/.m2/repository/org/ehcache/ehcache/3.1.1/ehcache-3.1.1.jar or its parent.
[AUDIT ] CWWKT0016I: Web application available (default_host): http://localhost:9080/ne/
[AUDIT ] CWWKZ0001I: Application NormalizationEngine_ROAR-Dev started in 4.426 seconds.
[AUDIT ] CWWKF0012I: The server installed the following features: [jsp-2.3, ejbLite-3.2, servlet-3.1, jndi-1.0, json-1.0, localConnector-1.0, jdbc-4.1, jaxrs-2.0, jaxrsClient-2.0, el-3.0].
[AUDIT ] CWWKF0011I: The server defaultServer is ready to run a smarter planet.
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
INFO : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Tue Sep 06 15:43:30 IST 2016]; root of context hierarchy
INFO : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Registering annotated classes: [class com.hcl.ne.init.RootConfig,class com.hcl.ne.init.SecurityConfig]
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Overriding bean definition for bean 'requestDataValueProcessor' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.security.config.annotation.web.configuration.WebMvcSecurityConfiguration; factoryMethodName=requestDataValueProcessor; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebMvcSecurityConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.security.config.annotation.web.servlet.configuration.WebMvcSecurityConfiguration; factoryMethodName=requestDataValueProcessor; initMethodName=null; destroyMethodName=(inferred); defined in org.springframework.security.config.annotation.web.servlet.configuration.WebMvcSecurityConfiguration]
[err] ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/approve]}" onto public org.springframework.web.servlet.ModelAndView com.hcl.ne.controller.ApproveController.getViewHistory(org.springframework.web.servlet.ModelAndView,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,javax.servlet.http.HttpSession)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/getStatus]}" onto public java.util.List<com.hcl.ne.model.DropdownMenu> com.hcl.ne.controller.CompanyController.getStatus()
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/company]}" onto public org.springframework.web.servlet.ModelAndView com.hcl.ne.controller.CompanyController.listCompany(org.springframework.web.servlet.ModelAndView,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,com.hcl.ne.model.Company,java.lang.String) throws java.io.IOException,com.hcl.ne.service.util.AppException
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/newCompany],methods=[GET]}" onto public org.springframework.web.servlet.ModelAndView com.hcl.ne.controller.CompanyController.newCompany(org.springframework.web.servlet.ModelAndView,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/company],methods=[POST],params=[saveCompany]}" onto public org.springframework.web.servlet.ModelAndView com.hcl.ne.controller.CompanyController.saveCompany(com.hcl.ne.model.Company,org.springframework.validation.BindingResult,javax.servlet.http.HttpServletRequest) throws com.hcl.ne.service.util.AppException
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/editCompany],methods=[GET]}" onto public com.hcl.ne.model.Company com.hcl.ne.controller.CompanyController.editCompany(com.hcl.ne.model.Company,org.springframework.validation.BindingResult,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.lang.Exception
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/getDropdownVaues]}" onto public java.util.Map<java.lang.String, java.util.List<com.hcl.ne.model.DropdownMenu>> com.hcl.ne.controller.CompanyController.getDropdownValues()
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/configure]}" onto public org.springframework.web.servlet.ModelAndView com.hcl.ne.controller.ConfigureController.getConfigureList(org.springframework.web.servlet.ModelAndView,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,javax.servlet.http.HttpSession)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/getstate]}" onto public java.util.List<com.hcl.ne.model.DropdownMenu> com.hcl.ne.controller.LocationController.getState(int)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/newLocation],methods=[GET]}" onto public org.springframework.web.servlet.ModelAndView com.hcl.ne.controller.LocationController.newLocation(org.springframework.web.servlet.ModelAndView)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/location]}" onto public org.springframework.web.servlet.ModelAndView com.hcl.ne.controller.LocationController.listLocation(org.springframework.web.servlet.ModelAndView,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,com.hcl.ne.model.Location,java.lang.String) throws java.io.IOException,com.hcl.ne.service.util.AppException
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/getcity]}" onto public java.util.List<com.hcl.ne.model.DropdownMenu> com.hcl.ne.controller.LocationController.getCity(int)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/location],methods=[POST],params=[saveLocation]}" onto public org.springframework.web.servlet.ModelAndView com.hcl.ne.controller.LocationController.saveLocation(com.hcl.ne.model.Location,org.springframework.validation.BindingResult,javax.servlet.http.HttpServletRequest) throws com.hcl.ne.service.util.AppException
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/editLocation],methods=[GET]}" onto public com.hcl.ne.model.Location com.hcl.ne.controller.LocationController.editLocation(com.hcl.ne.model.Location,org.springframework.validation.BindingResult,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.lang.Exception
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/login],methods=[GET]}" onto public java.lang.String com.hcl.ne.controller.LoginController.loginPage()
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/normalize]}" onto public org.springframework.web.servlet.ModelAndView com.hcl.ne.controller.NormalizeController.getViewHistory(org.springframework.web.servlet.ModelAndView,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,javax.servlet.http.HttpSession)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/parameters]}" onto public org.springframework.web.servlet.ModelAndView com.hcl.ne.controller.ParametersController.getParametersList(org.springframework.web.servlet.ModelAndView,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,com.hcl.ne.model.Parameters)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/parameters],methods=[POST]}" onto public org.springframework.web.servlet.ModelAndView com.hcl.ne.controller.ParametersController.saveOrUpdateParameters(com.hcl.ne.model.Parameters,org.springframework.validation.BindingResult,javax.servlet.http.HttpServletRequest) throws com.hcl.ne.service.util.AppException
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/productMapping]}" onto public org.springframework.web.servlet.ModelAndView com.hcl.ne.controller.ProductMappingController.listCompany(org.springframework.web.servlet.ModelAndView,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,com.hcl.ne.model.Company,java.lang.String,javax.servlet.http.HttpSession) throws java.io.IOException,com.hcl.ne.service.util.AppException
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/rulesConfig]}" onto public org.springframework.web.servlet.ModelAndView com.hcl.ne.controller.RulesConfigController.rulesConfig(org.springframework.web.servlet.ModelAndView,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,javax.servlet.http.HttpSession)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/console],methods=[GET]}" onto public org.springframework.web.servlet.ModelAndView com.hcl.ne.controller.UserController.console(org.springframework.web.servlet.ModelAndView,javax.servlet.http.HttpSession)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/]}" onto public org.springframework.web.servlet.ModelAndView com.hcl.ne.controller.UserController.list(org.springframework.web.servlet.ModelAndView,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpSession) throws com.hcl.ne.service.util.AppException
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/getTimeZone]}" onto public com.hcl.ne.model.User com.hcl.ne.controller.UserController.getTimeZone(javax.servlet.http.HttpServletRequest) throws java.io.IOException
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/getLocation]}" onto public com.hcl.ne.model.AjaxResponseBody com.hcl.ne.controller.UserController.getLocationList(javax.servlet.http.HttpServletRequest) throws java.io.IOException
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/user]}" onto public org.springframework.web.servlet.ModelAndView com.hcl.ne.controller.UserController.listCompany(org.springframework.web.servlet.ModelAndView,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,com.hcl.ne.model.User,java.lang.String) throws java.io.IOException,com.hcl.ne.service.util.AppException
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/addUser]}" onto public com.hcl.ne.model.AjaxResponseBody com.hcl.ne.controller.UserController.getDropdownList(javax.servlet.http.HttpServletRequest) throws java.io.IOException
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/appConfiguration],methods=[GET]}" onto public org.springframework.web.servlet.ModelAndView com.hcl.ne.controller.UserController.appConfiguration(org.springframework.web.servlet.ModelAndView,javax.servlet.http.HttpSession)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/administration],methods=[GET]}" onto public org.springframework.web.servlet.ModelAndView com.hcl.ne.controller.UserController.administration(org.springframework.web.servlet.ModelAndView,javax.servlet.http.HttpSession)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/reports],methods=[GET]}" onto public org.springframework.web.servlet.ModelAndView com.hcl.ne.controller.UserController.reports(org.springframework.web.servlet.ModelAndView,javax.servlet.http.HttpSession)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/logout],methods=[GET]}" onto public java.lang.String com.hcl.ne.controller.UserController.logoutPage(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/addUser],methods=[POST]}" onto public java.lang.String com.hcl.ne.controller.UserController.addUser(javax.servlet.http.HttpServletRequest,com.hcl.ne.model.User,org.springframework.validation.BindingResult)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/editUser],methods=[GET]}" onto public com.hcl.ne.model.User com.hcl.ne.controller.UserController.editUser(com.hcl.ne.model.User,org.springframework.validation.BindingResult,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.lang.Exception
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/viewHistory]}" onto public org.springframework.web.servlet.ModelAndView com.hcl.ne.controller.ViewHisoryController.getViewHistory(org.springframework.web.servlet.ModelAndView,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,javax.servlet.http.HttpSession)
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Tue Sep 06 15:43:30 IST 2016]; root of context hierarchy
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 5256 ms
Context Initialized
[ERROR ] SRVE0283E: Exception caught while initializing context: java.lang.NullPointerException
at com.hcl.ne.jobs.QuartzListener.contextInitialized(QuartzListener.java:42)
at com.ibm.ws.webcontainer.webapp.WebApp.notifyServletContextCreated(WebApp.java:2389)
at [internal classes]
QuartzListener
package com.hcl.ne.jobs;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Controller;
import com.hcl.ne.dao.JobSchedulingDAO;
import com.hcl.ne.model.Jobs;
/**
* @author diwakar_b
*
*/
public class QuartzListener implements ServletContextListener{
Scheduler scheduler = null;
@Autowired
private JobSchedulingDAO jobSchedulingDAO;
List<Jobs> jobsList = null;
@Override
public void contextInitialized(ServletContextEvent context) {
System.out.println("Context Initialized");
try {
jobsList =jobSchedulingDAO.getJobsList();
Iterator<Jobs> it = jobsList.iterator();
if(it.hasNext()) {
Jobs job = it.next();
int jobScheduleTime = job.getJobScheduleTime();
String jobName = job.getJobName();
System.out.println(jobScheduleTime);
System.out.println(jobName);
JobDetail job1 = (JobDetail) JobBuilder.newJob(Job1.class).withIdentity("firstJob", "group1").build();
}
JobDetail job1 = (JobDetail) JobBuilder.newJob(Job1.class).withIdentity("firstJob", "group1").build();
Trigger trigger1 = (Trigger) TriggerBuilder.newTrigger().withIdentity("firstTrigger", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * * * ? *"))
.build();
JobDetail job2 = (JobDetail) JobBuilder.newJob(Job2.class).withIdentity("firstJob", "group2").build();
Trigger trigger2 = (Trigger) TriggerBuilder.newTrigger().withIdentity("secondTrigger", "group2")
.withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * * * ? *"))
.build();
// schedule it
scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job1, trigger1);
scheduler.scheduleJob(job2, trigger2);
} catch (SchedulerException e) {
e.printStackTrace();
}
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("Context Destroyed");
try {
scheduler.shutdown();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class>com.hcl.ne.jobs.QuartzListener</listener-class>
</listener>
</web-app>
JbSchedulingDAO
/**
*
*/
package com.hcl.ne.dao;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.hcl.ne.model.Jobs;
/**
* @author diwakar_b
*
*/
@Repository
public interface JobSchedulingDAO {
public List<Jobs> getJobsList();
}
JobSchedulingDAOImpl
/**
*
*/
package com.hcl.ne.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import com.hcl.ne.model.Jobs;
import com.hcl.ne.service.logging.AppLogger;
/**
* @author diwakar_b
*
*/
public class JobSchedulingDAOImpl implements JobSchedulingDAO {
public JobSchedulingDAOImpl(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
private final static AppLogger LOG = AppLogger.getLogger();
private JdbcTemplate jdbcTemplate;
private List<Jobs> jobsList;
@Override
public List<Jobs> getJobsList() {
String sql = "select * from ne_job_scheduling";
LOG.info("Select Performed");
jobsList = jdbcTemplate.query(sql, new RowMapper<Jobs>() {
@Override
public Jobs mapRow(ResultSet rs, int rowNum) throws SQLException {
Jobs jobs = new Jobs();
jobs.setJobId(rs.getInt("jobId"));
jobs.setJobDetails(rs.getString("jobDetails"));
jobs.setJobId(rs.getInt("jobName"));
jobs.setJobId(rs.getInt("jobScheduleTime"));
return jobs;
}
});
return jobsList;
}}
更新了QuartzListenerClass
public class QuartzListener {
@Autowired
private JobSchedulingDAO jobSchedulingDAO;
List<Jobs> jobsList = null;
Scheduler scheduler = null;
@PostConstruct
public void initIt() {
System.out.println("Init method after properties are set : ") ;
try {
jobsList =jobSchedulingDAO.getJobsList();
Iterator<Jobs> it = jobsList.iterator();
if(it.hasNext()) {
Jobs job = it.next();
int jobScheduleTime = job.getJobScheduleTime();
String jobName = job.getJobName();
System.out.println(jobScheduleTime);
System.out.println(jobName);
JobDetail job1 = (JobDetail) JobBuilder.newJob(Job1.class).withIdentity("firstJob", "group1").build();
}
JobDetail job1 = (JobDetail) JobBuilder.newJob(Job1.class).withIdentity("firstJob", "group1").build();
Trigger trigger1 = (Trigger) TriggerBuilder.newTrigger().withIdentity("firstTrigger", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * * * ? *"))
.build();
JobDetail job2 = (JobDetail) JobBuilder.newJob(Job2.class).withIdentity("firstJob", "group2").build();
Trigger trigger2 = (Trigger) TriggerBuilder.newTrigger().withIdentity("secondTrigger", "group2")
.withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * * * ? *"))
.build();
// schedule it
scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job1, trigger1);
scheduler.scheduleJob(job2, trigger2);
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
您的QuartzListener不是由Spring实例化的,而是由容器实例化的。
这就是为什么@Autowired没有被解释为&#39;
当执行contextInitialized方法时,没有注入dao:NullPointerException。
您应该将QuartzListener中的代码移动到spring bean中并在@PostContruct方法中调用它