Skip to content

Commit d260fb2

Browse files
Aram89pivovarit
authored andcommitted
Added new module to demonstrate topic "Introduction to RabbitMQ". (eugenp#1197)
1 parent 9dab68a commit d260fb2

4 files changed

Lines changed: 104 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@
199199
<module>apache-velocity</module>
200200
<module>apache-solrj</module>
201201

202+
<module>rabbitmq</module>
203+
204+
202205
</modules>
203206

204207
<build>

‎rabbitmq/pom.xml‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>rabbitmq</artifactId>
8+
<version>0.1-SNAPSHOT</version>
9+
10+
<name>rabbitmq</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>com.rabbitmq</groupId>
16+
<artifactId>amqp-client</artifactId>
17+
<version>3.6.6</version>
18+
</dependency>
19+
</dependencies>
20+
<build>
21+
<pluginManagement>
22+
<plugins>
23+
<plugin>
24+
<groupId>org.apache.maven.plugins</groupId>
25+
<artifactId>maven-compiler-plugin</artifactId>
26+
<version>${maven-compiler-plugin.version}</version>
27+
<configuration>
28+
<source>1.8</source>
29+
<target>1.8</target>
30+
</configuration>
31+
</plugin>
32+
</plugins>
33+
34+
</pluginManagement>
35+
</build>
36+
37+
<properties>
38+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
39+
40+
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
41+
</properties>
42+
43+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.consumer;
2+
3+
import com.rabbitmq.client.*;
4+
5+
import java.io.IOException;
6+
import java.util.concurrent.TimeoutException;
7+
8+
public class Receiver {
9+
10+
private static final String QUEUE_NAME = "products_queue";
11+
12+
public static void main (String[] args) throws IOException, TimeoutException {
13+
ConnectionFactory factory = new ConnectionFactory();
14+
factory.setHost("localhost");
15+
Connection connection = factory.newConnection();
16+
Channel channel = connection.createChannel();
17+
18+
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
19+
20+
Consumer consumer = new DefaultConsumer(channel) {
21+
@Override
22+
public void handleDelivery(String consumerTag,
23+
Envelope envelope, AMQP.BasicProperties properties,
24+
byte[] body) throws IOException {
25+
String message = new String(body, "UTF-8");
26+
System.out.println(" [x] Received '" + message + "'");
27+
}
28+
};
29+
channel.basicConsume(QUEUE_NAME, true, consumer);
30+
}
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.producer;
2+
3+
import com.rabbitmq.client.*;
4+
5+
import java.io.IOException;
6+
import java.util.concurrent.TimeoutException;
7+
8+
public class Publisher {
9+
10+
private final static String QUEUE_NAME = "products_queue";
11+
12+
public static void main(String[]args) throws IOException, TimeoutException {
13+
ConnectionFactory factory = new ConnectionFactory();
14+
factory.setHost("localhost");
15+
Connection connection = factory.newConnection();
16+
Channel channel = connection.createChannel();
17+
18+
String message = "product details";
19+
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
20+
21+
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
22+
System.out.println(" [x] Sent '" + message + "'");
23+
24+
channel.close();
25+
connection.close();
26+
}
27+
}

0 commit comments

Comments
 (0)