You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2009/03/21 14:56:01 UTC
svn commit: r756939 - in /camel/trunk/tests/camel-itest/src/test:
java/org/apache/camel/itest/tx/ resources/org/apache/camel/itest/tx/
Author: davsclaus
Date: Sat Mar 21 13:56:00 2009
New Revision: 756939
URL: http://svn.apache.org/viewvc?rev=756939&view=rev
Log:
Added jms-http with TX example using Spring Testing.
Added:
camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/tx/
camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/tx/JmsToHttpRoute.java (with props)
camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/tx/JmsToHttpTXTest.java (with props)
camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/tx/
camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/tx/JmsToHttpTXTest-context.xml (with props)
Added: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/tx/JmsToHttpRoute.java
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/tx/JmsToHttpRoute.java?rev=756939&view=auto
==============================================================================
--- camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/tx/JmsToHttpRoute.java (added)
+++ camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/tx/JmsToHttpRoute.java Sat Mar 21 13:56:00 2009
@@ -0,0 +1,89 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.itest.tx;
+
+import javax.annotation.Resource;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.spring.SpringRouteBuilder;
+import org.apache.camel.spring.spi.SpringTransactionPolicy;
+
+/**
+ * Route that listen on a JMS queue and send a request/reply over http
+ * before returning a response. Is transacted.
+ * <p/>
+ * Notice we use the SpringRouteBuilder that supports transacted
+ * error handler.
+ *
+ * @version $Revision$
+ */
+public class JmsToHttpRoute extends SpringRouteBuilder {
+
+ @Resource(name = "PROPAGATION_REQUIRED")
+ private SpringTransactionPolicy required;
+
+ @EndpointInject(name = "data")
+ private Endpoint data;
+
+ private static int counter;
+
+ private String nok = "<?xml version=\"1.0\"?><reply><status>nok</status></reply>";
+ private String ok = "<?xml version=\"1.0\"?><reply><status>ok</status></reply>";
+
+ public void configure() throws Exception {
+ // configure a global transacted error handler
+ errorHandler(transactionErrorHandler(required));
+
+ from(data)
+ // send a request to http and get the response
+ .to("http://localhost:8080/sender")
+ // convert the response to String so we can work with it
+ .convertBodyTo(String.class)
+ // do a choice if the response is okay or not
+ .choice()
+ // do an xpath to compare if the statis is NOT okay
+ .when().xpath("/reply/status != 'ok'")
+ // as this is based on an unit test we use mocks to verify how many times we did rollback
+ .to("mock:rollback")
+ // response is not okay so force a rollback by throwing an exception
+ .process(new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ throw new IllegalArgumentException("Rollback please");
+ }
+ })
+ .otherwise()
+ // otherwise since its okay, the route ends and the response is sent back
+ // to the original caller
+ .end();
+
+ // this is our http route that will fail the first 2 attempts
+ // before it sends a ok response
+ from("jetty:http://localhost:8080/sender").process(new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ if (counter++ < 2) {
+ exchange.getOut().setBody(nok);
+ } else {
+ exchange.getOut().setBody(ok);
+ }
+ }
+ });
+ }
+
+}
Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/tx/JmsToHttpRoute.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/tx/JmsToHttpRoute.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/tx/JmsToHttpTXTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/tx/JmsToHttpTXTest.java?rev=756939&view=auto
==============================================================================
--- camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/tx/JmsToHttpTXTest.java (added)
+++ camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/tx/JmsToHttpTXTest.java Sat Mar 21 13:56:00 2009
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.itest.tx;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests;
+
+/**
+ * Unit test will look for the spring .xml file with the same class name
+ * but postfixed with -config.xml as filename.
+ * <p/>
+ * We use Spring Testing for unit test, eg we extend AbstractJUnit38SpringContextTests
+ * that is a Spring class.
+ *
+ * @version $Revision$
+ */
+@ContextConfiguration
+public class JmsToHttpTXTest extends AbstractJUnit38SpringContextTests {
+
+ // use uri to refer to our mock
+ @EndpointInject(uri = "mock:rollback")
+ MockEndpoint mock;
+
+ // use the spring id to refer to the endpoint we should send data to
+ // notice using this id we can setup the actual endpoint in spring XML
+ // and we can even use spring ${ } propertiy in the spring XML
+ @EndpointInject(name = "data")
+ private ProducerTemplate template;
+
+ // the ok response to expect
+ private String ok = "<?xml version=\"1.0\"?><reply><status>ok</status></reply>";
+
+ public void testSendToTXJms() throws Exception {
+ // we assume 2 rollbacks
+ mock.expectedMessageCount(2);
+
+ // use requestBody to force a InOut message exchange pattern ( = request/reply)
+ // will send and wait for a response
+ Object out = template.requestBody("<?xml version=\"1.0\"?><request><status id=\"123\"/></request>");
+
+ // compare response
+ assertEquals(ok, out);
+
+ // assert the mock is correct
+ mock.assertIsSatisfied();
+ }
+
+}
Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/tx/JmsToHttpTXTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/tx/JmsToHttpTXTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/tx/JmsToHttpTXTest-context.xml
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/tx/JmsToHttpTXTest-context.xml?rev=756939&view=auto
==============================================================================
--- camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/tx/JmsToHttpTXTest-context.xml (added)
+++ camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/tx/JmsToHttpTXTest-context.xml Sat Mar 21 13:56:00 2009
@@ -0,0 +1,71 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:broker="http://activemq.apache.org/schema/core"
+ xsi:schemaLocation="
+ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+ http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+ http://activemq.apache.org/schema/core http://activemq.org/config/1.0/1.0.xsd">
+
+ <!-- ActiveMQ broker -->
+ <broker:broker useJmx="false" persistent="false" brokerName="localhost">
+ <broker:transportConnectors>
+ <broker:transportConnector name="tcp" uri="tcp://localhost:61616"/>
+ </broker:transportConnectors>
+ </broker:broker>
+
+ <!-- ActiveMQ connection factory -->
+ <bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
+ <property name="brokerURL" value="tcp://localhost:61616"/>
+ </bean>
+
+ <!-- Spring JMX TX manager -->
+ <bean id="jmsTransactionManager" class="org.springframework.jms.connection.JmsTransactionManager">
+ <property name="connectionFactory" ref="jmsFactory"/>
+ </bean>
+
+ <!-- Policy for required -->
+ <bean id="PROPAGATION_REQUIRED" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
+ <property name="transactionManager" ref="jmsTransactionManager"/>
+ </bean>
+
+ <!-- Jms configuration -->
+ <bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration">
+ <property name="connectionFactory" ref="jmsFactory"/>
+ <property name="transactionManager" ref="jmsTransactionManager"/>
+ <property name="transacted" value="true"/>
+ <property name="concurrentConsumers" value="1"/>
+ </bean>
+
+ <!-- ActiveMQ component to be used in Camel -->
+ <bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
+ <property name="connectionFactory" ref="jmsFactory"/>
+ <property name="configuration" ref="jmsConfig"/>
+ </bean>
+
+ <!-- Camel context -->
+ <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
+ <!-- find our router in this package -->
+ <package>org.apache.camel.itest.tx</package>
+
+ <!-- define our data endpoint as the activemq queue we send a message to -->
+ <endpoint id="data" uri="activemq:queue:data"/>
+ </camelContext>
+
+</beans>
Propchange: camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/tx/JmsToHttpTXTest-context.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/tx/JmsToHttpTXTest-context.xml
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange: camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/tx/JmsToHttpTXTest-context.xml
------------------------------------------------------------------------------
svn:mime-type = text/xml