简体   繁体   中英

spring batch with AMQP Listener

I want to use spring batch with a RabbitMQ listener

When a message is received, the first step will be called and repeat the treatment after each message reciption

This is my listener : public class MyMessageListener implements MessageListener {

@Override
public void onMessage(Message msg) {
    String messageBody= new String(msg.getBody());
    logger.info("Listener received message {}", messageBody);
    Gson gson = new Gson();


    //call spring batch step to Generate report
    //TODO


}

}

You can use Spring Batch Integration and the JobLaunchingGateway to launch a job for each message.

However, if you want multiple messages to comprise a "batch", you should use a RabbitTemplate within an ItemReader to receive() the messages you want to be processed as a batch (instead of a listener).

I call the job in a listener and it work fine :

@Override 
void onMessage(Message msg) {

    JobLauncher jobLauncher = (JobLauncher) appContext.getBean("jobLauncher");
    Job job = (Job) appContext.getBean("rapportJob");

    try {
        JobParameters jobParameters = new JobParametersBuilder()
                                      .addLong("time",System.currentTimeMillis())
                                      .toJobParameters();
        JobExecution execution = jobLauncher.run(job, jobParameters);
        System.out.println("Exit Status : " + execution.getStatus());

    } catch (Exception e) {
        e.printStackTrace();
    }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM