Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add README

  • Loading branch information...
commit a5820a0f9be0e53ce1a17d0e90a19e74a0a180f6 1 parent 47d588b
John Simone authored
View
121 README.md
@@ -0,0 +1,121 @@
+## Using RabbitMQ from Java
+
+RabbitMQ provides a standard Java client. In order to use the client in your project you have to declare the dependency in your build and initialize the connection from the environment variable that Heroku provides to your application.
+
+### Add the RabbitMQ client to Your Pom.xml
+
+Add the following dependency to your pom.xml in order to use the RabbitMQ client:
+
+ <dependency>
+ <groupId>com.rabbitmq</groupId>
+ <artifactId>amqp-client</artifactId>
+ <version>2.7.0</version>
+ </dependency>
+
+### Use RabbitMQ in Your Application
+
+Create the RabbitMQ connection factory:
+
+ :::java
+ public class RabbitFactoryUtil {
+ public static ConnectionFactory getConnectionFactory() throws URISyntaxException {
+ ConnectionFactory factory = new ConnectionFactory();
+ URI uri = new URI(getenv("RABBITMQ_URL"));
+ factory.setUsername(uri.getUserInfo().split(":")[0]);
+ factory.setPassword(uri.getUserInfo().split(":")[1]);
+ factory.setHost(uri.getHost());
+ factory.setPort(uri.getPort());
+ factory.setVirtualHost(uri.getPath().substring(1));
+ return factory;
+ }
+ }
+
+Send message to a queue:
+
+ :::java
+ Connection connection = factory.newConnection();
+ Channel channel = connection.createChannel();
+ channel.exchangeDeclare("sample-exchange", "direct", true);
+ channel.queueDeclare("sample-queue", true, false, false, null);
+ channel.queueBind("sample-queue", "sample-exchange", "sample-key");
+ channel.basicPublish("sample-exchange", "sample-key", null, "sample message".getBytes("UTF-8");
+
+Receive message from a queue:
+
+ :::java
+ Connection connection = factory.newConnection();
+ Channel channel = connection.createChannel();
+ channel.exchangeDeclare("sample-exchange", "direct", true);
+ channel.queueDeclare("sample-queue", true, false, false, null);
+ channel.queueBind("sample-queue", "sample-exchange", "sample-key");
+ QueueingConsumer consumer = new QueueingConsumer(channel);
+ channel.basicConsume(queueName, true, consumer);
+
+ while (true) {
+ //consumer.nextDelivery will block until it receives a message
+ QueueingConsumer.Delivery delivery = consumer.nextDelivery();
+ }
+
+### Using RabbitMQ with Spring
+
+Spring provides a rabbit template that can be used to easily connect to RabbitMQ. These allow much of the setup required each time a queue connection is made to be moved into Spring configuration and kept in the template classes within the Spring container. There are XML namespaces that make the configuration easier as well.
+
+To use the template first declare it in your pom.xml:
+
+ <dependency>
+ <groupId>org.springframework.amqp</groupId>
+ <artifactId>spring-rabbit</artifactId>
+ <version>1.0.0.RELEASE</version>
+ </dependency>
+
+Application context XML to configure the rabbit template:
+
+ <beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:context="http://www.springframework.org/schema/context"
+ xmlns:rabbit="http://www.springframework.org/schema/rabbit"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
+ http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"
+ default-autowire="byName">
+
+ <context:property-placeholder/>
+
+ <bean id="cf" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
+ <!-- This constructor arg utilizes the RabbitFactoryUtil class shown in the java example above -->
+ <constructor-arg><value>#{ T(com.heroku.devcenter.RabbitFactoryUtil).getConnectionFactory()}</value></constructor-arg>
+ </bean>
+
+ <rabbit:queue id="sample-queue" durable="true" auto-delete="false" exclusive="false" name="sample-queue"/>
+
+ <rabbit:direct-exchange name="sample-exchange" durable="true" auto-delete="false" id="sample-exchange">
+ <rabbit:bindings>
+ <rabbit:binding queue="sample-queue" key="sample-key"/>
+ </rabbit:bindings>
+ </rabbit:direct-exchange>
+
+ <bean id="template" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
+ <property name="connectionFactory" ref="cf"/>
+ <property name="exchange" value="sample-exchange"/>
+ <property name="queue" value="sample-queue"/>
+ <property name="routingKey" value="sample-key"/>
+ </bean>
+
+ </beans>
+
+Sending messages with rabbit template:
+
+ :::java
+ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
+ RabbitTemplate rabbitTemplate = ctx.getBean(RabbitTemplate.class);
+ rabbitTemplate.send(new Message("sample message".getBytes("UTF-8"), new MessageProperties()));
+
+Receiving messages with rabbit template:
+
+ :::java
+ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
+ RabbitTemplate rabbitTemplate = ctx.getBean(RabbitTemplate.class);
+ Message response = rabbitTemplate.receive();
+
+
+You can also download the [sample code](http://github.com/heroku/devcenter-rabbitmq-java)
View
5 pom.xml
@@ -14,11 +14,6 @@
<!-- Required only if using Spring -->
<dependency>
<groupId>org.springframework.amqp</groupId>
- <artifactId>spring-amqp</artifactId>
- <version>1.0.0.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
View
13 src/main/java/com/heroku/devcenter/PojoReceiver.java
@@ -1,13 +1,13 @@
package com.heroku.devcenter;
import java.io.IOException;
-import static java.lang.System.getenv;
import java.net.URISyntaxException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.GetResponse;
+import com.rabbitmq.client.QueueingConsumer;
public class PojoReceiver {
@@ -21,12 +21,15 @@ public static void main(String[] args) throws IOException, InterruptedException,
channel.exchangeDeclare(exchangeName, "direct", true);
channel.queueDeclare(queueName, true, false, false, null);
channel.queueBind(queueName, exchangeName, routingKey);
-
+ QueueingConsumer consumer = new QueueingConsumer(channel);
+ channel.basicConsume(queueName, true, consumer);
+
while (true) {
System.out.println("Waiting for message...");
- GetResponse response = channel.basicGet(queueName,true);
- if (response != null) {
- System.out.println("Recieved:->" + new String(response.getBody(), "UTF-8"));
+ //consumer.nextDelivery will block until it receives a message
+ QueueingConsumer.Delivery delivery = consumer.nextDelivery();
+ if (delivery != null) {
+ System.out.println("Recieved:->" + new String(delivery.getBody(), "UTF-8"));
}
}
View
23 src/main/java/com/heroku/devcenter/SpringReceiver.java
@@ -0,0 +1,23 @@
+package com.heroku.devcenter;
+
+import java.io.UnsupportedEncodingException;
+
+import org.springframework.amqp.core.Message;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+public class SpringReceiver {
+
+ public static void main(String[] args) throws UnsupportedEncodingException, InterruptedException {
+ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
+ RabbitTemplate rabbitTemplate = ctx.getBean(RabbitTemplate.class);
+ while (true) {
+ System.out.println("Checking for message...");
+ Message response = rabbitTemplate.receive();
+ if (response != null) {
+ System.out.println("Spring Recieved:->" + new String(response.getBody(), "UTF-8"));
+ }
+ Thread.sleep(500);
+ }
+ }
+}
View
23 src/main/java/com/heroku/devcenter/SpringSender.java
@@ -0,0 +1,23 @@
+package com.heroku.devcenter;
+
+import java.io.UnsupportedEncodingException;
+
+import org.springframework.amqp.core.Message;
+import org.springframework.amqp.core.MessageProperties;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+public class SpringSender {
+
+ public static void main(String[] args) throws UnsupportedEncodingException, InterruptedException {
+ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
+ RabbitTemplate rabbitTemplate = ctx.getBean(RabbitTemplate.class);
+ while(true){
+ String msg = "Spring Sent at:" + System.currentTimeMillis();
+ System.out.println(msg);
+ byte[] body = msg.getBytes("UTF-8");
+ rabbitTemplate.send(new Message(body, new MessageProperties()));
+ Thread.sleep(1000);
+ }
+ }
+}
View
32 src/main/resources/applicationContext.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:context="http://www.springframework.org/schema/context"
+ xmlns:rabbit="http://www.springframework.org/schema/rabbit"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
+ http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"
+ default-autowire="byName">
+
+ <context:property-placeholder/>
+
+ <bean id="cf" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
+ <constructor-arg><value>#{ T(com.heroku.devcenter.RabbitFactoryUtil).getConnectionFactory()}</value></constructor-arg>
+ </bean>
+
+ <rabbit:queue id="sample-queue" durable="true" auto-delete="false" exclusive="false" name="sample-queue"/>
+
+ <rabbit:direct-exchange name="sample-exchange" durable="true" auto-delete="false" id="sample-exchange">
+ <rabbit:bindings>
+ <rabbit:binding queue="sample-queue" key="sample-key"/>
+ </rabbit:bindings>
+ </rabbit:direct-exchange>
+
+ <bean id="template" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
+ <property name="connectionFactory" ref="cf"/>
+ <property name="exchange" value="sample-exchange"/>
+ <property name="queue" value="sample-queue"/>
+ <property name="routingKey" value="sample-key"/>
+ </bean>
+
+</beans>
Please sign in to comment.
Something went wrong with that request. Please try again.