如何将数据传递给作为 MethodInvokingJobDetailFactoryBean 运行的方法?
我真正想做的是创建一个不同时运行的 Quartz 作业,但也可以访问 JobExecutionContext 以获得 previousFireTime
。这是我的对象:
// imports...
public class UtilityObject {
private SomeService someService;
@Autowired
public UtilityObject(SomeService someService) {
this.someService = someService;
}
public void execute(JobExecutionContext ctx) throws JobExecutionException {
Date prevDate = ctx.getPreviousFireTime();
// rest of the code...
}
}
这是我配置 bean 的方式:
<bean name="utilityBean" class="UtilityObject" />
<bean id="utilityJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetOjbect" ref="utilityBean" />
<property name="targetMethod" value="execute" />
<property name="concurrent" value="false" />
</bean>
<bean name="utilityTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="utilityJob" />
<property name="startDelay" value="5000" />
<property name="repeatInterval" value="20000" />
</bean>
当我尝试运行它时,它在创建 bean 期间失败了
NoSuchMethodException:UtilityJob.execute()
有什么想法吗?
解决方案:
阅读斯卡夫曼的回答后,我能够让我的解决方案发挥作用。使用我拥有的触发器,我知道名称是,并发现默认组是“DEFAULT”。我让我的类扩展了 QuartzJobBean 类,然后添加了这段代码:
protected void executeInternal(JobExecutionContext ctx)
throws JobExecutionException {
boolean isRunning =
(ctx.getScheduler().getTriggerState("utilityTrigger", "DEFAULT") ==
Trigger.STATE_BLOCKED);
if (isRunning) {
// run the job
}
}
抱歉,格式很奇怪;这些是一些很长的线!
What I'm really trying to do is create a Quartz job that doesn't run concurrently, but also can access the JobExecutionContext
in order to get the previousFireTime
. Here's my object:
// imports...
public class UtilityObject {
private SomeService someService;
@Autowired
public UtilityObject(SomeService someService) {
this.someService = someService;
}
public void execute(JobExecutionContext ctx) throws JobExecutionException {
Date prevDate = ctx.getPreviousFireTime();
// rest of the code...
}
}
And here's how I've configured my bean:
<bean name="utilityBean" class="UtilityObject" />
<bean id="utilityJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetOjbect" ref="utilityBean" />
<property name="targetMethod" value="execute" />
<property name="concurrent" value="false" />
</bean>
<bean name="utilityTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="utilityJob" />
<property name="startDelay" value="5000" />
<property name="repeatInterval" value="20000" />
</bean>
When I try to run this, it fails during the creation of the bean with
NoSuchMethodException: UtilityJob.execute()
Any ideas?
Solution:
After reading skaffman's answer, I was able to get my solution working. Using the trigger that I had, I knew that the name was, and figured out that the default group was 'DEFAULT'. I had my class extend the QuartzJobBean
class, and then added this bit of code:
protected void executeInternal(JobExecutionContext ctx)
throws JobExecutionException {
boolean isRunning =
(ctx.getScheduler().getTriggerState("utilityTrigger", "DEFAULT") ==
Trigger.STATE_BLOCKED);
if (isRunning) {
// run the job
}
}
Sorry for the weird formatting; these are some long lines!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以使用参数属性将参数传递给
MethodInvokingJobDetailFactoryBean
,其方式与使用 springMethodInvokingFactoryBean
相同。例如:
It is possible to pass arguments to
MethodInvokingJobDetailFactoryBean
in the same way as with a springMethodInvokingFactoryBean
using the arguments property.For example:
MethodInvokingJobDetailFactoryBean 很方便,但很原始 - 它只能执行不带参数的公共方法。
如果您的作业需要访问 Quartz API,那么您需要使用
JobDetailBean
和QuartzJobBean
而不是MethodInvokingJobDetailFactoryBean
。请参阅文档< /a> 获取说明。QuartzJobBean
在运行时会传递当前的JobExecutionContext
。MethodInvokingJobDetailFactoryBean
is handy, but primitive - it can only execute public methods with no arguments.If your job needs access to the Quartz API, then you'll need to use
JobDetailBean
andQuartzJobBean
instead ofMethodInvokingJobDetailFactoryBean
. See docs for instructions how. TheQuartzJobBean
is passed the currentJobExecutionContext
when it runs.