Spring Rabbit MQ Publish and Acknowledge

时间:2017-03-10 06:56:50

标签: spring-boot spring-rabbit spring-rabbitmq

在我的项目中,我有3个Classes App(发布者),PojoListener( Receiver ),Config(绑定App和PojoListner ) 。但我需要将PojoListener从我的项目中分离出来并部署到其他地方,以便我的Listener类将连续监听Rabbit Mq并将消息恢复到队列,然后再发送到App Class,以获取我的App类发布的任何消息。

由于我的配置文件对于Publisher和Receiver都很常见。有没有办法分开它们。我需要在不同的服务器和Publisher上部署我的接收器。两人都会听到共同的队列。

**App.java** :
public class App {

    public static void main(String[] args) throws UnsupportedEncodingException {        
        ApplicationContext context = new AnnotationConfigApplicationContext(RabbitMQConfig.class);
        RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
        String response = (String) rabbitTemplate.convertSendAndReceive("message from publisher");
        System.out.println("message sent");
        System.out.println("response from :" + response);
    }
}

PojoListener.java:

public class PojoListener {

    public String handleMessage(String msg) {
        System.out.println("IN POJO RECEIVER!!!");
        return msg.toUpperCase();
    }
}

RabbitMQConfig.java

@Configuration

public class RabbitMQConfig {

@Bean

public ConnectionFactory rabbitConnectionFactory() {

    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    connectionFactory.setHost("localhost");
    connectionFactory.setUsername("guest");
    connectionFactory.setPassword("guest");
    return connectionFactory;
}  


@Bean

public RabbitTemplate fixedReplyQRabbitTemplate() {

    RabbitTemplate template = new RabbitTemplate(rabbitConnectionFactory());
    template.setExchange(ex().getName());
    template.setRoutingKey("test");
    template.setReplyQueue(replyQueue());
    template.setReplyTimeout(60000);
    return template;
}


@Bean

public SimpleMessageListenerContainer replyListenerContainer() {

    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(rabbitConnectionFactory());
    container.setQueues(replyQueue());
    container.setMessageListener(fixedReplyQRabbitTemplate());
    return container;
}



@Bean

public SimpleMessageListenerContainer serviceListenerContainer() {

    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(rabbitConnectionFactory());
    container.setQueues(requestQueue());
    container.setMessageListener(new MessageListenerAdapter(new PojoListener()));
    return container;
}


@Bean

public DirectExchange ex() {

    return new DirectExchange("rabbit-exchange", false, true);
}

@Bean

public Binding binding() {

    return BindingBuilder.bind(requestQueue()).to(ex()).with("test");
}


@Bean

public Queue requestQueue() {

    return new Queue("request-queue-spring");
}


@Bean

public Queue replyQueue() {

    return new Queue("response-queue-spring");
}

@Bean

public RabbitAdmin admin() {

   return new RabbitAdmin(rabbitConnectionFactory());
}
}

0 个答案:

没有答案