activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chir...@apache.org
Subject svn commit: r530433 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/component/direct/ main/resources/META-INF/services/org/apache/camel/component/ test/java/org/apache/camel/component/direct/
Date Thu, 19 Apr 2007 14:44:02 GMT
Author: chirino
Date: Thu Apr 19 07:44:00 2007
New Revision: 530433

URL: http://svn.apache.org/viewvc?view=rev&rev=530433
Log:
Added a direct endpoint component

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/direct/
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/direct/DirectComponent.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/direct/DirectEndpoint.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/direct/package.html
    activemq/camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/direct
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/direct/
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/direct/DirectRouteTest.java

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/direct/DirectComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/direct/DirectComponent.java?view=auto&rev=530433
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/direct/DirectComponent.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/direct/DirectComponent.java
Thu Apr 19 07:44:00 2007
@@ -0,0 +1,53 @@
+/*
+ * 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.component.direct;
+
+import java.util.concurrent.ScheduledExecutorService;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Component;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+
+/**
+ * Represents the component that manages {@link DirectEndpoint}.  It holds the 
+ * list of named direct endpoints.
+ *
+ * @org.apache.xbean.XBean
+ * @version $Revision: 519973 $
+ */
+public class DirectComponent<E extends Exchange> implements Component<E> {
+
+	private CamelContext context;
+
+	public CamelContext getCamelContext() {
+		return context;
+	}
+
+	public ScheduledExecutorService getExecutorService() {
+		return null;
+	}
+
+	public Endpoint<E> resolveEndpoint(String uri) throws Exception {
+		return new DirectEndpoint<E>(uri,this);
+	}
+
+	public void setCamelContext(CamelContext context) {
+		this.context = context;
+	}	
+
+}

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/direct/DirectEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/direct/DirectEndpoint.java?view=auto&rev=530433
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/direct/DirectEndpoint.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/direct/DirectEndpoint.java
Thu Apr 19 07:44:00 2007
@@ -0,0 +1,96 @@
+/*
+ * 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.component.direct;
+
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.impl.DefaultConsumer;
+import org.apache.camel.impl.DefaultEndpoint;
+import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.impl.DefaultProducer;
+
+/**
+ * Represents a direct endpoint that synchronously invokes the consumers of the endpoint
when a producer 
+ * sends a message to it.
+ *
+ * @org.apache.xbean.XBean
+ * @version $Revision: 519973 $
+ */
+public class DirectEndpoint<E extends Exchange> extends DefaultEndpoint<E> {
+
+	private final CopyOnWriteArrayList<DefaultConsumer<E>> consumers = new CopyOnWriteArrayList<DefaultConsumer<E>>();
+	
+	boolean allowMutlipleConsumers=true;	
+	
+    public DirectEndpoint(String uri, DirectComponent<E> component) {
+        super(uri, component);
+    }
+
+    public Producer<E> createProducer() throws Exception {
+        return startService(new DefaultProducer<E>(this) {
+            public void process(E exchange) {
+            	DirectEndpoint.this.process(exchange);
+            }
+        });    	
+    }
+
+    protected void process(E exchange) {
+    	for (DefaultConsumer<E> consumer : consumers) {
+			consumer.getProcessor().process(exchange);
+		}
+	}
+
+	public Consumer<E> createConsumer(Processor<E> processor) throws Exception {
+		DefaultConsumer<E> consumer = new DefaultConsumer<E>(this, processor) {
+			@Override
+			public void start() throws Exception {
+				if( !allowMutlipleConsumers && !consumers.isEmpty() )
+					throw new IllegalStateException("Endpoint "+getEndpointUri()+" only allows 1 active
consumer but you attempted to start a 2nd consumer.");
+				
+				consumers.add(this);
+				super.start();
+			}
+			
+			@Override
+			public void stop() throws Exception {
+				super.stop();
+				consumers.remove(this);
+			}
+		};
+		
+		return consumer;
+    }
+
+    public E createExchange() {
+    	// How can we create a specific Exchange if we are generic??
+    	// perhaps it would be better if we did not implement this. 
+        return (E) new DefaultExchange(getContext());
+    }
+
+	public boolean isAllowMutlipleConsumers() {
+		return allowMutlipleConsumers;
+	}
+	public void setAllowMutlipleConsumers(boolean allowMutlipleConsumers) {
+		this.allowMutlipleConsumers = allowMutlipleConsumers;
+	}
+
+
+}

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/direct/package.html
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/direct/package.html?view=auto&rev=530433
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/direct/package.html
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/direct/package.html
Thu Apr 19 07:44:00 2007
@@ -0,0 +1,26 @@
+<!--
+    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.
+-->
+<html>
+<head>
+</head>
+<body>
+
+A direct endpoint synchronously invokes all the consumers when a producer sends an exchange
to the endpoint.  This also known
+as strait through processing. 
+
+</body>
+</html>

Added: activemq/camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/direct
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/direct?view=auto&rev=530433
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/direct
(added)
+++ activemq/camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/component/direct
Thu Apr 19 07:44:00 2007
@@ -0,0 +1 @@
+class=org.apache.camel.component.direct.DirectComponent

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/direct/DirectRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/direct/DirectRouteTest.java?view=auto&rev=530433
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/direct/DirectRouteTest.java
(added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/direct/DirectRouteTest.java
Thu Apr 19 07:44:00 2007
@@ -0,0 +1,71 @@
+/**
+ *
+ * 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.component.direct;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import junit.framework.TestCase;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.DefaultCamelContext;
+
+/**
+ * @version $Revision: 520220 $
+ */
+public class DirectRouteTest extends TestCase {
+
+	
+    public void testSedaQueue() throws Exception {
+        CamelContext container = new DefaultCamelContext();
+
+        final AtomicBoolean invoked = new AtomicBoolean();
+        
+        // lets add some routes
+        container.addRoutes(new RouteBuilder() {
+            public void configure() {
+                from("direct:test.a").to("direct:test.b");
+                from("direct:test.b").process(new Processor<Exchange>() {
+                    public void process(Exchange e) {
+                    	invoked.set(true);
+                    }
+                });
+            }
+        });
+
+        
+        container.start();
+        
+        // now lets fire in a message
+        Endpoint<Exchange> endpoint = container.resolveEndpoint("direct:test.a");
+        Exchange exchange = endpoint.createExchange();
+        exchange.getIn().setHeader("cheese", 123);
+
+        Producer<Exchange> producer = endpoint.createProducer();
+        producer.process(exchange);
+
+        // now lets sleep for a while
+        assertTrue("Did not receive the message!", invoked.get());
+
+        container.stop();
+    }
+}



Mime
View raw message