如何在春季批处理中同时运行两个作业

时间:2017-01-06 08:12:19

标签: java spring spring-batch spring-scheduled

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

以下是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:

public class DeleteOldOTPWeekly实现Tasklet {}

公共类CouponToggleActivation实现Tasklet {}

如果我只运行一份工作,事情就会正常工作。但是当我安排第二项工作时,它也会例外:

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 个答案:

答案 0 :(得分:0)

错误表明couponToggleActivation没有设置JobRepository。

我认为这是指otpJob的工作。

答案 1 :(得分:0)

谢谢。为了你的努力。

经过长时间的挣扎,我发现在宣布couponToggleActivation的Job和Tasklet方面存在问题。

<batch:job id="couponToggleActivationJob" 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状态。

所以我做到了。

SELECT t.NdtType,
       t.RequestNumber,
       t.NdtReportNumber,
       t.ResponseReportDatetime,
       t.Defect,
       t.ResponseReportNumber,
       t.Remark
FROM
(
    SELECT NRD.NdtType,
           NRD.Id,
           NRD.RequestNumber,
           NR.NdtReportNumber,
           NRD.ResponseReportDatetime,
           NRD.Defect,
           NRD.ResponseReportNumber,
           NRD.Remark,
           ROW_NUMBER() OVER(PARTITION BY NRD.NdtType ORDER BY NRD.Id DESC) AS rn
    FROM dbo.NdtReportDetails NRD
    LEFT OUTER JOIN NdtReports NR
        ON NRD.ReportId = NR.Id
    WHERE NRD.JointId = 1661632 AND
          NRD.NdtType IN ('RT', 'PT', 'PWHT', 'MT', 'UT')
) t
WHERE t.rn = 1
ORDER BY t.Id DESC