BAEL-4908 added code for pubsub vs message queues tutorial
This commit is contained in:
parent
a50be6d21c
commit
628cfcdea1
|
@ -9,20 +9,42 @@
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-modules</artifactId>
|
<artifactId>parent-boot-2</artifactId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../parent-boot-2</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>${spring-cloud-dependencies.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.rabbitmq</groupId>
|
<groupId>com.rabbitmq</groupId>
|
||||||
<artifactId>amqp-client</artifactId>
|
<artifactId>amqp-client</artifactId>
|
||||||
<version>${amqp-client.version}</version>
|
<version>${amqp-client.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<amqp-client.version>5.12.0</amqp-client.version>
|
<amqp-client.version>5.12.0</amqp-client.version>
|
||||||
|
<spring-cloud-dependencies.version>2020.0.3</spring-cloud-dependencies.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.baeldung.pubsubmq.client;
|
||||||
|
|
||||||
|
import org.springframework.amqp.core.Queue;
|
||||||
|
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||||
|
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
|
||||||
|
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class ClientApplication {
|
||||||
|
private static final String MESSAGE_QUEUE = "pizza-message-queue";
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Queue queue() {
|
||||||
|
return new Queue(MESSAGE_QUEUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
|
||||||
|
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
|
||||||
|
container.setConnectionFactory(connectionFactory);
|
||||||
|
container.setQueueNames(MESSAGE_QUEUE);
|
||||||
|
container.setMessageListener(listenerAdapter);
|
||||||
|
return container;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Consumer consumer() {
|
||||||
|
return new Consumer();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public MessageListenerAdapter listenerAdapter(Consumer consumer) {
|
||||||
|
return new MessageListenerAdapter(consumer, "receiveOrder");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(ClientApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.baeldung.pubsubmq.client;
|
||||||
|
|
||||||
|
public class Consumer {
|
||||||
|
public void receiveOrder(String message) {
|
||||||
|
System.out.printf("Order received: %s%n", message);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package com.baeldung.pubsubmq.server;
|
||||||
|
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
|
public class Publisher {
|
||||||
|
|
||||||
|
private RabbitTemplate rabbitTemplate;
|
||||||
|
private String queue;
|
||||||
|
private String topic;
|
||||||
|
|
||||||
|
public Publisher(RabbitTemplate rabbitTemplate, String queue, String topic) {
|
||||||
|
this.rabbitTemplate = rabbitTemplate;
|
||||||
|
this.queue = queue;
|
||||||
|
this.topic = topic;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void postMessages() {
|
||||||
|
rabbitTemplate.convertAndSend(queue, "1 Pepperoni");
|
||||||
|
rabbitTemplate.convertAndSend(queue, "3 Margarita");
|
||||||
|
rabbitTemplate.convertAndSend(queue, "1 Ham and Pineapple (yuck)");
|
||||||
|
|
||||||
|
rabbitTemplate.convertAndSend(topic, "notification", "New Deal on T-Shirts: 95% off!");
|
||||||
|
rabbitTemplate.convertAndSend(topic, "notification", "2 for 1 on all Jeans!");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,57 @@
|
||||||
|
package com.baeldung.pubsubmq.server;
|
||||||
|
|
||||||
|
import org.springframework.amqp.core.Binding;
|
||||||
|
import org.springframework.amqp.core.BindingBuilder;
|
||||||
|
import org.springframework.amqp.core.Queue;
|
||||||
|
import org.springframework.amqp.core.TopicExchange;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class ServerApplication {
|
||||||
|
private static final String MESSAGE_QUEUE = "pizza-message-queue";
|
||||||
|
private static final String PUB_SUB_TOPIC = "notification-topic";
|
||||||
|
private static final String PUB_SUB_EMAIL_QUEUE = "email-queue";
|
||||||
|
private static final String PUB_SUB_TEXT_QUEUE = "text-queue";
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Queue queue() {
|
||||||
|
return new Queue(MESSAGE_QUEUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Queue emailQueue() {
|
||||||
|
return new Queue(PUB_SUB_EMAIL_QUEUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Queue textQueue() {
|
||||||
|
return new Queue(PUB_SUB_TEXT_QUEUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public TopicExchange exchange() {
|
||||||
|
return new TopicExchange(PUB_SUB_TOPIC);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Binding emailBinding(Queue emailQueue, TopicExchange exchange) {
|
||||||
|
return BindingBuilder.bind(emailQueue).to(exchange).with("notification");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Binding textBinding(Queue textQueue, TopicExchange exchange) {
|
||||||
|
return BindingBuilder.bind(textQueue).to(exchange).with("notification");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Publisher publisher(RabbitTemplate rabbitTemplate) {
|
||||||
|
return new Publisher(rabbitTemplate, MESSAGE_QUEUE, PUB_SUB_TOPIC);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(ServerApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue