所以我有兩個工作,每個都有獨立的tasklet。Spring批處理和彈簧TaskScheduler
職吧將做:
sysout("JobA executed at "+new Date());
JOBB將做:
sysout("JobB executed at "+new Date());
這兩者可以是在一個單獨的XML或者在一個XML。但我目前有兩個單獨的XML。我定義了兩個作業用同樣的方法是這樣的:
<batch:job id="task_scheduler" restartable="false">
<batch:step id="test_task_scheduler"
allow-start-if-complete="true">
<batch:tasklet ref="job1" />
</batch:step>
</batch:job>
<bean id="job1" class="com.irsis.batch.tasklet.TestOut" />
<bean id="runScheduler" class="com.irsis.batch.taskscheduler.RunScheduler1" />
然後在我application-context.xml
我定義的任務調度
<task:scheduled-tasks>
<task:scheduled ref="runScheduler1" method="run"
cron="*0 0/1 * 1/1 * ? " />
<task:scheduled ref="runScheduler2" method="run"
cron="0 0/2 * 1/1 * ? "/>
</task:scheduled-tasks>
當運行我有這樣的例外
Could not autowire field: org.springframework.batch.core.Job com.irsis.batch.taskscheduler.RunScheduler1.job; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.batch.core.Job] is defined: expected single matching bean but found 2: task_scheduler,task_scheduler2
順便說一句,這是我的RunScheduler1.java文件
@Autowired
JobLauncher jobLauncher;
@Autowired
Job job;
public void run() {
try {
String dateParam = new Date().toString();
JobParameters param = new JobParametersBuilder().addString("date",
dateParam).toJobParameters();
JobExecution execution = jobLauncher.run(job, param);
System.out.println("Exit Status : " + execution.getStatus());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Done");
}
現在我的問題是春季批處理可能有多個作業嗎?因爲從我看到的例外是,它只是期待一份工作。
雖然我嘗試了另一種方式。我所做的是在應用程序的context.xml
<batch:job id="job" restartable="false">
<batch:step id="test_task_scheduler"
allow-start-if-complete="true">
<batch:tasklet ref="job1" />
</batch:step>
</batch:job>
<bean id="job1" class="com.irsis.batch.tasklet.TestOut" />
<bean id="runScheduler1" class="com.irsis.batch.taskscheduler.RunScheduler1" />
<batch:job id="job2" restartable="false">
<batch:step id="test_task2_scheduler"
allow-start-if-complete="true">
<batch:tasklet ref="job2" />
</batch:step>
</batch:job>
<bean id="job2" class="com.irsis.batch.tasklet.TestOut2" />
<bean id="runScheduler2" class="com.irsis.batch.taskscheduler.RunScheduler2" />
<task:scheduled-tasks>
<task:scheduled ref="runScheduler1" method="run"
cron="*0 0/1 * 1/1 * ? " />
</task:scheduled-tasks>
<task:scheduled-tasks>
<task:scheduled ref="runScheduler2" method="run"
cron="0 0/2 * 1/1 * ? " />
</task:scheduled-tasks>
它跑的第一份工作,然後每當它試圖運行第二個作業出現此異常這樣的事情:
PreparedStatementCallback; SQL [INSERT into BATCH_JOB_INSTANCE(JOB_INSTANCE_ID, JOB_NAME, JOB_KEY, VERSION)
values (?, ?, ?, ?)]; ERROR: duplicate key value violates unique constraint "job_inst_un" Detail: Key (job_name, job_key)=(job, 8a87688793625c6f3b8f982dbe6689d2) already exists.; nested exception is org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "job_inst_un" Detail: Key (job_name, job_key)=(job, 8a87688793625c6f3b8f982dbe6689d2) already exists.
更新:我想我會改變我的方法。另一個問題鏈接here
感謝,
噴氣
喜@BijuKunjummen我編輯我的問題。我添加了RunScheduler1.java文件以供參考。 –
劃傷我以前的評論。能夠運行該項目,但是無論何時開始執行作業,它都會捕獲異常。 「工作不能爲空。」 –
我可以推薦簡單地結束您當前的問題,並針對您的具體問題重新開放 - 現在您已經提到了3個不同的問題,並且我已經爲其中2個提供了答案。 –