我有一个使用Spring Batch JmsItemReader和сustomItemWriter的简单程序。该程序的目的是读取ActiveMQ队列。我已将提交间隔设置为25,但是它不起作用。我会以随机的间隔将商品发送给我的商品作家。我花了一整天的时间来解决这个问题,但我无法解决。请帮我。这是我的工作配置:
<batch:job id="reportJob">
<batch:step id="step1">
<batch:tasklet>
<batch:chunk reader="jmsItemReader"
writer="customItemWriter"
commit-interval="25">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="jmsItemReader"
class="org.springframework.batch.item.jms.JmsItemReader">
<property name="itemType" value="com.example.Entities.Spitter"/>
<property name="jmsTemplate" ref="jmsTemplate"/>
</bean>
<bean id="customItemWriter" class="com.example.batch.CustomItemWriter"/>
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
</bean>
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<bean id="schedulingLauncher" class="com.example.batch.SchedulingLauncher">
<property name="job" ref="reportJob"/>
<property name="jobLauncher" ref="jobLauncher"/>
</bean>
<task:scheduler id="scheduler" />
<task:scheduled-tasks scheduler="scheduler">
<task:scheduled ref="schedulingLauncher"
method="launch"
fixed-delay="10000" />
</task:scheduled-tasks>
JMS豆:
<bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
<property name="userName" value="admin"/>
<property name="password" value="admin"/>
</bean>
<bean id="queue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="JpaQueue"/>
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="defaultDestination" ref="queue"/>
<property name="receiveTimeout" value="500"/>
<property name="sessionTransacted" value="true" />
</bean>
我的ItemWriter:
public class CustomItemWriter implements ItemWriter<Object> {
public void write(List<?> items) throws Exception {
System.out.println("List size: " + items.size());
for (Object item: items) {
System.out.println(item);
}
}
}
还有ShedulingLauncher:
public class SchedulingLauncher {
private Job job;
private JobLauncher jobLauncher;
public void launch() throws Exception{
JobParameters jobParams =
new JobParametersBuilder().addLong("launchTime", System.currentTimeMillis()).toJobParameters();
jobLauncher.run(job,jobParams);
}
输出:
SimpleJobLauncher:133-使用以下参数启动了作业:[FlowJob:[name = reportJob]]:[{launchTime = 1545564286588}]
执行步骤:[step1]
列表大小:3
Spitter(id = 0,userName = UserName0,password = pasword0,fullName = User User0,email=qwer@qwer.com0)
Spitter(id = 0,userName = UserName572,password = pasword572,fullName = User User572,email=qwer@qwer.com572)
Spitter(id = 0,userName = UserName773,password = pasword773,fullName = User User773,email=qwer@qwer.com773)
SimpleJobLauncher:136-作业:[FlowJob:[name = reportJob]]已完成,并带有以下参数:[{launchTime = 1545564286588}]和以下状态:[COMPLETED]
答案 0 :(得分:0)
我看不出读取项目的数量为何以及如何是随机的。。不过有几点注意事项:
sessionTransacted
。您是否还在readerIsTransactionalQueue
上设置了JmsItemReader
?MapJobRepositoryFactoryBean
默认情况下使用ResourcelessTransactionManager
。这意味着您的(事务处理)jms会话正在运行时,没有正在进行的事务。您是否尝试在jms模板上设置sessionAcknowledgeMode = Session.SESSION_TRANSACTED
?JmsTransactionManager
。这是故意的吗?sessionTransacted
,要么使用事务性作业存储库(在这种情况下,您需要一个JtaTransactionManager
来同步两个事务管理器:jms和数据库)如果这些提示无济于事,请与Github(或其他地方)上的所有代码和依赖项共享一个项目,以便重现问题并为您提供帮助。