initial commit

This commit is contained in:
markpollack
2010-06-29 22:16:39 -04:00
committed by Dave Syer
parent ff3fec90fd
commit 933acbe493
52 changed files with 2429 additions and 0 deletions

View File

@@ -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());
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,8 @@
package org.springframework.amqp.helloworld;
public class HelloWorldHandler {
public void handleMessage(String text) {
System.out.println("Received: " + text);
}
}

View File

@@ -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.");
}
}

View File

@@ -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));
}*/
}