adding async example to helloworld sample
This commit is contained in:
@@ -7,13 +7,9 @@ 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());
|
||||
|
||||
ApplicationContext context = new AnnotationConfigApplicationContext(RabbitConfiguration.class);
|
||||
RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
|
||||
System.out.println("Received: " + rabbitTemplate.receiveAndConvert());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,16 +6,11 @@ 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.");
|
||||
|
||||
ApplicationContext context = new AnnotationConfigApplicationContext(RabbitConfiguration.class);
|
||||
RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
|
||||
rabbitTemplate.convertAndSend("Hello World");
|
||||
System.out.println("Sent: Hello World");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
public class RabbitConfiguration extends AbstractRabbitConfiguration {
|
||||
|
||||
protected final String helloWorldQueueName = "hello.world.queue";
|
||||
|
||||
|
||||
@Bean
|
||||
public ConnectionFactory connectionFactory() {
|
||||
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
|
||||
@@ -20,7 +20,7 @@ public class RabbitConfiguration extends AbstractRabbitConfiguration {
|
||||
connectionFactory.setPassword("guest");
|
||||
return connectionFactory;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public RabbitTemplate rabbitTemplate() {
|
||||
RabbitTemplate template = new RabbitTemplate(connectionFactory());
|
||||
@@ -50,7 +50,6 @@ public class RabbitConfiguration extends AbstractRabbitConfiguration {
|
||||
}*/
|
||||
|
||||
/*
|
||||
|
||||
public Queue declareUniqueQueue(String namePrefix) {
|
||||
Queue queue = new Queue(namePrefix + "-" + UUID.randomUUID());
|
||||
rabbitAdminTemplate().declareQueue(queue);
|
||||
@@ -74,6 +73,4 @@ public class RabbitConfiguration extends AbstractRabbitConfiguration {
|
||||
return declare(new Binding(declareUniqueQueue(queuePrefix), exchange, routingKey));
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.springframework.amqp.helloworld.async;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
public class Consumer {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.springframework.amqp.helloworld;
|
||||
package org.springframework.amqp.helloworld.async;
|
||||
|
||||
import org.springframework.amqp.helloworld.RabbitConfiguration;
|
||||
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
|
||||
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -7,7 +8,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ConsumerConfiguration extends RabbitConfiguration {
|
||||
|
||||
|
||||
@Bean
|
||||
public SimpleMessageListenerContainer listenerContainer() {
|
||||
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
|
||||
@@ -16,5 +17,5 @@ public class ConsumerConfiguration extends RabbitConfiguration {
|
||||
container.setMessageListener(new MessageListenerAdapter(new HelloWorldHandler()));
|
||||
return container;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
package org.springframework.amqp.helloworld;
|
||||
package org.springframework.amqp.helloworld.async;
|
||||
|
||||
public class HelloWorldHandler {
|
||||
|
||||
public void handleMessage(String text) {
|
||||
System.out.println("Received: " + text);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.springframework.amqp.helloworld.async;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
public class Producer {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
new AnnotationConfigApplicationContext(ProducerConfiguration.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package org.springframework.amqp.helloworld.async;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.springframework.amqp.core.Queue;
|
||||
import org.springframework.amqp.rabbit.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.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor;
|
||||
|
||||
@Configuration
|
||||
public class ProducerConfiguration extends AbstractRabbitConfiguration {
|
||||
|
||||
protected final String helloWorldQueueName = "hello.world.queue";
|
||||
|
||||
@Override
|
||||
public RabbitTemplate rabbitTemplate() {
|
||||
RabbitTemplate template = new RabbitTemplate(connectionFactory());
|
||||
template.setRoutingKey(this.helloWorldQueueName);
|
||||
template.setQueue(this.helloWorldQueueName);
|
||||
return template;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ConnectionFactory connectionFactory() {
|
||||
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
|
||||
connectionFactory.setUsername("guest");
|
||||
connectionFactory.setPassword("guest");
|
||||
return connectionFactory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Queue helloWorldQueue() {
|
||||
return new Queue(this.helloWorldQueueName);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ScheduledProducer scheduledProducer() {
|
||||
return new ScheduledProducer();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public BeanPostProcessor postProcessor() {
|
||||
return new ScheduledAnnotationBeanPostProcessor();
|
||||
}
|
||||
|
||||
|
||||
static class ScheduledProducer {
|
||||
|
||||
@Autowired
|
||||
private volatile RabbitTemplate rabbitTemplate;
|
||||
|
||||
private final AtomicInteger counter = new AtomicInteger();
|
||||
|
||||
@Scheduled(fixedRate = 3000)
|
||||
public void sendMessage() {
|
||||
rabbitTemplate.convertAndSend("Hello World " + counter.incrementAndGet());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,6 +6,6 @@ log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
#log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
|
||||
log4j.appender.stdout.layout.ConversionPattern=%-5p [%40.40c{4}]: %m%n
|
||||
|
||||
log4j.category.org.springframework.amqp.rabbit=DEBUG
|
||||
log4j.category.org.springframework.amqp.rabbit=INFO
|
||||
log4j.category.org.springframework.beans.factory=INFO
|
||||
|
||||
|
||||
Reference in New Issue
Block a user