MethodInvokingJobDetailFactoryBean Pass Parameter

MethodInvokingJobDetailFactoryBean Pass Parameter

If you are working with Spring with Quartz to schedule the job and wanted to pass parameter while using the MethodInvokingJobDetailFactoryBean Pass Parameter please use below:

  • Spring bean:
<bean id="cronQuartzJobScheduler" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
	<property name="targetObject" ref="cronQuartzJob" />
	<property name="targetMethod" value="executeScheduleJob" />
	<property name="arguments">
		<list>
			<value>JavaHonk1</value>
			<value>JavaHonk2</value>
		</list>
	</property>
</bean>
  • Java class: CronQuartzJob.java:
package com.javahonk;

import java.time.LocalDateTime;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.quartz.JobExecutionException;

/**
 * @author Java Honk
 *
 */
public class CronQuartzJob {
	
	private static final Logger LOGGER = LogManager.getLogger(CronQuartzJob.class.getName());
	
	public void executeScheduleJob(String value1, String value2) throws JobExecutionException {
		
		LOGGER.info("Received two values: "+value1+"  Value2: "+value2);
		LOGGER.info("Cron Quartz Job started at time: {}", LocalDateTime.now());		
		
	}

}
  • Note: If you still getting exception in spring where its says method not found please include empty method as well. I belive this is bug using MethodInvokingJobDetailFactoryBean as below:
package com.javahonk;

import java.time.LocalDateTime;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.quartz.JobExecutionException;

public class CronQuartzJob {
	
	private static final Logger LOGGER = LogManager.getLogger(CronQuartzJob.class.getName());
	
	public void executeScheduleJob(){}
	
	public void executeScheduleJob(String value1, String value2) throws JobExecutionException {
		
		LOGGER.info("Received two values: "+value1+"  Value2: "+value2);
		LOGGER.info("Cron Quartz Job started at time: {}", LocalDateTime.now());		
		
	}

}

Extras:

  • If you want to pass single parameter use bean as below:
<bean id="cronQuartzJobScheduler" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
	<property name="targetObject" ref="cronQuartzJob" />
	<property name="targetMethod" value="executeScheduleJob" />
	<property name="arguments" value="JavaHonk1"/>
	
</bean>
  • Method without any parameter:
<bean id="cronQuartzJobScheduler" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
	<property name="targetObject" ref="cronQuartzJob" />
	<property name="targetMethod" value="executeScheduleJob" />		
</bean>

Reference:

Leave a Reply

Your email address will not be published. Required fields are marked *