From 796d402f679578f7635c75bcabf519608b2e4958 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Sun, 13 Oct 2019 02:10:16 -0400 Subject: [PATCH] Read file from conf --- spring-amqp/spring-amqp.gradle | 8 ++ .../springamqp/samplewithconf/AMQPConf.java | 94 +++++++++++++++++++ .../AMQPConfHelloWorldMessageApp.java | 47 ++++++++++ .../simple/AMQPHelloWorldMessageApp.java | 2 +- 4 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 spring-amqp/src/main/java/com/ossez/springamqp/samplewithconf/AMQPConf.java create mode 100644 spring-amqp/src/main/java/com/ossez/springamqp/samplewithconf/AMQPConfHelloWorldMessageApp.java diff --git a/spring-amqp/spring-amqp.gradle b/spring-amqp/spring-amqp.gradle index 8887d0501c..88d60a6721 100644 --- a/spring-amqp/spring-amqp.gradle +++ b/spring-amqp/spring-amqp.gradle @@ -1 +1,9 @@ description = "Spring Amqp" + +dependencies { + // SPRING + compile 'org.springframework.boot:spring-boot-starter-amqp:2.1.8.RELEASE' + + testCompile group: 'org.awaitility', name: 'awaitility', version: '4.0.1' +} + diff --git a/spring-amqp/src/main/java/com/ossez/springamqp/samplewithconf/AMQPConf.java b/spring-amqp/src/main/java/com/ossez/springamqp/samplewithconf/AMQPConf.java new file mode 100644 index 0000000000..258f7ae3cb --- /dev/null +++ b/spring-amqp/src/main/java/com/ossez/springamqp/samplewithconf/AMQPConf.java @@ -0,0 +1,94 @@ +package com.ossez.springamqp.samplewithconf; + +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.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.HashMap; +import java.util.Map; + +@Configuration +public class AMQPConf { + public static final String MY_QUEUE_NAME = "com.ossez.real.estate"; + private static final String MY_QUEUE_HOSTMANME = "mq.ossez.com"; + private static final String MY_QUEUE_USERNAME = "username"; + private static final String MY_QUEUE_PASSWORD = "password"; + + @Bean + public ConnectionFactory connectionFactory() { + CachingConnectionFactory connectionFactory = new CachingConnectionFactory(MY_QUEUE_HOSTMANME); + connectionFactory.setUsername(MY_QUEUE_USERNAME); + connectionFactory.setPassword(MY_QUEUE_PASSWORD); + + return connectionFactory; + } + + @Bean + public AmqpAdmin amqpAdmin() { + return new RabbitAdmin(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 + // Every queue is bound to the default direct exchange + public Queue helloWorldQueue() { + Map args = new HashMap<>(); + // set the queue with a dead letter feature + args.put("x-queue-type", "classic"); + Queue queue = new Queue(MY_QUEUE_NAME, true, false, false, args); + +// Queue queue = new Queue(this.helloWorldQueueName); + + return queue; + } + + /* + @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)); + }*/ + +} diff --git a/spring-amqp/src/main/java/com/ossez/springamqp/samplewithconf/AMQPConfHelloWorldMessageApp.java b/spring-amqp/src/main/java/com/ossez/springamqp/samplewithconf/AMQPConfHelloWorldMessageApp.java new file mode 100644 index 0000000000..c1b9ce49ef --- /dev/null +++ b/spring-amqp/src/main/java/com/ossez/springamqp/samplewithconf/AMQPConfHelloWorldMessageApp.java @@ -0,0 +1,47 @@ +package com.ossez.springamqp.samplewithconf; + +import org.springframework.amqp.core.AmqpTemplate; +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.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author YuCheng Hu + */ +@SpringBootApplication +public class AMQPConfHelloWorldMessageApp { + + + public static void main(String[] args) { + SpringApplication.run(AMQPConfHelloWorldMessageApp.class, args); + } + + + @Bean + public ApplicationRunner runner(RabbitTemplate rabbitTemplate) { + return args -> { + for (int i = 0; i < 10000; i++) { + rabbitTemplate.convertAndSend("Hello, world - OSSEZ.COM + CONF! - " + i); + } + }; + } + + + @RabbitListener(queues = AMQPConf.MY_QUEUE_NAME) + public void listen(String in) { + System.out.println("Message read from myQueue : " + in); + } + +} 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 index 9ccef845a2..ff3324bef0 100644 --- a/spring-amqp/src/main/java/com/ossez/springamqp/simple/AMQPHelloWorldMessageApp.java +++ b/spring-amqp/src/main/java/com/ossez/springamqp/simple/AMQPHelloWorldMessageApp.java @@ -66,7 +66,7 @@ public class AMQPHelloWorldMessageApp { public ApplicationRunner runner(RabbitTemplate template) { return args -> { for (int i = 0; i < 1000; i++) { - template.convertAndSend("Hello, world - OSSEZ.COM!" + i); + template.convertAndSend("Hello, world - OSSEZ.COM! >> " + i); } };