gpt4 book ai didi

java - Quarz 计划作业抛出一些错误

转载 作者:行者123 更新时间:2023-11-29 05:09:00 25 4
gpt4 key购买 nike

我在使用 Quarz 在基于 Spring 的 Java 应用程序中安排作业时遇到问题。这是我的自定义作业:

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.package.UserService;

@Component
public class ConnectionIntervalJob implements Job {

@Autowired
private UserService userService;

public void execute(JobExecutionContext jExeCtx) throws JobExecutionException {
userService.calculateDelta();
}
}

我的 ConnectionScheduler 类:

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class ConnectionScheduler {
public ConnectionScheduler() throws Exception {
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
JobDetail job = JobBuilder
.newJob(com.package.ConnectionIntervalJob.class)
.withIdentity("job1" + (int)Math.floor(Math.random()*10) + 3 , "group1" +(int)Math.floor(Math.random()*10) + 3).build();

Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("trigger" +(int)Math.floor(Math.random()*10) + 3, "group1" +(int)Math.floor(Math.random()*10) + 3)
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(20).withRepeatCount(5))
.build();
try {
scheduler.start();
scheduler.scheduleJob(job, trigger);
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}

当我使用 Apache Tomcat 7 构建我的项目时。控制台记录了一些错误:

ERROR org.quartz.core.JobRunShell - Job group163.job163 threw an unhandled   Exception: 
java.lang.NullPointerException
at com.package.jobs.ConnectionIntervalJob.execute(ConnectionIntervalJob.java:18)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
[DefaultQuartzScheduler_Worker-1] ERROR org.quartz.core.ErrorLogger - Job (group163.job163 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)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: java.lang.NullPointerException
at com.pakage.jobs.ConnectionIntervalJob.execute(ConnectionIntervalJob.java:18)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
... 1 more

我想我搞砸了作业名称和触发器名称。因此,我决定生成一个随机 int 值并将其添加到组名和触发器名称中,但它没有用。我使用以下链接作为引用: quarz doc

更新:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<!-- Database Configuration -->
<import resource="DataSource.xml" />
<import resource="hibernate.xml" />
<bean class="com.package7.config.WebSocketConfig" />

<!-- Auto scan the components -->
<context:annotation-config />
<context:component-scan base-package="com.package1.entities" />
<context:component-scan base-package="com.package2.ving.controllers" />
<context:component-scan base-package="com.package3.dao" />
<context:component-scan base-package="com.package4.daoimpl" />
<context:component-scan base-package="com.package5.serviceimpl" />
<context:component-scan base-package="com.package6.jobs" />

感谢任何帮助!谢谢!

最佳答案

你得到 NPE 很可能是因为 quartz 作业不是由 spring 创建的,所以注入(inject)不会工作。

尝试将以下内容作为 execute() 方法中的第一行。

SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

这将确保将依赖项注入(inject)到作业中。

编辑您的另一个选择是使用 Spring-quartz集成而不是使用纯 quartz 。

关于java - Quarz 计划作业抛出一些错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29364277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com