From a13f0afaa5ebed2274eff85b3df8b6b8a37d7605 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Sun, 13 Oct 2019 01:21:04 -0400 Subject: [PATCH] Change the conf to make this can run as spring boot --- .../simple/AMQPHelloWorldMessageApp.java | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 spring-amqp/src/main/java/com/ossez/springamqp/simple/AMQPHelloWorldMessageApp.java diff --git a/spring-amqp/src/main/java/com/ossez/springamqp/simple/AMQPHelloWorldMessageApp.java b/spring-amqp/src/main/java/com/ossez/springamqp/simple/AMQPHelloWorldMessageApp.java new file mode 100644 index 0000000000..852396009d --- /dev/null +++ b/spring-amqp/src/main/java/com/ossez/springamqp/simple/AMQPHelloWorldMessageApp.java @@ -0,0 +1,87 @@ +package com.ossez.springamqp.simple; + +import org.springframework.amqp.core.Queue; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; +import org.springframework.amqp.rabbit.connection.ConnectionFactory; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +import java.util.HashMap; +import java.util.Map; + +@SpringBootApplication +public class AMQPHelloWorldMessageApp { + + private static final boolean NON_DURABLE = true; + private static final String MY_QUEUE_NAME = "com.ossez.real.estate"; + + public static void main(String[] args) { + +// ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class); +// AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class); +// +// amqpTemplate.convertAndSend("Hello World"); +// System.out.println("Sent: Hello World"); + + SpringApplication.run(AMQPHelloWorldMessageApp.class, args); + } + + @Bean + public ConnectionFactory connectionFactory() { + CachingConnectionFactory connectionFactory = new CachingConnectionFactory("158.69.60.50"); + connectionFactory.setUsername("admin"); + connectionFactory.setPassword("Lucas#1120"); + + return connectionFactory; + } + + @Bean + 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.setRoutingKey(MY_QUEUE_NAME); + + //Where we will synchronously receive messages from + template.setDefaultReceiveQueue(MY_QUEUE_NAME); + return template; + } + + @Bean + Queue myQueue() { + Map args = new HashMap<>(); +// // set the queue with a dead letter feature +// args.put("x-queue-type", "classic"); + + return new Queue(MY_QUEUE_NAME, NON_DURABLE, false, false, args); +// return new Queue(MY_QUEUE_NAME, NON_DURABLE); + + + + } + + + @Bean + public ApplicationRunner runner(RabbitTemplate template) { + return args -> { + for (int i =0; i<1000; i++) { + template.convertAndSend("Hello, world- YuCheng Hu!"+ i); + } + + }; + } + + + + @RabbitListener(queues = MY_QUEUE_NAME) + public void listen(String in) { + System.out.println("Message read from myQueue : " + in); + } + +}