Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Barely working message passing from web to worker

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

0 comments on commit 0a01d14

Please sign in to comment.
Something went wrong with that request. Please try again.