我使用Quartz运行这项工作,每隔1分钟使用Spring restful web服务自动运行,但我发现了问题。
日志:
17:24:00,016 INFO [stdout] (DefaultQuartzScheduler_Worker-6) Hello Letter Printing Timer Quartz! Thu Dec 21 17:24:00 ICT 2017
17:24:00,016 INFO [stdout] (DefaultQuartzScheduler_Worker-6) ini controller testing
17:24:00,016 INFO [stdout] (DefaultQuartzScheduler_Worker-6) ----controller-----
17:24:00,016 ERROR [org.quartz.core.JobRunShell] (DefaultQuartzScheduler_Worker-6) Job DEFAULT.6da64b5bd2ee-929cc152-4d2a-4f06-8149-273dd89c6e36 threw an unhandled Exception: : java.lang.NullPointerException
at com.prudential.letter.printing.controller.ModifyFileController.modifyFileForGenerateUniqueID(ModifyFileController.java:20) [classes:]
at com.prudential.letter.printing.HelloJob.execute(HelloJob.java:20) [classes:]
at org.quartz.core.JobRunShell.run(JobRunShell.java:202) [quartz-2.2.1.jar:]
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz-2.2.1.jar:]
17:24:00,016 ERROR [org.quartz.core.ErrorLogger] (DefaultQuartzScheduler_Worker-6) Job (DEFAULT.6da64b5bd2ee-929cc152-4d2a-4f06-8149-273dd89c6e36 threw an exception.: org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.NullPointerException]
at org.quartz.core.JobRunShell.run(JobRunShell.java:213) [quartz-2.2.1.jar:]
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz-2.2.1.jar:]
Caused by: java.lang.NullPointerException
at com.prudential.letter.printing.controller.ModifyFileController.modifyFileForGenerateUniqueID(ModifyFileController.java:20) [classes:]
at com.prudential.letter.printing.HelloJob.execute(HelloJob.java:20) [classes:]
at org.quartz.core.JobRunShell.run(JobRunShell.java:202) [quartz-2.2.1.jar:]
... 1 more
我在每个控制器,服务器和dao实现中打印,但从作业直到控制器。从控制器到服务时,我发现了一个错误。
我的代码:
HelloJob.java
public class HelloJob implements Job {
TestingController testing = new TestingController();
ModifyFileController modify = new ModifyFileController();
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("Hello Letter Printing Timer Quartz! " + new Date());
testing.getData();
modify.modifyFileForGenerateUniqueID(); // to controller
}
}
我的控制员:
@RestController
@RequestMapping(value = "/")
public class ModifyFileController {
@Autowired
private FileService fileService;
@PostMapping("modify")
public String modifyFileForGenerateUniqueID() {
System.out.println("----controller-----");
return fileService.modify();
}
}
接口:
public interface FileService {
public String modify();
}
服务impl:
@Service
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class FileServiceImpl implements FileService {
private static final Logger log = LoggerFactory
.getLogger(FileServiceImpl.class);
@Autowired
private FileDao fileDao;
@Autowired
ConfigProperties configProperties;
@Override
@Transactional
public String modify() {
System.out.println("----service impl-----");
String data = configProperties.getSeparator();
String[] result = data.split(";");
String timeout = configProperties.getTimeout();
Integer i = Integer.parseInt(timeout);
return fileDao.modify(result, i);
}
}
道:
public interface FileDao {
public String modify(String[] result, Integer timeout);
}
Dao Impl:
@Repository
public class FileDaoImpl implements FileDao {
@Transactional
@Override
public String modify(String[] delimeter, Integer timeout) {
// the content here
return "Success";
}
}
Quartz Listener:
@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(HelloJob.class).build();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("simple").withSchedule(
CronScheduleBuilder.cronSchedule("0 0/1 * 1/1 * ? *")).startNow().build();
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
} catch (Exception e) {
ctx.log("There was an error scheduling the job.", e);
}
}
}
为什么正在运行的作业不去服务impl?只是停在控制器然后错误?感谢
答案 0 :(得分:0)
您不应该使用new
关键字来初始化新的控制器。
此外,使用Spring Dependency Injection。
public class HelloJob implements Job {
@Autowired
private TestingController testing;
@Autowired
private ModifyFileController modify;
// ...
}
问题是ModifyFileController
还依赖于FileService
等等。当你试图以这种方式调用它时,该对象为null。
我必须在Job中添加,我认为最好在Service
图层而不是Controller
图层中调用方法。
答案 1 :(得分:0)
您应该在心理上将工作视为与控制器处于同一应用程序级别。你永远不应该以编程方式从其他类中调用控制器方法。您应该直接与作业中的FileService进行交互。