当前位置: 首页 > 知识库问答 >
问题:

如何在spring batch中同时运行两个作业

闾丘晨
2023-03-14

我正在尝试使用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>

以下是CouPonTougleActivationScheduler和OTPJobScheduler的实现:

@EnableAsync

@enableSched调度公有类CouPonTougleActivationScheduler{

@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());

}

}

有一些微线程可以完成各自的工作:

公共类DeleteOldOTPWeekly实现Tasklet{}

实现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

任何评论。将是可观的。

共有2个答案

狄宏大
2023-03-14

谢谢感谢你的努力。

经过长时间的斗争,我能够发现,有问题与声明的工作和任务的优惠券切换激活。

<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容器无法识别作业id和Tasklet实现器类ref,并且模糊不清。

所以我成功了。

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

        </batch:tasklet>
    </batch:step>
</batch:job>
徐翔
2023-03-14

该错误表示,CouPonTokgleActivation没有JobRepository集。

我想这指的是otpJobs工作。

 类似资料:
  • 我有一个情况,我需要启动两个线程一个接一个。我尝试了以下代码片段,在这里我可以启动Thread12,但不能启动Thread2。我怎样才能开始两个......?如何启动两个线程一个接一个...? 代码片段

  • 用例:步骤1:ItemReader:从数据库中读取1000个ItemProcessor块中的数据:处理这些数据。ItemWriter:将数据写入地图,以便下一步使用 步骤2:ItemReader:读取地图ItemProcessor:处理地图数据并获取新对象。ItemWriter:将新的进程对象持久化到数据库中。 现在我希望Map在整个作业中保持不变,目前我已经为Map创建了一个不同的POJO类,并

  • 问题内容: 是否可以同时在两个不同的元素上运行两个动画?我需要与此问题相反的Jquery排队动画。 我需要做这样的事情… 但要同时运行这两个。我唯一想到的就是为每个动画使用一次,但是我认为这不是最好的解决方案。 问题答案: 就在这里!

  • 在Quartz-Scheduler中是否可以定义作业的执行约束? 谢谢你的回答。

  • 默认情况下,所有设置都是。如何启用同时运行的多个作业?

  • 正如title一样,我有几个同时设置触发时间的作业,但我不希望它们一次全部执行。因为我担心它可能会导致我的系统过热问题。有没有什么方法可以配置触发器,使它的执行时间可以在定义的偏移量之间变化? 例如: 排定要在12pm执行的2个作业:Job1和Job2 在12pm执行Job1。 在12:01pm执行Job2。