我从某个时候尝试设置一个小程序,它一起使用Spring和Quartz来安排任务。我跟着其他一些类似的答案没有运气。 目前我认为我已经正确配置了,我看到没有更多的例外,但我的工作似乎没有开始。
在Spring生成的log.out中,我在最后看到以下消息:
2015-06-04T15:46:57.928 DEBUG [org.springframework.core.env.PropertySourcesPropertyResolver] 在中搜索关键'spring.liveBeansView.mbeanDomain' [systemProperties] 2015-06-04T15:46:57.929 DEBUG [org.springframework.core.env.PropertySourcesPropertyResolver] 在中搜索关键'spring.liveBeansView.mbeanDomain' [systemEnvironment] 2015-06-04T15:46:57.929 DEBUG [org.springframework.core.env.PropertySourcesPropertyResolver]可以 在任何属性中找不到键'spring.liveBeansView.mbeanDomain' 资源。返回[null]
我会告诉你我的代码......
这是我启动调度程序的类:
public class JobRunner {
public static void main(String[] args) throws SchedulerException {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(WhatsTheTimeConfiguration.class);
AutowiringSpringBeanJobFactory autowiringSpringBeanJobFactory = new AutowiringSpringBeanJobFactory();
autowiringSpringBeanJobFactory.setApplicationContext(applicationContext);
SpringBeanJobFactory springBeanJobFactory = new SpringBeanJobFactory();
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setTriggers(trigger());
schedulerFactoryBean.setJobFactory(springBeanJobFactory);
schedulerFactoryBean.start();
}
private static SimpleTrigger trigger() {
return newTrigger()
.withIdentity("whatsTheTimeJobTrigger", "jobsGroup1")
.startNow()
.withSchedule(simpleSchedule()
.withIntervalInSeconds(1)
.repeatForever())
.build();
}
}
我想提一下,如果我使用方法schedulerFactoryBean.getScheduler()。start(),它会在调度程序上抛出一个空指针异常,这就是为什么我在工厂调用start()。
AutowiringSpringBeanJobFactory类是从stackoverflow中的另一个答案复制粘贴的。我决定这样做,因为我找到的所有其他答案只是通过xml进行配置而我不想使用xml。
public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements
ApplicationContextAware {
private transient AutowireCapableBeanFactory beanFactory;
@Override
public void setApplicationContext(final ApplicationContext context) {
beanFactory = context.getAutowireCapableBeanFactory();
}
@Override
protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
final Object job = super.createJobInstance(bundle);
beanFactory.autowireBean(job);
return job;
}
}
这是代表我想要触发的Job的类:
@Component
public class WhatsTheTimeManager extends QuartzJobBean {
@Autowired
private WhatsTheTime usecase;
@Autowired
private LocationRetriever locationDataProvider;
public WhatsTheTimeManager() {
}
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
usecase.tellMeWhatsTheTimeIn(locationDataProvider.allLocations());
}
public void setUsecase(WhatsTheTime usecase) {
this.usecase = usecase;
}
public void setLocationDataProvider(LocationRetriever locationDataProvider) {
this.locationDataProvider = locationDataProvider;
}
}
My Spring配置正在进行组件扫描,非常简单:
@Configuration
@ComponentScan(basePackages = "com.springpractice")
public class WhatsTheTimeConfiguration {
}
从这一点来说,我拥有的只是一些接口,组件和域对象,但我也会粘贴它们,以防万一我忘了:
public interface LocationRetriever {
List<String> allLocations();
}
public interface TimeOutputRenderer {
TimeReport renderReport(String timeInLocation, String location);
}
public interface TimeRetriever {
String timeFor(String location);
}
@Component
public class LocationRetrieverDataProvider implements LocationRetriever{
public LocationRetrieverDataProvider() {
}
@Override
public List<String> allLocations() {
return asList("Europe/London", "Europe/Madrid", "Europe/Moscow", "Asia/Tokyo", "Australia/Melbourne", "America/New_York");
}
}
@Component
public class TimeOutputRendererDataProvider implements TimeOutputRenderer {
public TimeOutputRendererDataProvider() {
}
@Override
public TimeReport renderReport(String location, String time) {
System.out.println(location + " time is " + time);
return new TimeReport(location, time);
}
}
@Component
public class TimeRetrieverDataProvider implements TimeRetriever {
public TimeRetrieverDataProvider() {
}
@Override
public String timeFor(String location) {
SimpleDateFormat timeInLocation = new SimpleDateFormat("dd-M-yyyy hh:mm:ss a");
timeInLocation.setTimeZone(TimeZone.getTimeZone(location));
return timeInLocation.format(new Date());
}
}
最后一个细节,可能是有意义的。 我在我的库中使用的版本如下:
quartz 2.2.1
春季4.1.6.RELEASE
当我运行应用程序时,我希望每秒打印这些国家/地区的时间,但不会发生。
如果你想克隆代码并亲自尝试看看,你可以在这个git repo找到它(如果你愿意,可以随意分叉):https://github.com/SFRJ/cleanarchitecture
答案 0 :(得分:3)
代码中的主要错误是你不让Spring处理你的日程安排。
虽然你可以在代码中使用Quartz作为任何其他代码,但与Spring集成的想法是告诉Spring你想要完成的工作,让Spring为你做艰苦的工作。
为了允许Spring运行Quartz调度,您需要将Job,JobDetail和Trigger声明为Bean。
Spring只处理Bean,如果它们是通过Spring生命周期创建的(即使用注释或XML),而不是如果对象是用带有new
语句的代码创建的。
以下代码需要从JobRunner.java
中删除:
SpringBeanJobFactory springBeanJobFactory = new SpringBeanJobFactory();
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setTriggers(trigger());
schedulerFactoryBean.setJobFactory(springBeanJobFactory);
schedulerFactoryBean.start();
...
private static SimpleTrigger trigger() {
return newTrigger()
.withIdentity("whatsTheTimeJobTrigger", "jobsGroup1")
.startNow()
.withSchedule(simpleSchedule()
.withIntervalInSeconds(1)
.repeatForever())
.build();
}
该代码必须重新写入WhatsTheTimeConfiguration.java
,以下是它现在的样子:
@Configuration
@ComponentScan(basePackages = "com.djordje.cleanarchitecture")
public class WhatsTheTimeConfiguration {
@Bean
public SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setTriggers(trigger());
schedulerFactoryBean.setJobDetails(jobDetail());
schedulerFactoryBean.setJobFactory(springBeanJobFactory());
return schedulerFactoryBean;
}
@Bean
public SpringBeanJobFactory springBeanJobFactory() {
return new AutowiringSpringBeanJobFactory();
}
@Bean
public JobDetail jobDetail() {
JobDetailImpl jobDetail = new JobDetailImpl();
jobDetail.setKey(new JobKey("WhatsTheTime"));
jobDetail.setJobClass(WhatsTheTimeManager.class);
jobDetail.setDurability(true);
return jobDetail;
}
@Bean
public SimpleTrigger trigger() {
return newTrigger()
.forJob(jobDetail())
.withIdentity("whatsTheTimeJobTrigger", "jobsGroup1")
.startNow()
.withSchedule(simpleSchedule()
.withIntervalInSeconds(1)
.repeatForever())
.build();
}
}
SchedulerFactoryBean
现在是一个Bean,将由Spring处理和初始化,SimpleTrigger
和AutowiringSpringBeanJobFactory
也是如此。
我添加了缺少的JobDetail
类,但缺少了SimpleTrigger
和SchedulerFactoryBean
所需的连线。他们都需要了解JobDetail
,这是唯一知道哪个类是需要触发的工作类的地方。