Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

clean up, added tests to support the jms integration.

  • Loading branch information...
commit ac7518392cd8c5307b9780e703dac4e20006f0c5 1 parent 1a1e7eb
authored August 27, 2011
2  .gitignore
@@ -7,4 +7,4 @@
7 7
 *.iml
8 8
 target
9 9
 .dropbox
10  
-
  10
+activemq-data
12  obm/pom.xml
@@ -15,6 +15,13 @@
15 15
     <dependencyManagement>
16 16
         <dependencies>
17 17
             <dependency>
  18
+                <groupId>org.xerial.snappy</groupId>
  19
+                <artifactId>snappy-java</artifactId>
  20
+                <version>1.0.3.2</version>
  21
+                <type>jar</type>
  22
+                <scope>compile</scope>
  23
+            </dependency>
  24
+            <dependency>
18 25
                 <groupId>com.google.protobuf</groupId>
19 26
                 <artifactId>protobuf-java</artifactId>
20 27
                 <version>2.3.0</version>
@@ -129,7 +136,12 @@
129 136
         </dependencies>
130 137
     </dependencyManagement>
131 138
     <dependencies>
  139
+        <dependency>
  140
+            <groupId>org.xerial.snappy</groupId>
  141
+            <artifactId>snappy-java</artifactId>
  142
+
132 143
 
  144
+        </dependency>
133 145
         <dependency>
134 146
             <groupId>com.google.protobuf</groupId>
135 147
             <artifactId>protobuf-java</artifactId>
61  obm/src/main/java/org/springframework/jms/support/converter/obm/MarshallingMessageConverter.java
@@ -15,6 +15,8 @@
15 15
  */
16 16
 package org.springframework.jms.support.converter.obm;
17 17
 
  18
+import org.apache.commons.logging.Log;
  19
+import org.apache.commons.logging.LogFactory;
18 20
 import org.springframework.beans.factory.InitializingBean;
19 21
 import org.springframework.jms.support.converter.MessageConversionException;
20 22
 import org.springframework.jms.support.converter.MessageConverter;
@@ -32,15 +34,52 @@
32 34
 import java.io.IOException;
33 35
 
34 36
 /**
  37
+ * Because the {@link Marshaller} interface requires a class to which to transform the bytes,
  38
+ * you must specify a {@link #payloadClass payload class} when using this {@link MessageConverter}.
  39
+ * This is inconvenient, however, in most typical configurations a {@link org.springframework.jms.core.JmsTemplate}
  40
+ * will be tied to the workload of one {@link javax.jms.Destination}, and typically a {@link javax.jms.Destination}
  41
+ * will only transport one type of payload.
  42
+ *
35 43
  * @author Josh Long
36 44
  * @see org.springframework.jms.support.converter.MarshallingMessageConverter
37 45
  */
