如何将数据传递给作为 MethodInvokingJobDetailFactoryBean 运行的方法?

发布于 2024-10-31 23:29:55 字数 1812 浏览 1 评论 0原文

我真正想做的是创建一个不同时运行的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

够运 2024-11-07 23:29:55

可以使用参数属性将参数传递给 MethodInvokingJobDetailFactoryBean,其方式与使用 spring MethodInvokingFactoryBean 相同。

例如:

<bean id="myJob"
      class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="myBean" />
    <property name="targetMethod" value="myMethod" />
    <property name="arguments">
        <list>
            <value>greetings</value>
            <ref bean="anotherBean"/>
        </list>
    </property>
</bean>

It is possible to pass arguments to MethodInvokingJobDetailFactoryBean in the same way as with a spring MethodInvokingFactoryBean using the arguments property.

For example:

<bean id="myJob"
      class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="myBean" />
    <property name="targetMethod" value="myMethod" />
    <property name="arguments">
        <list>
            <value>greetings</value>
            <ref bean="anotherBean"/>
        </list>
    </property>
</bean>
琉璃梦幻 2024-11-07 23:29:55

MethodInvokingJobDetailFactoryBean 很方便,但很原始 - 它只能执行不带参数的公共方法。

如果您的作业需要访问 Quartz API,那么您需要使用 JobDetailBeanQuartzJobBean 而不是 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 and QuartzJobBean instead of MethodInvokingJobDetailFactoryBean. See docs for instructions how. The QuartzJobBean is passed the current JobExecutionContext when it runs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文