简体   繁体   中英

RabbitMQ fixed reply queue configuration

I'm having some trouble configuring a fixed reply queue to my rabbitMQ message.

I have the producer and consumer as follows:

public class Producer {

    private static ApplicationContext context;
    private static RabbitTemplate template;

    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("mq-producer-context.xml");
        context = new ClassPathXmlApplicationContext("context.xml");
        template = context.getBean(RabbitTemplate.class);
        //Queue replyQueue = new Queue("ub.replyqueue");
        //template.setReplyQueue(replyQueue);
    }

    @Scheduled(fixedRate = 1000)
    public void execute() {
        System.out.println("execute...");
        template.convertAndSend("helloooo");
        //template.convertSendAndReceive("helloooo");
    }
}

Consumer:

public class Consumer implements MessageListener {

    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("mq-consumer-context.xml");
    }

    public void onMessage(Message message) { 
        System.out.println("message received" + message);
    }
    /**public String handleMessage(String msg) {
        return "RECEIVED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
    }**/
}

Producer xml config: (in beans... tag)

<beans>
    <import resource="context.xml" />

    <task:scheduler id="myScheduler" pool-size="10" />
    <task:annotation-driven scheduler="myScheduler" />

    <bean id="producer" class="com.urbanbuz.mq.Producer"></bean>
</beans>

consumer xml config:

<beans>
    <import resource="context.xml"/>

    <rabbit:listener-container connection-factory="connectionFactory">
        <rabbit:listener ref="consumer" queue-names="ub.queue" />
    </rabbit:listener-container>

    <context:annotation-config />
    <context:component-scan base-package="com.urbanbuz.mq" />
    <aop:aspectj-autoproxy />

    <bean id="consumer" class="com.urbanbuz.mq.Consumer"></bean>
</beans>

context.xml:

<beans>
    <rabbit:connection-factory id="connectionFactory" host="localhost" virtual-host="farahvhost" username="farah" password="farah" />

    <rabbit:admin connection-factory="connectionFactory" />

    <rabbit:queue name="ub.queue" />

    <rabbit:direct-exchange name="ub.exchange">
        <rabbit:bindings>
            <rabbit:binding queue="ub.queue"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:direct-exchange>

    <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="ub.exchange" queue="ub.queue" reply-timeout="15000" />
</beans>

Documentation was not really clear. I tried using a convertSendAndReceive on the producer side and an on message handler on the consumer side however that did not work (code commented out as shown above), I got this error: No default listener method specified: Either specify a non-null value for the 'defaultListenerMethod' property or override the 'getListenerMethodName' method.

When using a fixed reply queue, you have to configure a listener container on the rabbit template.

The simplest configuration is to just add a <rabbit:reply-listener /> child element to the template.

See the reference documentation for complete details.

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