Browse files

init

  • Loading branch information...
0 parents commit 47d588b3177139601486395817d88b6cf820a701 John Simone committed Nov 20, 2011
4 .gitignore
@@ -0,0 +1,4 @@
+.classpath
+.settings/
+target
+.project
2 Procfile
@@ -0,0 +1,2 @@
+sender: sh target/bin/sender
+receiver: sh target/bin/receiver
57 pom.xml
@@ -0,0 +1,57 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>com.heroku.devcenter</groupId>
+ <artifactId>rabbitMqSample</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+
+ <dependencies>
+ <dependency>
+ <groupId>com.rabbitmq</groupId>
+ <artifactId>amqp-client</artifactId>
+ <version>2.7.0</version>
+ </dependency>
+
+ <!-- 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>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>appassembler-maven-plugin</artifactId>
+ <version>1.1.1</version>
+ <configuration>
+ <assembleDirectory>target</assembleDirectory>
+ <programs>
+ <program>
+ <mainClass>com.heroku.devcenter.PojoSender</mainClass>
+ <name>sender</name>
+ </program>
+ <program>
+ <mainClass>com.heroku.devcenter.PojoReceiver</mainClass>
+ <name>receiver</name>
+ </program>
+ </programs>
+ </configuration>
+ <executions>
+ <execution>
+ <phase>package</phase>
+ <goals>
+ <goal>assemble</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
34 src/main/java/com/heroku/devcenter/PojoReceiver.java
@@ -0,0 +1,34 @@
+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;
+
+public class PojoReceiver {
+
+ public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
+ ConnectionFactory factory = RabbitFactoryUtil.getConnectionFactory();
+ Connection connection = factory.newConnection();
+ Channel channel = connection.createChannel();
+ String exchangeName = "sample-exchange";
+ String queueName = "sample-queue";
+ String routingKey = "sample-key";
+ channel.exchangeDeclare(exchangeName, "direct", true);
+ channel.queueDeclare(queueName, true, false, false, null);
+ channel.queueBind(queueName, exchangeName, routingKey);
+
+ 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"));
+ }
+ }
+
+ }
+}
32 src/main/java/com/heroku/devcenter/PojoSender.java
@@ -0,0 +1,32 @@
+package com.heroku.devcenter;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+
+import com.rabbitmq.client.Channel;
+import com.rabbitmq.client.Connection;
+import com.rabbitmq.client.ConnectionFactory;
+
+public class PojoSender {
+
+ public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
+ ConnectionFactory factory = RabbitFactoryUtil.getConnectionFactory();
+ Connection connection = factory.newConnection();
+ Channel channel = connection.createChannel();
+ String exchangeName = "sample-exchange";
+ String queueName = "sample-queue";
+ String routingKey = "sample-key";
+ channel.exchangeDeclare(exchangeName, "direct", true);
+ channel.queueDeclare(queueName, true, false, false, null);
+ channel.queueBind(queueName, exchangeName, routingKey);
+
+ while (true) {
+ String msg = "Sent at:" + System.currentTimeMillis();
+ byte[] body = msg.getBytes("UTF-8");
+ channel.basicPublish(exchangeName, routingKey, null, body);
+ System.out.println("Sent:->" + msg);
+ Thread.sleep(1000);
+ }
+
+ }
+}
24 src/main/java/com/heroku/devcenter/RabbitFactoryUtil.java
@@ -0,0 +1,24 @@
+package com.heroku.devcenter;
+
+import static java.lang.System.getenv;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import com.rabbitmq.client.ConnectionFactory;
+
+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;
+ }
+
+}

0 comments on commit 47d588b

Please sign in to comment.