想知道如何将 JAXBElement 传递给 Camel 路由,该路由从通过 Camel Route 加载的 Spring 批处理读取的批处理文件的每一行进行处理。
下面给出的代码片段使用 customerWriter 方法调用 JMSTemplate 将消息写入队列。相反,我需要将消息路由到另一个 Camel 路由。
当前: CamelRoute -> ReadFile -> Spring Batch -> 处理每一行 -> Queue
预期: CamelRoute -> ReadFile -> Spring Batch -> 处理每一行 -> Camel Route
骆驼路线读取文件:
@Override
public void configure() {
String fromUri = batchLoadPath + "?" + batchFileOptions;
from(fromUri).process(new Processor() {
public void process(Exchange msg) {
File file = msg.getIn().getBody(File.class);
String fileName = file.getAbsolutePath();
try {
JobParameters jobParameters = new JobParametersBuilder().addString("input.file.name", fileName).addDate("dateTime", new Date()).toJobParameters();
jobLauncher.run(importCustomerJob, jobParameters);
} catch (Exception e) {
log.error(Process file encountered error:" + e.getMessage(), e);
}
}
})
.to("log:EndBatch");
批量配置:
@Bean
public JmsItemWriter<String> customerWriter() {
JmsItemWriter<String> writer = new JmsItemWriter<String>();
writer.setJmsTemplate(jmsTemplate);
return writer;
}
public Job importCustomerJob(JobCompletionNotificationListener listener, JobBuilderFactory jobBuilderFactory, Step step1) {
JobBuilder builder = jobBuilderFactory.get("importCustomerJob");
builder.incrementer(new RunIdIncrementer());
builder.listener(listener);
JobFlowBuilder jfb = builder.flow(step1);
jfb.end();
Job job = jfb.build().build();
return job;
}
@Bean
public Step step1(StepBuilderFactory stepBuilderFactory) {
// Read chunk of 10 records and writing those as messages to queue
return stepBuilderFactory.get("step1")
.<Customer, String>chunk(10)
.reader(customerReader())
.faultTolerant()
.skipPolicy(fileVerificationSkipper())
.processor(customerItemProcessor())
.writer(customerWriter())
.listener(customerReader())
.build();
}
批处理器:
public class CustomerItemProcessor implements ItemProcessor<Customer, String> {
@Autowired
JaxbUtil jaxbUtil;
public String process(Customer item) throws Exception {
// Mapping code goes here
JAXBElement<CustomerX> mobj = customerFactory.createCustomerX(cp);
return jaxbUtil.objectToXml(mobj);
}
}