38 46
 public class MarshallingMessageConverter implements MessageConverter, InitializingBean {
39 47
 
  48
+    private Log log = LogFactory.getLog(getClass());
  49
+
40 50
     private Marshaller marshaller;
41 51
     private Unmarshaller unmarshaller;
42 52
     private Class<?> payloadClass;
43 53
 
  54
+    public MarshallingMessageConverter() {
  55
+    }
  56
+
  57
+    public MarshallingMessageConverter(Class<?> aClass, Marshaller marshaller1) {
  58
+        this();
  59
+        try {
  60
+            setMarshaller(marshaller1);
  61
+            setPayloadClass(aClass);
  62
+            if (marshaller1 instanceof Unmarshaller) {
  63
+                setUnmarshaller((Unmarshaller) marshaller1);
  64
+            }
  65
+            afterPropertiesSet();
  66
+        } catch (Exception e) {
  67
+            throw new RuntimeException(e);
  68
+        }
  69
+    }
  70
+
  71
+    public MarshallingMessageConverter(Class<?> cl, Marshaller marshaller, Unmarshaller unmarshaller) {
  72
+        this();
  73
+        try {
  74
+            setMarshaller(marshaller);
  75
+            setUnmarshaller(unmarshaller);
  76
+            setPayloadClass(cl);
  77
+            afterPropertiesSet();
  78
+        } catch (Exception e) {
  79
+            throw new RuntimeException(e);
  80
+        }
  81
+    }
  82
+
44 83
     public void setPayloadClass(Class<?> payloadClass) {
45 84
         this.payloadClass = payloadClass;
46 85
     }
@@ -56,7 +95,11 @@ public void setUnmarshaller(Unmarshaller unmarshaller) {
56 95
     @Override
57 96
     public javax.jms.Message toMessage(Object object, javax.jms.Session session) throws JMSException, MessageConversionException {
58 97
         try {
59  
-            return marshalToBytesMessage(object, session, this.marshaller);
  98
+            javax.jms.Message msg = marshalToBytesMessage(object, session, this.marshaller);
  99
+            if (log.isDebugEnabled()) {
  100
+                log.debug("converted " + object + " to a message.");
  101
+            }
  102
+            return msg;
60 103
         } catch (Exception ex) {
61 104
             throw new MessageConversionException("Could not marshal [" + object + "]", ex);
62 105
         }
@@ -88,20 +131,32 @@ protected Object unmarshalFromBytesMessage(Class clzz, BytesMessage message, org
88 131
             byte[] bytes = new byte[(int) message.getBodyLength()];
89 132
             message.readBytes(bytes);
90 133
             ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
91  
-
92  
-            return unmarshaller.unmarshal(clzz, bis);
  134
+            Object result = unmarshaller.unmarshal(clzz, bis);
  135
+            Assert.notNull(result, "the result from the queue is null");
  136
+            if (log.isDebugEnabled()) {
  137
+                log.debug("received: " + result );
  138
+            }
  139
+            return result;
93 140
         } catch (Exception e) {
94 141
             throw new RuntimeException(e);
95 142
         }
96 143
     }
97 144
 
98 145
     protected BytesMessage marshalToBytesMessage(Object object, Session session, org.springframework.obm.Marshaller marshaller) throws JMSException, IOException, XmlMappingException {
  146
+        Assert.notNull(object);
99 147
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
100 148
         BytesMessage message;
101 149
         try {
  150
+
102 151
             marshaller.marshal(object, bos);
  152
+
103 153
             message = session.createBytesMessage();
104 154
             message.writeBytes(bos.toByteArray());
  155
+
  156
+            if (log.isDebugEnabled()) {
  157
+                log.debug("sent:" + object);
  158
+            }
  159
+
105 160
         } catch (Exception e) {
106 161
             throw new RuntimeException(e);
107 162
         }
6  obm/src/test/java/org/springframework/http/converter/obm/AvroHttpMessageConverterTest.java
@@ -30,7 +30,7 @@
30 30
 import org.springframework.obm.avro.AvroMarshaller;
31 31
 import org.springframework.obm.avro.crm.Customer;
32 32
 import org.springframework.stereotype.Controller;
33  
-import org.springframework.util.http.IntegrationTestUtils;
  33
+import org.springframework.util.http.RestIntegrationTestUtils;
34 34
 import org.springframework.web.bind.annotation.PathVariable;
35 35
 import org.springframework.web.bind.annotation.RequestMapping;
36 36
 import org.springframework.web.bind.annotation.RequestMethod;
@@ -78,7 +78,7 @@ public void testAvroMarshaller() throws Throwable {
78 78
 
79 79
     @Test
80 80
     public void testSimpleIntegration() throws Throwable {
81  
-        IntegrationTestUtils.startServiceAndConnect(MyService.class, new IntegrationTestUtils.ServerExecutionCallback() {
  81
+        RestIntegrationTestUtils.startServiceAndConnect(MyService.class, new RestIntegrationTestUtils.ServerExecutionCallback() {
82 82
             @Override
83 83
             public void doWithServer(RestTemplate restTemplate, Server server) throws Throwable {
84 84
                 Assert.assertNotNull(restTemplate);
@@ -104,7 +104,7 @@ public void doWithServer(RestTemplate restTemplate, Server server) throws Throwa
104 104
 
105 105
     @Configuration
106 106
     @EnableWebMvc
107  
-    static public class MyService extends IntegrationTestUtils.AbstractRestServiceConfiguration {
  107
+    static public class MyService extends RestIntegrationTestUtils.AbstractRestServiceConfiguration {
108 108
         @Bean
109 109
         public CustomerController controller() {
110 110
             return new CustomerController();
6  obm/src/test/java/org/springframework/http/converter/obm/MessagePackHttpMessageConverterTest.java
@@ -31,7 +31,7 @@
31 31
 import org.springframework.obm.messagepack.Cat;
32 32
 import org.springframework.obm.messagepack.MessagePackMarshaller;
33 33
 import org.springframework.stereotype.Controller;
34  
-import org.springframework.util.http.IntegrationTestUtils;
  34
+import org.springframework.util.http.RestIntegrationTestUtils;
35 35
 import org.springframework.web.bind.annotation.PathVariable;
36 36
 import org.springframework.web.bind.annotation.RequestMapping;
37 37
 import org.springframework.web.bind.annotation.RequestMethod;
@@ -74,7 +74,7 @@ public void testHttpReading() throws Throwable {
74 74
 
75 75
     @Test
76 76
     public void testSimpleIntegration() throws Throwable {
77  
-        IntegrationTestUtils.startServiceAndConnect(MyService.class, new IntegrationTestUtils.ServerExecutionCallback() {
  77
+        RestIntegrationTestUtils.startServiceAndConnect(MyService.class, new RestIntegrationTestUtils.ServerExecutionCallback() {
78 78
             @Override
79 79
             public void doWithServer(RestTemplate clientRestTemplate, Server server) throws Throwable {
80 80
 
@@ -99,7 +99,7 @@ public void doWithServer(RestTemplate clientRestTemplate, Server server) throws
99 99
 
100 100
     @Configuration
101 101
     @EnableWebMvc
102  
-    static public class MyService extends IntegrationTestUtils.AbstractRestServiceConfiguration {
  102
+    static public class MyService extends RestIntegrationTestUtils.AbstractRestServiceConfiguration {
103 103
         @Bean
104 104
         public CatController controller() {
105 105
             return new CatController();
6  obm/src/test/java/org/springframework/http/converter/obm/ThriftHttpMessageConverterTest.java
@@ -32,7 +32,7 @@
32 32
 import org.springframework.obm.thrift.ThriftMarshaller;
33 33
 import org.springframework.obm.thrift.crm.Customer;
34 34
 import org.springframework.stereotype.Controller;
35  
-import org.springframework.util.http.IntegrationTestUtils;
  35
+import org.springframework.util.http.RestIntegrationTestUtils;
36 36
 import org.springframework.web.bind.annotation.PathVariable;
37 37
 import org.springframework.web.bind.annotation.RequestMapping;
38 38
 import org.springframework.web.bind.annotation.RequestMethod;
@@ -75,7 +75,7 @@ public void testHttpReading() throws Throwable {
75 75
 
76 76
     @Test
77 77
     public void testSimpleIntegration() throws Throwable {
78  
-        IntegrationTestUtils.startServiceAndConnect(MyService.class, new IntegrationTestUtils.ServerExecutionCallback() {
  78
+        RestIntegrationTestUtils.startServiceAndConnect(MyService.class, new RestIntegrationTestUtils.ServerExecutionCallback() {
79 79
             @Override
80 80
             public void doWithServer(RestTemplate clientRestTemplate, Server server) throws Throwable {
81 81
 
@@ -98,7 +98,7 @@ public void doWithServer(RestTemplate clientRestTemplate, Server server) throws
98 98
 
99 99
     @Configuration
100 100
     @EnableWebMvc
101  
-    static public class MyService extends IntegrationTestUtils.AbstractRestServiceConfiguration {
  101
+    static public class MyService extends RestIntegrationTestUtils.AbstractRestServiceConfiguration {
102 102
         @Bean
103 103
         public CrmRestController controller() {
104 104
             return new CrmRestController();
114  obm/src/test/java/org/springframework/jms/support/converter/obm/TestJmsMessageConverter.java
... ...
@@ -0,0 +1,114 @@
  1
+package org.springframework.jms.support.converter.obm;
  2
+
  3
+import org.apache.activemq.broker.BrokerService;
  4
+import org.junit.Assert;
  5
+import org.junit.Before;
  6
+import org.junit.Test;
  7
+import org.springframework.jms.core.JmsTemplate;
  8
+import org.springframework.obm.avro.AvroMarshaller;
  9
+import org.springframework.obm.avro.crm.Customer;
  10
+import org.springframework.obm.messagepack.Cat;
  11
+import org.springframework.obm.messagepack.MessagePackMarshaller;
  12
+import org.springframework.obm.protocolbuffers.ProtocolBuffersMarshaller;
  13
+import org.springframework.obm.protocolbuffers.crm.Crm;
  14
+import org.springframework.obm.thrift.ThriftMarshaller;
  15
+import org.springframework.util.jms.JmsIntegrationTestUtils;
  16
+
  17
+/**
  18
+ * Tests the {@link org.springframework.obm.avro.AvroMarshaller} with JMS (specifically, an embedded ActiveMQ broker).
  19
+ *
  20
+ * @author Josh Long
  21
+ */
  22
+public class TestJmsMessageConverter {
  23
+
  24
+    // thrift
  25
+    private ThriftMarshaller thriftMarshaller = new ThriftMarshaller();
  26
+    private org.springframework.obm.thrift.crm.Customer thriftCustomer = new org.springframework.obm.thrift.crm.Customer("John", "Doe", "email@email.com", 22);
  27
+
  28
+
  29
+    // messagepack
  30
+    private MessagePackMarshaller msgPackMarshaller = new MessagePackMarshaller();
  31
+    private Cat msgPackCat;
  32
+
  33
+    // protocol buffers
  34
+    private ProtocolBuffersMarshaller buffersMarshaller = new ProtocolBuffersMarshaller();
  35
+    private Crm.Customer buffersCustomer;
  36
+    // avro
  37
+    private AvroMarshaller avroMarshaller = new AvroMarshaller();
  38
+    private Customer avroCustomer = new Customer();
  39
+
  40
+    @Before
  41
+    public void before() throws Throwable {
  42
+        // protocol buffers
  43
+        buffersCustomer = Crm.Customer.newBuilder().setEmail("email@e.com").setFirstName("john").setLastName("long").build();
  44
+
  45
+        // avro
  46
+        avroCustomer.firstName = "josh";
  47
+        avroCustomer.lastName = "long";
  48
+        avroCustomer.email = "em@em.com";
  49
+        avroCustomer.id = (int) (1000 * Math.random());
  50
+
  51
+        // message pack
  52
+        msgPackCat = new Cat("Felix", (int) (1000 * Math.random()));
  53
+        this.msgPackMarshaller.afterPropertiesSet();
  54
+
  55
+        // thrift
  56
+        thriftMarshaller.afterPropertiesSet();
  57
+    }
  58
+
  59
+    @Test
  60
+    public void testThrift() throws Throwable {
  61
+        JmsIntegrationTestUtils.startAndConnectToJmsBroker(org.springframework.obm.thrift.crm.Customer.class, thriftMarshaller, new JmsIntegrationTestUtils.JmsBrokerExecutionCallback() {
  62
+            @Override
  63
+            public void doWithActiveMq(BrokerService brokerService, JmsTemplate jmsTemplate) throws Throwable {
  64
+                String avroDestination = "thrift";
  65
+                jmsTemplate.convertAndSend(avroDestination, thriftCustomer);
  66
+
  67
+                org.springframework.obm.thrift.crm.Customer customerReceived =
  68
+                     (org.springframework.obm.thrift.crm.Customer) jmsTemplate.receiveAndConvert(avroDestination);
  69
+                Assert.assertEquals(thriftCustomer, customerReceived);
  70
+            }
  71
+        });
  72
+    }
  73
+
  74
+    @Test
  75
+    public void testAvro() throws Throwable {
  76
+        JmsIntegrationTestUtils.startAndConnectToJmsBroker(Customer.class, avroMarshaller, new JmsIntegrationTestUtils.JmsBrokerExecutionCallback() {
  77
+            @Override
  78
+            public void doWithActiveMq(BrokerService brokerService, JmsTemplate jmsTemplate) throws Throwable {
  79
+                String avroDestination = "avro";
  80
+                jmsTemplate.convertAndSend(avroDestination, avroCustomer);
  81
+
  82
+                Customer customerReceived = (Customer) jmsTemplate.receiveAndConvert(avroDestination);
  83
+                Assert.assertEquals(avroCustomer, customerReceived);
  84
+            }
  85
+        });
  86
+    }
  87
+
  88
+    @Test
  89
+    public void testMessagePack() throws Throwable {
  90
+        JmsIntegrationTestUtils.startAndConnectToJmsBroker(Cat.class, this.msgPackMarshaller, new JmsIntegrationTestUtils.JmsBrokerExecutionCallback() {
  91
+            @Override
  92
+            public void doWithActiveMq(BrokerService brokerService, JmsTemplate jmsTemplate) throws Throwable {
  93
+                String pbDestination = "messagepack";
  94
+                jmsTemplate.convertAndSend(pbDestination, msgPackCat);
  95
+                Cat rCat = (Cat) jmsTemplate.receiveAndConvert(pbDestination);
  96
+                Assert.assertEquals(rCat, msgPackCat);
  97
+            }
  98
+        });
  99
+    }
  100
+
  101
+    @Test
  102
+    public void testProtocolBuffers() throws Throwable {
  103
+        JmsIntegrationTestUtils.startAndConnectToJmsBroker(Crm.Customer.class, this.buffersMarshaller, new JmsIntegrationTestUtils.JmsBrokerExecutionCallback() {
  104
+            @Override
  105
+            public void doWithActiveMq(BrokerService brokerService, JmsTemplate jmsTemplate) throws Throwable {
  106
+                String pbDestination = "pb";
  107
+                jmsTemplate.convertAndSend(pbDestination, buffersCustomer);
  108
+                Crm.Customer receivedCustomer = (Crm.Customer) jmsTemplate.receiveAndConvert(pbDestination);
  109
+                Assert.assertEquals(receivedCustomer, buffersCustomer);
  110
+            }
  111
+        });
  112
+    }
  113
+
  114
+}
8  obm/src/test/java/org/springframework/obm/messagepack/Cat.java
@@ -50,6 +50,14 @@ public boolean equals(Object o) {
50 50
     }
51 51
 
52 52
     @Override
  53
+    public String toString() {
  54
+        return "Cat{" +
  55
+                       "name='" + name + '\'' +
  56
+                       ", id=" + id +
  57
+                       '}';
  58
+    }
  59
+
  60
+    @Override
53 61
     public int hashCode() {
54 62
         int result = name != null ? name.hashCode() : 0;
55 63
         result = 31 * result + id;
4  obm/src/test/java/org/springframework/remoting/thrift/TestThriftServiceExporter.java
@@ -19,7 +19,7 @@
19 19
 import org.springframework.obm.thrift.crm.Customer;
20 20
 import org.springframework.util.http.DispatcherServletJettyConfigurationCallback;
21 21
 import org.springframework.util.http.EndpointTestUtils;
22  
-import org.springframework.util.http.IntegrationTestUtils;
  22
+import org.springframework.util.http.RestIntegrationTestUtils;
23 23
 import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
24 24
 
25 25
 import java.util.Arrays;
@@ -52,7 +52,7 @@ public void before() throws Throwable {
52 52
     @After
53 53
     public void after() throws Throwable {
54 54
 
55  
-        IntegrationTestUtils.stopServerQuietly(this.jettyServer);
  55
+        RestIntegrationTestUtils.stopServerQuietly(this.jettyServer);
56 56
         if (log.isDebugEnabled()) {
57 57
             log.debug("stopped jetty server");
58 58
         }
4  ...ringframework/util/http/IntegrationTestUtils.java → ...framework/util/http/RestIntegrationTestUtils.java
@@ -31,14 +31,14 @@
31 31
  *
32 32
  * @author Josh Long
33 33
  */
34  
-public class IntegrationTestUtils {
  34
+public class RestIntegrationTestUtils {
35 35
     public static interface
36 36
             ServerExecutionCallback {
37 37
         void doWithServer(RestTemplate restTemplate, Server server) throws Throwable;
38 38
     }
39 39
 
40 40
 
41  
-    static Log log = LogFactory.getLog(IntegrationTestUtils.class);
  41
+    static Log log = LogFactory.getLog(RestIntegrationTestUtils.class);
42 42
 
43 43
     static private Map<AbstractRestServiceConfiguration, BeanFactory> beanFactoryMap = new ConcurrentHashMap<AbstractRestServiceConfiguration, BeanFactory>();
44 44
 
60  obm/src/test/java/org/springframework/util/jms/JmsIntegrationTestUtils.java
... ...
@@ -0,0 +1,60 @@
  1
+package org.springframework.util.jms;
  2
+
  3
+import org.apache.activemq.ActiveMQConnectionFactory;
  4
+import org.apache.activemq.broker.BrokerService;
  5
+import org.apache.activemq.broker.TransportConnector;
  6
+import org.apache.commons.logging.Log;
  7
+import org.apache.commons.logging.LogFactory;
  8
+import org.junit.Assert;
  9
+import org.springframework.jms.core.JmsTemplate;
  10
+import org.springframework.jms.support.converter.obm.MarshallingMessageConverter;
  11
+import org.springframework.obm.Marshaller;
  12
+
  13
+/**
  14
+ * Supports integration tests with an embedded ActiveMQ instance.
  15
+ *
  16
+ * @author Josh Long
  17
+ */
  18
+abstract public class JmsIntegrationTestUtils {
  19
+
  20
+    static private Log log = LogFactory.getLog(JmsIntegrationTestUtils.class);
  21
+
  22
+    /**
  23
+     * during the execution of this callback, a JMS broker will be available and the {@link JmsTemplate} required to
  24
+     * connect to that JMS broker will be provided as a parameter to this
  25
+     * class's {@link JmsBrokerExecutionCallback#doWithActiveMq(org.apache.activemq.broker.BrokerService, org.springframework.jms.core.JmsTemplate)}
  26
+     */
  27
+    public static interface JmsBrokerExecutionCallback {
  28
+        void doWithActiveMq(BrokerService brokerService, JmsTemplate jmsTemplate) throws Throwable;
  29
+    }
  30
+
  31
+    public static void startAndConnectToJmsBroker(Class<?> clzzForPayload, Marshaller m, JmsBrokerExecutionCallback callback) throws Throwable {
  32
+        Assert.assertNotNull("the " + JmsBrokerExecutionCallback.class.getName() + "can not be null", callback);
  33
+        String destinationUrl = "tcp://localhost:61617";
  34
+        BrokerService broker = new BrokerService();
  35
+        TransportConnector connector = broker.addConnector(destinationUrl);
  36
+        broker.start();
  37
+        while (!broker.isStarted()) {
  38
+            Thread.sleep(500);
  39
+        }
  40
+        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(destinationUrl);
  41
+        MarshallingMessageConverter converter = new MarshallingMessageConverter(clzzForPayload, m);
  42
+        JmsTemplate jmsTemplate = new JmsTemplate(activeMQConnectionFactory);
  43
+        converter.setPayloadClass(clzzForPayload);
  44
+        jmsTemplate.setMessageConverter(converter);
  45
+        jmsTemplate.afterPropertiesSet();
  46
+
  47
+        try {
  48
+            callback.doWithActiveMq(broker, jmsTemplate);
  49
+        } catch (AssertionError ae) {
  50
+            throw ae;
  51
+        } catch (Throwable th) {
  52
+            if (log.isErrorEnabled()) {
  53
+                log.error("execution of jms session failed.", th);
  54
+            }
  55
+        } finally {
  56
+            connector.stop();
  57
+            broker.stop();
  58
+        }
  59
+    }
  60
+}

0 notes on commit ac75183

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