gpt4 book ai didi

java - Spring 3\Quartz 2.集成问题

转载 作者:太空宇宙 更新时间:2023-11-04 14:59:13 25 4
gpt4 key购买 nike

我在集成 Spring 和 quartz 方面遇到问题。我需要将 CronTriggerFactoryBean 动态添加到 SchedulerFactoryBean

XML Spring 映射:

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" scope="prototype">
<property name="jobDetails">
<list>
<ref bean="plannedVacationServiceJob" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="plannedVacationServiceCronTrigger" />
</list>
</property>
</bean>

<bean id="plannedVacationServiceJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.my.service.package.PlannerJob" />
<property name="durability" value="true" />
</bean>

<bean id="plannedVacationServiceCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean" scope="prototype">
<property name="jobDetail" ref="plannedVacationServiceJob" />
<property name="cronExpression" value="*/15 * * * * ?" />
</bean>

Java 代码:

@Service
public class Planner implements Planning {
@Autowired
@Qualifier("plannedVacationServiceCronTrigger")
private CronTriggerFactoryBean plannedVacationServiceCronTrigger;

@Autowired
private SchedulerFactoryBean schedulerFactoryBean;

@PostConstruct
public void init() {
schedulerFactoryBean.start();
}
//some code
private void addTask(PlannerEntity entity) {
try {
String name = getIdentityName(entity);
JobKey jobKey = new JobKey(name);
String cronExpression = getCronExpression(entity);
plannedVacationServiceCronTrigger.setCronExpression(cronExpression);
JobDetail jobDetail = JobBuilder.newJob(PlannerJob.class).withIdentity(jobKey).build();
plannedVacationServiceCronTrigger.setJobDetail(jobDetail);
plannedVacationServiceCronTrigger.setName(name);
plannedVacationServiceCronTrigger.afterPropertiesSet();
schedulerFactoryBean.getScheduler().scheduleJob(jobDetail, plannedVacationServiceCronTrigger.getObject());
triggers.put(entity.getId(), trigger);
} catch (Exception e) {
e.printStackTrace();
}
}
}

在本例中,我们只有一个触发器,它是在 xml 映射中添加的

我尝试从 xml 中删除对触发器的引用并从代码创建 CronTriggerFactoryBean:

private void addTask(PlannerEntity entity) {
try {
String name = getIdentityName(entity);
JobKey jobKey = new JobKey(name);
String cronExpression = getCronExpression(entity);
CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
trigger.setCronExpression(cronExpression);
JobDetail jobDetail = JobBuilder.newJob(PlannerJob.class).withIdentity(jobKey).build();
trigger.setJobDetail(jobDetail);
trigger.setName(name);
trigger.afterPropertiesSet();
schedulerFactoryBean.getScheduler().scheduleJob(jobDetail, trigger.getObject());
triggers.put(entity.getId(), trigger);
} catch (Exception e) {
e.printStackTrace();
}
}

但在这种情况下我们没有任何触发器:

2014-04-02 14:28:55,144 DEBUG [org.quartz.core.QuartzSchedulerThread(org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread)] - batch acquisition of 0 triggers
2014-04-02 14:29:18,981 DEBUG [org.quartz.core.QuartzSchedulerThread(org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread)] - batch acquisition of 0 triggers
2014-04-02 14:29:46,361 DEBUG [org.quartz.core.QuartzSchedulerThread(org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread)] - batch acquisition of 0 triggers
2014-04-02 14:30:09,439 DEBUG [org.quartz.core.QuartzSchedulerThread(org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread)] - batch acquisition of 0 triggers

最佳答案

你不能真的那样做。 FactoryBean 有一个特定的契约,而您正试图在其中执行操作。

一个factory bean是一个 bean,其目的是以编程方式创建一个 bean。您无法真正注入(inject)工厂 bean,因为它存在的唯一目的是注册 bean。此外,start回调与应用程序上下文生命周期相关。当您从代码中调用它时,为时已晚,调度程序已经处于 Activity 状态。

触发器在 bean 初始化时注册(在 afterPropertiesSet 回调方法中)。如果您之后尝试向工厂添加其他触发器,则不会考虑它,因为调度程序已经创建。

话虽如此,我并没有真正看到 XML 和 Java 代码之间的关系。在常规情况下,您的 plannedVacationServiceCronTrigger 应该已注册(并且没有理由将其设为 prototype bean)。

关于java - Spring 3\Quartz 2.集成问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22810050/

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