diff --git a/pom.xml b/pom.xml index 9e95537775..1b7d0b419c 100644 --- a/pom.xml +++ b/pom.xml @@ -199,6 +199,9 @@ apache-velocity apache-solrj + rabbitmq + + diff --git a/rabbitmq/pom.xml b/rabbitmq/pom.xml new file mode 100644 index 0000000000..03f192e4e1 --- /dev/null +++ b/rabbitmq/pom.xml @@ -0,0 +1,43 @@ + + + + 4.0.0 + com.baeldung + rabbitmq + 0.1-SNAPSHOT + + rabbitmq + http://maven.apache.org + + + + com.rabbitmq + amqp-client + 3.6.6 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + + + + + UTF-8 + + 3.6.0 + + + \ No newline at end of file diff --git a/rabbitmq/src/main/java/com/baeldung/consumer/Receiver.java b/rabbitmq/src/main/java/com/baeldung/consumer/Receiver.java new file mode 100644 index 0000000000..d0612406e9 --- /dev/null +++ b/rabbitmq/src/main/java/com/baeldung/consumer/Receiver.java @@ -0,0 +1,31 @@ +package com.baeldung.consumer; + +import com.rabbitmq.client.*; + +import java.io.IOException; +import java.util.concurrent.TimeoutException; + +public class Receiver { + + private static final String QUEUE_NAME = "products_queue"; + + public static void main (String[] args) throws IOException, TimeoutException { + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost("localhost"); + Connection connection = factory.newConnection(); + Channel channel = connection.createChannel(); + + channel.queueDeclare(QUEUE_NAME, false, false, false, null); + + Consumer consumer = new DefaultConsumer(channel) { + @Override + public void handleDelivery(String consumerTag, + Envelope envelope, AMQP.BasicProperties properties, + byte[] body) throws IOException { + String message = new String(body, "UTF-8"); + System.out.println(" [x] Received '" + message + "'"); + } + }; + channel.basicConsume(QUEUE_NAME, true, consumer); + } +} diff --git a/rabbitmq/src/main/java/com/baeldung/producer/Publisher.java b/rabbitmq/src/main/java/com/baeldung/producer/Publisher.java new file mode 100644 index 0000000000..f9130c5d86 --- /dev/null +++ b/rabbitmq/src/main/java/com/baeldung/producer/Publisher.java @@ -0,0 +1,27 @@ +package com.baeldung.producer; + +import com.rabbitmq.client.*; + +import java.io.IOException; +import java.util.concurrent.TimeoutException; + +public class Publisher { + + private final static String QUEUE_NAME = "products_queue"; + + public static void main(String[]args) throws IOException, TimeoutException { + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost("localhost"); + Connection connection = factory.newConnection(); + Channel channel = connection.createChannel(); + + String message = "product details"; + channel.queueDeclare(QUEUE_NAME, false, false, false, null); + + channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); + System.out.println(" [x] Sent '" + message + "'"); + + channel.close(); + connection.close(); + } +}