How to pass data to a method executed as MethodInvokingJobDetailFactoryBean?

What I'm really trying to do is create a Quartz job that does not start at the same time, but can also access the JobExecutionContext to get the previousFireTime . Here is my goal:

 // 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 this is how I 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 while creating the bean with

NoSuchMethodException: UtilityJob.execute ()

Any ideas?

Decision

After reading the answer of the scaffman, I was able to make my decision work. Using the trigger that I had, I knew that it was the name, and found out that the default group is "DEFAULT". I had a class extending the QuartzJobBean class and then this bit of code added:

 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 a few long lines!

+4
source share
2 answers

MethodInvokingJobDetailFactoryBean convenient, but primitive - it can execute only public methods without arguments.

If your work requires access to the Quartz API, you need to use JobDetailBean and QuartzJobBean instead of MethodInvokingJobDetailFactoryBean . See the docs for instructions. When QuartzJobBean starts, the current JobExecutionContext .

+2
source

You can pass arguments to a MethodInvokingJobDetailFactoryBean in the same way as using spring MethodInvokingFactoryBean using the arguments property.

For instance:

 <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> 
+10
source

Source: https://habr.com/ru/post/1348116/


All Articles