Springboot RabbitMq消费者只在第一次消费。请帮助我正确配置,以及如何配置多个列表

时间:2018-03-22 15:25:53

标签: spring-boot rabbitmq

Springboot RabbitMq消费者只在第一次消费。请帮助我正确配置以及如何配置多个列表。我正在开发一个自动映射解决方案,我执行各种工作。并且需要将这些工作放入队列

这是我的应用程序类:Application.java

@SpringBootApplication
@ComponentScan(basePackages = { "com.fractal.sago", "com.fractal.grpc" })
public class Application extends SpringBootServletInitializer {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);

    }

RabbitMqConfig class:

@Configuration
public class RabbitMqConfig {
    public final static String JOB_QUEUE_NAME = "jobQueue";
    public final static String JOB_EXCHANGE_NAME = "jobExchange";

    @Bean
    Queue jobQueue() {
        return new Queue(JOB_QUEUE_NAME, true);
    }

    @Bean
    DirectExchange jobExchange() {
        return new DirectExchange(JOB_EXCHANGE_NAME);
    }

    @Bean
    Binding jobBinding(DirectExchange directExchange) {
        return BindingBuilder.bind(jobQueue()).to(jobExchange()).with(jobQueue().getName());
    }

    @Bean
    SimpleMessageListenerContainer jobQueueContainer(ConnectionFactory connectionFactory,
            MessageListenerAdapter joblistenerAdapter) {
        SimpleMessageListenerContainer jobQueueContainer = new SimpleMessageListenerContainer();
        jobQueueContainer.setConnectionFactory(connectionFactory);
        jobQueueContainer.setQueueNames(JOB_QUEUE_NAME);
        jobQueueContainer.setMessageListener(joblistenerAdapter);
        return jobQueueContainer;
    }

    @Bean
    MessageListenerAdapter joblistenerAdapter(JobQueueConsumer messageReceiver) {
        MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(messageReceiver, "receiveMessage");
        messageListenerAdapter.setMessageConverter(producerJackson2MessageConverter());
        return messageListenerAdapter;
    }

    @Bean
    public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

}

producer : JobProducer

@Component
public class JobQueueProducer {
    @Autowired
    RabbitTemplate rabbitTemplate;

    public void sendMessage(String message) {

        Message messageToSend = MessageBuilder.withBody(message.getBytes())
                .setDeliveryMode(MessageDeliveryMode.PERSISTENT).build();
        rabbitTemplate.convertAndSend(RabbitMqConfig.JOB_EXCHANGE_NAME, RabbitMqConfig.JOB_QUEUE_NAME, messageToSend);
        //rabbitTemplate.convertAndSend(RabbitMqConfig.JOB_QUEUE_NAME, messageToSend);

    }


}

Consumer :JobQueueConsumer

    enter code here

@Component
public class JobQueueConsumer implements MessageListener {
    @Autowired
    SagoAlgo sagoAlgo;

    @Autowired
    CCDMappingService ccdMappingService;

    @RabbitListener(queues = { RabbitMqConfig.JOB_QUEUE_NAME })
    public void receiveMessage(Message message) throws SQLException {
        System.out.println("Received Message: " + new String(message.getBody()));
        Integer jobId = Integer.parseInt(new String(message.getBody()));
        System.out.println(jobId);
        CCDMappingVO ccdVo =ccdMappingService.fetchCCDWithCategoriesById(jobId);
        sagoAlgo.execAlgo(ccdVo);  //my Algo to be executed

    }

//when I implement MessageListener default method to be executed
    public void onMessage(Message message) {
        // System.out.println("Received Message: " + new
        // String(message.getBody()));

    }
}

0 个答案:

没有答案