Permalink
Browse files

committed message-groups example

  • Loading branch information...
whaley committed Sep 24, 2011
1 parent 91ec91b commit 49235467fc00946040904a0924aee7081286509f
View
@@ -0,0 +1 @@
+.idea
@@ -1,69 +0,0 @@
-<?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.
--->
-
-<!-- Configures the Camel Context-->
-
-<beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:amq="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.apache.org/schema/core/activemq-core.xsd">
-
- <!-- This creates an embedded ActiveMQ Broker -->
- <broker xmlns="http://activemq.apache.org/schema/core" useJmx="false" persistent="false">
- <transportConnectors>
- <transportConnector uri="tcp://localhost:61616" />
- </transportConnectors>
- </broker>
-
- <!-- Lets connect the Camel ActiveMQ component to the embedded broker.
- See http://camel.apache.org/activemq.html for more information.
- -->
- <bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
- <property name="brokerURL" value="failover:tcp://localhost:61616"/>
- </bean>
-
- <!-- Here is a sample which processes the input files
- (leaving them in place - see the 'noop' flag), and
- sends them on a JMS queue. This queue is watched by
- another route which then performs content based
- routing on the messages using XPath.
- -->
- <camelContext xmlns="http://camel.apache.org/schema/spring">
- <package>com.brinksys.camel</package>
- <route>
- <from uri="file:src/data?noop=true"/>
- <to uri="activemq:personnel.records"/>
- </route>
- <route>
- <from uri="activemq:personnel.records"/>
- <choice>
- <when>
- <xpath>/person/city = 'London'</xpath>
- <to uri="file:target/messages/uk"/>
- </when>
- <otherwise>
- <to uri="file:target/messages/others"/>
- </otherwise>
- </choice>
- </route>
- </camelContext>
-
-</beans>
@@ -1,22 +0,0 @@
-
-#
-# The logging properties used for eclipse testing, We want to see debug output on the console.
-#
-log4j.rootLogger=INFO, out
-
-# uncomment the following line to turn on Camel debugging
-#log4j.logger.org.apache.camel=DEBUG
-
-# uncomment the following line to turn on ActiveMQ debugging
-#log4j.logger.org.apache.activemq=DEBUG
-
-log4j.logger.org.springframework=WARN
-
-
-# CONSOLE appender not used by default
-log4j.appender.out=org.apache.log4j.ConsoleAppender
-log4j.appender.out.layout=org.apache.log4j.PatternLayout
-log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
-#log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
-
-log4j.throwableRenderer=org.apache.log4j.EnhancedThrowableRenderer
@@ -1,22 +0,0 @@
-<?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.
--->
-<person user="hiram">
- <firstName>Hiram</firstName>
- <lastName>Chirino</lastName>
- <city>Tampa</city>
-</person>
@@ -1,22 +0,0 @@
-<?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.
--->
-<person user="jon">
- <firstName>Jonathan</firstName>
- <lastName>Anstey</lastName>
- <city>St. John's</city>
-</person>
@@ -1,22 +0,0 @@
-<?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.
--->
-<person user="james">
- <firstName>James</firstName>
- <lastName>Strachan</lastName>
- <city>London</city>
-</person>
@@ -1,22 +0,0 @@
-<?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.
--->
-<person user="james">
- <firstName>James</firstName>
- <lastName>Strachan</lastName>
- <city>London</city>
-</person>
@@ -0,0 +1,2 @@
+*message-groups.iml
+activemq-data
View
@@ -0,0 +1,30 @@
+<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.brinksys.camel</groupId>
+ <artifactId>message-groups</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <packaging>jar</packaging>
+
+ <name>message-groups</name>
+ <url>http://maven.apache.org</url>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.activemq</groupId>
+ <artifactId>activemq-camel</artifactId>
+ <version>5.5.0</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-simple</artifactId>
+ <version>1.5.11</version>
+ </dependency>
+ </dependencies>
+</project>
@@ -0,0 +1,101 @@
+package com.brinksys.camel;
+
+import org.apache.activemq.ActiveMQConnectionFactory;
+import org.apache.activemq.broker.BrokerService;
+import org.apache.activemq.camel.component.ActiveMQComponent;
+import org.apache.activemq.pool.PooledConnectionFactory;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.DefaultCamelContext;
+
+import java.util.concurrent.TimeUnit;
+
+public class App {
+ private static BrokerService broker;
+
+ public static void main(String[] args) throws Exception {
+ try {
+ startBroker();
+
+
+ CamelContext ctx = createCamelContext();
+ ctx.start();
+ ctx.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ /* Our direct route will take a message, and set the message to group 1 if the body is an integer,
+ * otherwise set the group to 2.
+ *
+ * This demonstrates the following concepts:
+ * 1) Header Manipulation
+ * 2) Checking the payload type of the body and using it in a choice.
+ * 3) JMS Message groups
+ */
+
+ from("direct:begin")
+ .choice()
+ .when(body().isInstanceOf(Integer.class)).setHeader("JMSXGroupID",constant("1"))
+ .otherwise().setHeader("JMSXGroupID",constant("2"))
+ .end()
+ .to("amq:queue:Message.Group.Test");
+
+ from("amq:queue:Message.Group.Test").routeId("Route A").process(new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ System.out.println("Route A Recieved :: " + exchange.getIn().getBody());
+ }
+ });
+
+ from("amq:queue:Message.Group.Test").routeId("Route B").process(new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ System.out.println("Route B Recieved :: " + exchange.getIn().getBody());
+ }
+ });
+ }
+ });
+
+ sendMessages(ctx.createProducerTemplate());
+ Thread.sleep(TimeUnit.SECONDS.toMillis(10));
+ stopBroker();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ private static CamelContext createCamelContext() throws Exception {
+ CamelContext camelContext = new DefaultCamelContext();
+
+ ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("vm://localhost/");
+ PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory(activeMQConnectionFactory);
+ pooledConnectionFactory.setMaxConnections(8);
+ pooledConnectionFactory.setMaximumActive(500);
+
+ ActiveMQComponent activeMQComponent = ActiveMQComponent.activeMQComponent();
+ activeMQComponent.setUsePooledConnection(true);
+ activeMQComponent.setConnectionFactory(pooledConnectionFactory);
+ camelContext.addComponent("amq", activeMQComponent);
+
+ return camelContext;
+ }
+
+ private static void sendMessages(ProducerTemplate pt) throws Exception {
+ for (int i = 0; i < 10; i++) {
+ pt.sendBody("direct:begin", Integer.valueOf(i));
+ }
+ for (int i = 0; i < 10; i++) {
+ pt.sendBody("direct:begin", "next group");
+ }
+ }
+
+ private static void startBroker() throws Exception {
+ broker = new BrokerService();
+ broker.addConnector("vm://localhost");
+ broker.start();
+ }
+
+ private static void stopBroker() throws Exception {
+ broker.stop();
+ }
+}
@@ -1,12 +0,0 @@
-Camel Router Project
-====================
-
-To run this router either embed the jar inside Spring
-or to run the route from within maven try
-
- mvn camel:run
-
-For more help see the Apache Camel documentation
-
- http://camel.apache.org/
-
Oops, something went wrong.

0 comments on commit 4923546

Please sign in to comment.