Permalink
Browse files

Barely working message passing from web to worker

  • Loading branch information...
1 parent 9210972 commit 0a01d143a8b742e6ef3a441809a452652a56f5f7 @ryanbrainard ryanbrainard committed Jun 6, 2012
View
28 common/src/main/java/com/heroku/devcenter/BigOperation.java
@@ -0,0 +1,28 @@
+package com.heroku.devcenter;
+
+import java.io.Serializable;
+
+/**
+ * @author Ryan Brainard
+ */
+public class BigOperation implements Serializable {
+
+ public static final String QUEUE_NAME = "myqueue";
+
+ String name;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public String toString() {
+ return "BigOperation{" +
+ "name='" + name + '\'' +
+ '}';
+ }
+}
View
14 ...a/com/heroku/devcenter/RabbitFactory.java → ...ava/com/heroku/devcenter/RabbitUtils.java
@@ -1,13 +1,23 @@
package com.heroku.devcenter;
import com.rabbitmq.client.ConnectionFactory;
+import org.springframework.amqp.core.AmqpTemplate;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.GenericXmlApplicationContext;
import java.net.URI;
import java.net.URISyntaxException;
import static java.lang.System.getenv;
-public class RabbitFactory {
+public class RabbitUtils {
+
+ private RabbitUtils(){}
+
+ public static AmqpTemplate getTemplate() {
+ ApplicationContext context = new GenericXmlApplicationContext("classpath:/rabbit-context.xml");
+ return context.getBean(AmqpTemplate.class);
+ }
public static ConnectionFactory getConnectionFactory() throws URISyntaxException {
ConnectionFactory factory = new ConnectionFactory();
@@ -21,7 +31,7 @@ public static ConnectionFactory getConnectionFactory() throws URISyntaxException
return factory;
}
-
+
private static String getEnvOrThrow(String name) {
final String env = getenv(name);
if (env == null) {
View
17 common/src/main/resources/rabbit-context.xml
@@ -0,0 +1,17 @@
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:rabbit="http://www.springframework.org/schema/rabbit"
+ xsi:schemaLocation="http://www.springframework.org/schema/rabbit
+ http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd
+ http://www.springframework.org/schema/beans
+ http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
+
+ <bean id="connectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
+ <constructor-arg value="#{ T(com.heroku.devcenter.RabbitUtils).getConnectionFactory()}"/>
+ </bean>
+
+ <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"/>
+
+ <rabbit:admin connection-factory="connectionFactory"/>
+
+</beans>
View
40 web/src/main/java/com/heroku/devcenter/RabbitController.java
@@ -1,27 +1,35 @@
package com.heroku.devcenter;
-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;
+import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
-import java.io.UnsupportedEncodingException;
+import java.util.Map;
@Controller
-@RequestMapping("/pdf")
+@RequestMapping("/rabbit")
public class RabbitController {
- 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);
- }
+ private final AmqpTemplate rabbit = RabbitUtils.getTemplate();
+
+ @ModelAttribute("bigOp")
+ public BigOperation newBigOp() {
+ return new BigOperation();
+ }
+
+ @RequestMapping()
+ public String display() {
+ return "rabbitForm";
+ }
+
+ @RequestMapping(method = RequestMethod.POST)
+ public String process(@ModelAttribute("bigOp") BigOperation bigOp, Map<String, Object> map) {
+
+ rabbit.convertAndSend(BigOperation.QUEUE_NAME, bigOp);
+ System.out.println("Sent message to RabbitMQ: " + bigOp.name);
+
+ return "rabbitConfirmation";
}
}
View
23 web/src/main/resources/applicationContext.xml
@@ -3,36 +3,15 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
- 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.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
- http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
+ http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:annotation-config />
<context:component-scan base-package="com.heroku.devcenter" />
<mvc:annotation-driven/>
- <bean id="cf" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
- <constructor-arg><value>#{ T(com.heroku.devcenter.RabbitFactory).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>
-
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
View
40 web/src/main/webapp/WEB-INF/jsp/editSObjectRecord.jsp
@@ -1,40 +0,0 @@
-<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
-<jsp:include page="header.jsp"/>
- <div class="row">
- <div class="span8 offset2">
- <div class="page-header">
- <h1>
- ${record.metadata.label}
- <c:if test="${record.get('name').value != null}">
- : ${record.get("name").value}
- </c:if>
- </h1>
- </div>
-
- <c:if test="${error != null}">
- <div class="alert">${error}</div>
- </c:if>
-
- <form method="POST" action="">
- <div class="btn-group">
- <input type="submit" value="Save" class="btn btn-primary">
- </div>
- <table class="table table-striped table-condensed">
- <c:forEach items="${record.fields}" var="field">
- <tr>
- <td><label for="${field.metadata.name}"><c:out value="${field.metadata.label}"/></label></td>
- <td>
- <input id="${field.metadata.name}"
- name="${field.metadata.name}"
- value="<c:out value="${field.value}"/>"/>
- </td>
- </tr>
- </c:forEach>
- </table>
- <div class="btn-group">
- <input type="submit" value="Save" class="btn btn-primary">
- </div>
- </form>
- </div>
- </div>
-<jsp:include page="footer.jsp"/>
View
16 web/src/main/webapp/WEB-INF/jsp/rabbitConfirmation.jsp
@@ -0,0 +1,16 @@
+<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
+<jsp:include page="header.jsp"/>
+<div class="row">
+ <div class="span8 offset2">
+ <div class="page-header">
+ <h1>Rabbit Form</h1>
+ </div>
+
+ <c:if test="${error != null}">
+ <div class="alert">${error}</div>
+ </c:if>
+
+ Thanks!
+ </div>
+</div>
+<jsp:include page="footer.jsp"/>
View
23 web/src/main/webapp/WEB-INF/jsp/rabbitForm.jsp
@@ -0,0 +1,23 @@
+<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
+<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
+<jsp:include page="header.jsp"/>
+<div class="row">
+ <div class="span8 offset2">
+ <div class="page-header">
+ <h1>Rabbit Form</h1>
+ </div>
+
+ <c:if test="${error != null}">
+ <div class="alert">${error}</div>
+ </c:if>
+
+ <form:form method="post" modelAttribute="bigOp">
+ <label>Name: <form:input path="name"/></label>
+
+ <div class="btn-group">
+ <input type="submit" class="btn btn-primary">
+ </div>
+ </form:form>
+ </div>
+</div>
+<jsp:include page="footer.jsp"/>
View
2 worker/pom.xml
@@ -29,7 +29,7 @@
<assembleDirectory>target</assembleDirectory>
<programs>
<program>
- <mainClass>com.heroku.devcenter.SpringReceiver</mainClass>
+ <mainClass>com.heroku.devcenter.RabbitReceiver</mainClass>
<name>receiver</name>
</program>
</programs>
View
22 worker/src/main/java/com/heroku/devcenter/RabbitReceiver.java
@@ -0,0 +1,22 @@
+package com.heroku.devcenter;
+
+import org.springframework.amqp.core.AmqpTemplate;
+
+public class RabbitReceiver {
+
+ public static void main(String[] args) {
+ final AmqpTemplate rabbit = RabbitUtils.getTemplate();
+
+ while (true) {
+ System.out.println("Checking for message...");
+
+ try {
+ BigOperation bigOp = (BigOperation) rabbit.receiveAndConvert(BigOperation.QUEUE_NAME);
+ System.out.println("Received Big Operation: " + bigOp);
+ } catch (Exception e) {
+ System.err.println("ERROR checking message");
+ e.printStackTrace(System.err);
+ }
+ }
+ }
+}
View
24 worker/src/main/java/com/heroku/devcenter/SpringReceiver.java
@@ -1,24 +0,0 @@
-package com.heroku.devcenter;
-
-import org.springframework.amqp.core.Message;
-import org.springframework.amqp.rabbit.core.RabbitTemplate;
-import org.springframework.context.support.ClassPathXmlApplicationContext;
-
-import java.io.UnsupportedEncodingException;
-
-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
24 worker/src/main/resources/applicationContext.xml
@@ -2,31 +2,9 @@
<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"
+ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-autowire="byName">
<context:property-placeholder/>
-
- <bean id="cf" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
- <constructor-arg><value>#{ T(com.heroku.devcenter.RabbitFactory).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>

0 comments on commit 0a01d14

Please sign in to comment.