如何在 Spring 季批次中同时运行两个作业

我正在尝试使用Spring Batch和Spring Task Scheduler运行两个作业而不管它们的调度时间 . 这两个作业(Tasklet)都在不同的时间间隔执行不同的工作 .

以下是springConfig.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:batch="http://www.springframework.org/schema/batch" xmlns:task="http://www.springframework.org/schema/batch"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd

	http://www.springframework.org/schema/batch 
	http://www.springframework.org/schema/batch/spring-batch.xsd
	">
	<context:component-scan
		base-package="com.mythribuswebservice.job,com.mythribuswebservice.job.scheduler" />
	<bean id="deleteOldOTPS" class="com.mythribuswebservice.job.tasklet.DeleteOldOTPWeekly"></bean>
	<bean id="couponToggleActivation"
		class="com.mythribuswebservice.job.tasklet.CouponToggleActivation"></bean>

	<batch:job id="otpJob" job-repository="jobRepository">
		<batch:step id="step1">
			<batch:tasklet ref="deleteOldOTPS">

			</batch:tasklet>
		</batch:step>
	</batch:job>
	<batch:job id="couponToggleActivation" job-repository="jobRepository">
		<batch:step id="couponActivation">
			<batch:tasklet ref="couponToggleActivation">

			</batch:tasklet>
		</batch:step>
	</batch:job>
	<bean id="jobRepository"
		class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
		<property name="transactionManager" ref="transactionManager" />
	</bean>
	<bean id="transactionManager"
		class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://orcl/mythribus" />
		<property name="username" value="root" />
		<property name="password" value="root" />
	</bean>
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<bean id="jobLauncher"
		class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
		<property name="jobRepository" ref="jobRepository" />
	</bean>
	<bean id="oTPJobScheduler" class="com.mythribuswebservice.job.scheduler.OTPJobScheduler">
	</bean>
	<bean id="couponToggleActivationScheduler"
		class="com.mythribuswebservice.job.scheduler.CouponToggleActivationScheduler">
	</bean>
</beans>

以下是CouponToggleActivationScheduler和OTPJobScheduler的实现:

@EnableAsync

@EnableScheduling公共类CouponToggleActivationScheduler {

@Autowired
JobLauncher jobLauncher;
@Autowired
Job couponToggleActivation;
final JobParametersBuilder jobParametersBuilder=new JobParametersBuilder();

@Scheduled(cron = "*/10 * * * * *")
public void runCouponActivationScheduler() {
    System.out.println("CouponToggleActivationScheduler" + new Date()+"-"+couponToggleActivation.hashCode());
    JobExecution execution;
    jobParametersBuilder.addString("couponToggleActivationScheduler", UUID.randomUUID().toString(), true);

        execution = jobLauncher.run(couponToggleActivation, jobParametersBuilder.toJobParameters());
        System.out.println("Exit Status : " + execution.getStatus());


}

}


public class OTPJobScheduler {

@Autowired
JobLauncher jobLauncher;
@Autowired
Job otpJob;
final JobParametersBuilder jobParametersBuilder=new JobParametersBuilder();


@Scheduled(cron = "*/10 * * * * *")
public void runOTPDeleteJob() {
    System.out.println("Ricks...." + new Date()+otpJob.hashCode());
    jobParametersBuilder.addString("otpJobScheduler", UUID.randomUUID().toString(),true);
    JobExecution execution;
        execution = jobLauncher.run(otpJob, jobParametersBuilder.toJobParameters());
        System.out.println("Exit Status : " + execution.getStatus());

}

}


有些Tasklet在那里做各自的工作:

公共类DeleteOldOTPWeekly实现Tasklet {}

公共类CouponToggleActivation实现Tasklet {}

If i am running only one job the things work fine.but when i schedule the second too it gives exception:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'couponToggleActivation': Cannot create inner bean '(inner bean)#12e0f78' of type [org.springframework.batch.core.configuration.xml.SimpleFlowFactoryBean] while setting bean property 'flow'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#12e0f78': Cannot create inner bean '(inner bean)#693226' of type [org.springframework.batch.core.job.flow.support.StateTransition] while setting bean property 'stateTransitions' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#693226': Cannot create inner bean '(inner bean)#758545' of type [org.springframework.batch.core.job.flow.support.state.StepState] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#758545': Cannot resolve reference to bean 'couponActivation' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'couponActivation': Cannot resolve reference to bean 'couponToggleActivation' while setting bean property 'tasklet'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'couponToggleActivation': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: JobRepository must be set
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:313)
	at com.mythribuswebservice.MythribusWebserviceApplication.main(MythribusWebserviceApplication.java:15)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#12e0f78': Cannot create inner bean '(inner bean)#693226' of type [org.springframework.batch.core.job.flow.support.StateTransition] while setting bean property 'stateTransitions' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#693226': Cannot create inner bean '(inner bean)#758545' of type [org.springframework.batch.core.job.flow.support.state.StepState] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#758545': Cannot resolve reference to bean 'couponActivation' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'couponActivation': Cannot resolve reference to bean 'couponToggleActivation' while setting bean property 'tasklet'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'couponToggleActivation': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: JobRepository must be set
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:299)
	... 16 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#693226': Cannot create inner bean '(inner bean)#758545' of type [org.springframework.batch.core.job.flow.support.state.StepState] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#758545': Cannot resolve reference to bean 'couponActivation' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'couponActivation': Cannot resolve reference to bean 'couponToggleActivation' while setting bean property 'tasklet'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'couponToggleActivation': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: JobRepository must be set
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:313)
	... 24 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#758545': Cannot resolve reference to bean 'couponActivation' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'couponActivation': Cannot resolve reference to bean 'couponToggleActivation' while setting bean property 'tasklet'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'couponToggleActivation': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: JobRepository must be set
	
	... 32 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'couponActivation': Cannot resolve reference to bean 'couponToggleActivation' while setting bean property 'tasklet'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'couponToggleActivation': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: JobRepository must be set
	
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
	... 40 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'couponToggleActivation': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: JobRepository must be set
	
	... 50 more
Caused by: java.lang.IllegalArgumentException: JobRepository must be set
	
	... 55 more

我删除了StackTrace .

任何评论都会很明显 .

回答(2)

2 years ago

该错误表明couponToggleActivation没有JobRepository设置 .

我认为这是指otpJob工作 .

2 years ago

谢谢..为你的努力 .

经过很长一段时间的努力,我能够发现声明couponToggleActivation的Job和Tasklet存在问题 .

<batch:job id="couponToggleActivation" job-repository="jobRepository">
    <batch:step id="couponActivation">
        <batch:tasklet ref="couponToggleActivation">

        </batch:tasklet>
    </batch:step>
</batch:job>

我已经编写了job id和tasklet ref作为couponToggleActivation,只是更改了为我工作的工作名称 .

问题是IOC容器无法识别Job id和Tasklet Implementer类ref并且处于Ambiguity状态 .

所以我做到了 .

<batch:job id="couponToggleActivationJob" job-repository="jobRepository">
    <batch:step id="couponActivation">
        <batch:tasklet ref="couponToggleActivation">

        </batch:tasklet>
    </batch:step>
</batch:job>