Skip to content

Instantly share code, notes, and snippets.

@michaelklishin
Created February 4, 2014 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelklishin/d33ad46f81ff591f0988 to your computer and use it in GitHub Desktop.
Save michaelklishin/d33ad46f81ff591f0988 to your computer and use it in GitHub Desktop.
package com.mycomp.reply;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Consumer {
public static void main(String[] args) {
new AnnotationConfigApplicationContext(ResponseConsumerConfiguration.class);
}
}
package com.mycomp.reply;
import java.util.ArrayList;
import java.util.List;
public class ConsumerMessageHandler {
public List<String> handleMessage(String request) {
System.out.println("Message Received at consumer end : " + request);
List<String> list = new ArrayList<String>();
list.add("Hello.....");
list.add("This is my response....");
return list;
}
}
package com.mycomp.request;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Producer {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(
RequestProducerConfiguration.class);
AmqpTemplate amqpTemplate = context.getBean(RabbitTemplate.class);
System.out.println("Response received : " + amqpTemplate
.convertSendAndReceive("Sending request....."));
}
}
package com.mycomp.request;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
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.support.converter.JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RequestProducerConfiguration {
protected final String requestQueueName = "request.queue";
protected final String replyQueueName = "reply.queue";
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
return connectionFactory;
}
@Bean
public AmqpAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());
}
@Bean
public RabbitTemplate amqpTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
template.setMessageConverter(new JsonMessageConverter());
template.setRoutingKey(this.requestQueueName);
template.setReplyQueue(replyQueue());
template.setReplyTimeout(60000);
return template;
}
@Bean
public SimpleMessageListenerContainer replyListenerContainer() {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory());
container.setQueues(replyQueue());
container.setMessageListener(amqpTemplate());
return container;
}
@Bean
public Queue replyQueue() {
return new Queue(this.replyQueueName);
}
@Bean
public Queue requestQueue() {
return new Queue(this.requestQueueName);
}
}
package com.mycomp.reply;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.amqp.support.converter.JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ResponseConsumerConfiguration {
protected final String requestQueueName = "request.queue";
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(
"localhost");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
return connectionFactory;
}
@Bean
public SimpleMessageListenerContainer listenerContainer() {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory());
container.setQueueNames(this.requestQueueName);
container.setMessageListener(new MessageListenerAdapter(new ConsumerMessageHandler(), new JsonMessageConverter()));
return container;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment