initial commit
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package org.springframework.amqp.helloworld;
|
||||
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
public class Consumer {
|
||||
|
||||
public static void main(String[] args) {
|
||||
//ApplicationContext ctx = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
|
||||
|
||||
|
||||
ApplicationContext ctx = new AnnotationConfigApplicationContext(RabbitConfiguration.class);
|
||||
RabbitTemplate rabbitTemplate = (RabbitTemplate) ctx.getBean(RabbitTemplate.class);
|
||||
|
||||
System.out.println("Received " + rabbitTemplate.receiveAndConvert());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.springframework.amqp.helloworld;
|
||||
|
||||
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
|
||||
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ConsumerConfiguration extends RabbitConfiguration {
|
||||
|
||||
@Bean
|
||||
public SimpleMessageListenerContainer listenerContainer() {
|
||||
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
|
||||
container.setConnectionFactory(connectionFactory());
|
||||
container.setQueueName(helloWorldQueue.getName());
|
||||
container.setMessageListener(new MessageListenerAdapter(new HelloWorldHandler()));
|
||||
return container;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.springframework.amqp.helloworld;
|
||||
|
||||
public class HelloWorldHandler {
|
||||
|
||||
public void handleMessage(String text) {
|
||||
System.out.println("Received: " + text);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.springframework.amqp.helloworld;
|
||||
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
public class Producer {
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
ApplicationContext ctx = new AnnotationConfigApplicationContext(RabbitConfiguration.class);
|
||||
|
||||
RabbitTemplate rabbitTemplate = (RabbitTemplate) ctx.getBean(RabbitTemplate.class);
|
||||
rabbitTemplate.convertAndSend("Hello World");
|
||||
System.out.println("Sent Hello World.");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package org.springframework.amqp.helloworld;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.amqp.core.Queue;
|
||||
import org.springframework.amqp.rabbit.admin.config.AbstractRabbitConfiguration;
|
||||
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class RabbitConfiguration extends AbstractRabbitConfiguration {
|
||||
|
||||
protected Queue helloWorldQueue = new Queue("hello.world.queue");
|
||||
|
||||
@Bean
|
||||
public ConnectionFactory connectionFactory() {
|
||||
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
|
||||
connectionFactory.setUsername("guest");
|
||||
connectionFactory.setPassword("guest");
|
||||
return connectionFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RabbitTemplate rabbitTemplate() {
|
||||
RabbitTemplate template = new RabbitTemplate(connectionFactory());
|
||||
//The routing key is set to the name of the queue by the broker for the default exchange.
|
||||
template.setDefaultRoutingKey(helloWorldQueue.getName());
|
||||
//Where we will synchronously receive messages from
|
||||
template.setDefaultReceiveQueue(helloWorldQueue);
|
||||
return template;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void configureBroker() {
|
||||
declare(helloWorldQueue);
|
||||
}
|
||||
|
||||
/*
|
||||
@Bean
|
||||
public Queue helloWorldQueue()
|
||||
{
|
||||
return declare(helloWorldQueue);
|
||||
}*/
|
||||
|
||||
/*
|
||||
//Each queue is bound to the default direct exchange
|
||||
@Bean
|
||||
public Binding binding() {
|
||||
return declare(new Binding(helloWorldQueue(), defaultDirectExchange()));
|
||||
}*/
|
||||
|
||||
/*
|
||||
@Bean
|
||||
public TopicExchange helloExchange() {
|
||||
return declare(new TopicExchange("hello.world.exchange"));
|
||||
}*/
|
||||
|
||||
/*
|
||||
|
||||
public Queue declareUniqueQueue(String namePrefix) {
|
||||
Queue queue = new Queue(namePrefix + "-" + UUID.randomUUID());
|
||||
rabbitAdminTemplate().declareQueue(queue);
|
||||
return queue;
|
||||
}
|
||||
|
||||
// if the default exchange isn't configured to your liking....
|
||||
@Bean Binding declareP2PBinding(Queue queue, DirectExchange exchange) {
|
||||
return declare(new Binding(queue, exchange, queue.getName()));
|
||||
}
|
||||
|
||||
@Bean Binding declarePubSubBinding(String queuePrefix, FanoutExchange exchange) {
|
||||
return declare(new Binding(declareUniqueQueue(queuePrefix), exchange));
|
||||
}
|
||||
|
||||
@Bean Binding declarePubSubBinding(UniqueQueue uniqueQueue, TopicExchange exchange) {
|
||||
return declare(new Binding(uniqueQueue, exchange));
|
||||
}
|
||||
|
||||
@Bean Binding declarePubSubBinding(String queuePrefix, TopicExchange exchange, String routingKey) {
|
||||
return declare(new Binding(declareUniqueQueue(queuePrefix), exchange, routingKey));
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user