Last active
May 29, 2019 04:38
-
-
Save garyrussell/8900257 to your computer and use it in GitHub Desktop.
Spring Boot AMQP Reqest/Reply Example with Fixed Reply Queue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2014 the original author or authors. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package foo; | |
import org.springframework.amqp.core.Binding; | |
import org.springframework.amqp.core.BindingBuilder; | |
import org.springframework.amqp.core.DirectExchange; | |
import org.springframework.amqp.core.Queue; | |
import org.springframework.amqp.rabbit.connection.ConnectionFactory; | |
import org.springframework.amqp.rabbit.core.RabbitAdmin; | |
import org.springframework.amqp.rabbit.core.RabbitTemplate; | |
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; | |
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.ResponseBody; | |
@Configuration | |
@Controller | |
@EnableAutoConfiguration | |
public class RequestReplyFixed { | |
@Autowired | |
private ConnectionFactory rabbitConnectionFactory; | |
@Autowired | |
private RabbitTemplate fixedReplyQRabbitTemplate; | |
@RequestMapping("/") | |
@ResponseBody | |
String home() throws Exception { | |
return (String) this.fixedReplyQRabbitTemplate.convertSendAndReceive("Hello, world!"); | |
} | |
@Bean | |
public RabbitTemplate fixedReplyQRabbitTemplate() { | |
RabbitTemplate template = new RabbitTemplate(this.rabbitConnectionFactory); | |
template.setExchange(ex().getName()); | |
template.setRoutingKey("test"); | |
template.setReplyQueue(replyQueue()); | |
return template; | |
} | |
@Bean | |
public SimpleMessageListenerContainer replyListenerContainer() { | |
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); | |
container.setConnectionFactory(this.rabbitConnectionFactory); | |
container.setQueues(replyQueue()); | |
container.setMessageListener(fixedReplyQRabbitTemplate()); | |
return container; | |
} | |
@Bean | |
public SimpleMessageListenerContainer serviceListenerContainer() { | |
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); | |
container.setConnectionFactory(this.rabbitConnectionFactory); | |
container.setQueues(requestQueue()); | |
container.setMessageListener(new MessageListenerAdapter(new PojoListener())); | |
return container; | |
} | |
@Bean | |
public DirectExchange ex() { | |
return new DirectExchange("ex"); | |
} | |
@Bean | |
public Binding binding() { | |
return BindingBuilder.bind(requestQueue()).to(ex()).with("test"); | |
} | |
@Bean | |
public Queue requestQueue() { | |
return new Queue("my.request.queue"); | |
} | |
@Bean | |
public Queue replyQueue() { | |
return new Queue("my.reply.queue"); | |
} | |
public static class PojoListener { | |
public String handleMessage(String foo) { | |
return foo.toUpperCase(); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
SpringApplication.run(RequestReplyFixed.class, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This implementation sends asynchronous messages ?