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/29 11:36:00 UTC
svn commit: r759657 - in /camel/trunk:
camel-core/src/main/java/org/apache/camel/
camel-core/src/main/java/org/apache/camel/impl/
camel-core/src/test/java/org/apache/camel/
camel-core/src/test/java/org/apache/camel/component/bean/
camel-core/src/test/j...
Author: davsclaus
Date: Sun Mar 29 09:35:59 2009
New Revision: 759657
URL: http://svn.apache.org/viewvc?rev=759657&view=rev
Log:
CAMEL-1281: Added ConsumerTemplate. CAMEL-1498: Improved error reporting for endpoint lookup with scheme prefix and no component exists with that scheme. Removed unused code.
Added:
camel/trunk/camel-core/src/main/java/org/apache/camel/ConsumerTemplate.java (with props)
camel/trunk/camel-core/src/main/java/org/apache/camel/FailedToCreateConsumerException.java (with props)
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java (with props)
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultConsumerTemplate.java (with props)
camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanConsumerTemplateTest.java (with props)
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeTemplateTest.java
- copied, changed from r759434, camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerBatchTest.java
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultConsumerTemplateTest.java (with props)
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelConsumerTemplateFactoryBean.java
- copied, changed from r759434, camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelTemplateFactoryBean.java
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelEndpointFactoryBean.java (contents, props changed)
- copied, changed from r759434, camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/EndpointFactoryBean.java
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelProducerTemplateFactoryBean.java (contents, props changed)
- copied, changed from r759434, camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelTemplateFactoryBean.java
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/SpringConsumerTemplateTest.java (with props)
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/SpringConsumerTemplateTest-context.xml (contents, props changed)
- copied, changed from r759434, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/RegistryInjection.xml
Removed:
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelTemplateFactoryBean.java
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/EndpointFactoryBean.java
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/factory/
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
camel/trunk/camel-core/src/main/java/org/apache/camel/ResolveEndpointFailedException.java
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
camel/trunk/camel-core/src/test/java/org/apache/camel/ContextTestSupport.java
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextTest.java
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/RouteWithMistypedComponentNameTest.java
camel/trunk/camel-core/src/test/java/org/apache/camel/management/CamelChoiceWithManagementTest.java
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
camel/trunk/components/camel-spring/src/main/resources/org/apache/camel/spring/jaxb.index
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java?rev=759657&r1=759656&r2=759657&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java Sun Mar 29 09:35:59 2009
@@ -305,6 +305,16 @@
ProducerTemplate createProducerTemplate();
/**
+ * Creates a new ConsumerTemplate.
+ * <p/>
+ * See this FAQ before use: <a href="http://camel.apache.org/why-does-camel-use-too-many-threads-with-producertemplate.html">
+ * Why does Camel use too many threads with ProducerTemplate?</a> as it also applies for ConsumerTemplate.
+ *
+ * @return the template
+ */
+ ConsumerTemplate createConsumerTemplate();
+
+ /**
* Adds the given interceptor strategy
*
* @param interceptStrategy the strategy
Added: camel/trunk/camel-core/src/main/java/org/apache/camel/ConsumerTemplate.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/ConsumerTemplate.java?rev=759657&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/ConsumerTemplate.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/ConsumerTemplate.java Sun Mar 29 09:35:59 2009
@@ -0,0 +1,211 @@
+/**
+ * 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;
+
+/**
+ * Template (named like Spring's TransactionTemplate & JmsTemplate
+ * et al) for working with Camel and consuming {@link Message} instances in an
+ * {@link Exchange} from an {@link Endpoint}.
+ * <p/>
+ * This template is an implementation of the
+ * <a href="http://camel.apache.org/polling-consumer.html">Polling Consumer EIP</a>.
+ * This is <b>not</b> the <a href="http://camel.apache.org/event-driven-consumer.html">Event Driven Consumer EIP</a>.
+ * <p/>
+ * <b>All</b> methods throws {@link RuntimeCamelException} if consuming of
+ * the {@link Exchange} failed and an Exception occured. The <tt>getCause</tt>
+ * method on {@link RuntimeCamelException} returns the wrapper original caused
+ * exception.
+ * <p/>
+ * All the receive<b>Body</b> methods will return the content according to this strategy
+ * <ul>
+ * <li>throws {@link RuntimeCamelException} as stated above</li>
+ * <li>The <tt>fault.body</tt> if there is a fault message set and its not <tt>null</tt></li>
+ * <li>The <tt>out.body<tt> if there is a out message set and its not <tt>null<tt></li>
+ * <li>The <tt>in.body<tt></li>
+ * </ul>
+ * <p/>
+ * <b>Important note on usage:</b> See this
+ * <a href="http://camel.apache.org/why-does-camel-use-too-many-threads-with-producertemplate.html">FAQ entry</a>
+ * before using, it applies to ConsumerTemplate as well.
+ *
+ * @version $Revision$
+ */
+public interface ConsumerTemplate extends Service {
+
+ /**
+ * Receives from the endpoint, waiting until there is a response
+ *
+ * @param endpointUri the endpoint to receive from
+ * @return the returned exchange
+ */
+ Exchange receive(String endpointUri);
+
+ /**
+ * Receives from the endpoint, waiting until there is a response
+ *
+ * @param endpoint the endpoint to receive from
+ * @return the returned exchange
+ */
+ Exchange receive(Endpoint endpoint);
+
+ /**
+ * Receives from the endpoint, waiting until there is a response
+ * or the timeout occurs
+ *
+ * @param endpointUri the endpoint to receive from
+ * @param timeout timeout in millis to wait for a response
+ * @return the returned exchange, or <tt>null</tt> if no response
+ */
+ Exchange receive(String endpointUri, long timeout);
+
+ /**
+ * Receives from the endpoint, waiting until there is a response
+ * or the timeout occurs
+ *
+ * @param endpoint the endpoint to receive from
+ * @param timeout timeout in millis to wait for a response
+ * @return the returned exchange, or <tt>null</tt> if no response
+ */
+ Exchange receive(Endpoint endpoint, long timeout);
+
+ /**
+ * Receives from the endpoint, not waiting for a response if non exists.
+ *
+ * @param endpointUri the endpoint to receive from
+ * @return the returned exchange, or <tt>null</tt> if no response
+ */
+ Exchange receiveNoWait(String endpointUri);
+
+ /**
+ * Receives from the endpoint, not waiting for a response if non exists.
+ *
+ * @param endpoint the endpoint to receive from
+ * @return the returned exchange, or <tt>null</tt> if no response
+ */
+ Exchange receiveNoWait(Endpoint endpoint);
+
+ /**
+ * Receives from the endpoint, waiting until there is a response
+ *
+ * @param endpointUri the endpoint to receive from
+ * @return the returned response body
+ */
+ Object receiveBody(String endpointUri);
+
+ /**
+ * Receives from the endpoint, waiting until there is a response
+ *
+ * @param endpoint the endpoint to receive from
+ * @return the returned response body
+ */
+ Object receiveBody(Endpoint endpoint);
+
+ /**
+ * Receives from the endpoint, waiting until there is a response
+ * or the timeout occurs
+ *
+ * @param endpointUri the endpoint to receive from
+ * @param timeout timeout in millis to wait for a response
+ * @return the returned response body, or <tt>null</tt> if no response
+ */
+ Object receiveBody(String endpointUri, long timeout);
+
+ /**
+ * Receives from the endpoint, waiting until there is a response
+ * or the timeout occurs
+ *
+ * @param endpoint the endpoint to receive from
+ * @param timeout timeout in millis to wait for a response
+ * @return the returned response body, or <tt>null</tt> if no response
+ */
+ Object receiveBody(Endpoint endpoint, long timeout);
+
+ /**
+ * Receives from the endpoint, not waiting for a response if non exists.
+ *
+ * @param endpointUri the endpoint to receive from
+ * @return the returned response body, or <tt>null</tt> if no response
+ */
+ Object receiveBodyNoWait(String endpointUri);
+
+ /**
+ * Receives from the endpoint, not waiting for a response if non exists.
+ *
+ * @param endpoint the endpoint to receive from
+ * @return the returned response body, or <tt>null</tt> if no response
+ */
+ Object receiveBodyNoWait(Endpoint endpoint);
+
+ /**
+ * Receives from the endpoint, waiting until there is a response
+ *
+ * @param endpointUri the endpoint to receive from
+ * @param type the expected response type
+ * @return the returned response body
+ */
+ <T> T receiveBody(String endpointUri, Class<T> type);
+
+ /**
+ * Receives from the endpoint, waiting until there is a response
+ *
+ * @param endpoint the endpoint to receive from
+ * @param type the expected response type
+ * @return the returned response body
+ */
+ <T> T receiveBody(Endpoint endpoint, Class<T> type);
+
+ /**
+ * Receives from the endpoint, waiting until there is a response
+ * or the timeout occurs
+ *
+ * @param endpointUri the endpoint to receive from
+ * @param timeout timeout in millis to wait for a response
+ * @param type the expected response type
+ * @return the returned response body, or <tt>null</tt> if no response
+ */
+ <T> T receiveBody(String endpointUri, long timeout, Class<T> type);
+
+ /**
+ * Receives from the endpoint, waiting until there is a response
+ * or the timeout occurs
+ *
+ * @param endpoint the endpoint to receive from
+ * @param timeout timeout in millis to wait for a response
+ * @param type the expected response type
+ * @return the returned response body, or <tt>null</tt> if no response
+ */
+ <T> T receiveBody(Endpoint endpoint, long timeout, Class<T> type);
+
+ /**
+ * Receives from the endpoint, not waiting for a response if non exists.
+ *
+ * @param endpointUri the endpoint to receive from
+ * @param type the expected response type
+ * @return the returned response body, or <tt>null</tt> if no response
+ */
+ <T> T receiveBodyNoWait(String endpointUri, Class<T> type);
+
+ /**
+ * Receives from the endpoint, not waiting for a response if non exists.
+ *
+ * @param endpoint the endpoint to receive from
+ * @param type the expected response type
+ * @return the returned response body, or <tt>null</tt> if no response
+ */
+ <T> T receiveBodyNoWait(Endpoint endpoint, Class<T> type);
+
+}
Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/ConsumerTemplate.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/ConsumerTemplate.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: camel/trunk/camel-core/src/main/java/org/apache/camel/FailedToCreateConsumerException.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/FailedToCreateConsumerException.java?rev=759657&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/FailedToCreateConsumerException.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/FailedToCreateConsumerException.java Sun Mar 29 09:35:59 2009
@@ -0,0 +1,35 @@
+/**
+ * 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;
+
+/**
+ * Thrown if Camel failed to create a consumer for a given endpoint.
+ *
+ * @version $Revision$
+ */
+public class FailedToCreateConsumerException extends RuntimeCamelException {
+ private final Endpoint endpoint;
+
+ public FailedToCreateConsumerException(Endpoint endpoint, Throwable cause) {
+ super("Failed to create Consumer for endpoint: " + endpoint + ". Reason: " + cause, cause);
+ this.endpoint = endpoint;
+ }
+
+ public Endpoint getEndpoint() {
+ return endpoint;
+ }
+}
Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/FailedToCreateConsumerException.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/FailedToCreateConsumerException.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/ResolveEndpointFailedException.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/ResolveEndpointFailedException.java?rev=759657&r1=759656&r2=759657&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/ResolveEndpointFailedException.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/ResolveEndpointFailedException.java Sun Mar 29 09:35:59 2009
@@ -25,7 +25,7 @@
private final String uri;
public ResolveEndpointFailedException(String uri, Throwable cause) {
- super("Failed to resolve endpoint: " + uri + " due to: " + cause, cause);
+ super("Failed to resolve endpoint: " + uri + " due to: " + cause.getMessage(), cause);
this.uri = uri;
}
@@ -34,6 +34,11 @@
this.uri = uri;
}
+ public ResolveEndpointFailedException(String uri) {
+ super("Failed to resolve endpoint: " + uri);
+ this.uri = uri;
+ }
+
public String getUri() {
return uri;
}
Added: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java?rev=759657&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java Sun Mar 29 09:35:59 2009
@@ -0,0 +1,91 @@
+/**
+ * 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.impl;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.FailedToCreateConsumerException;
+import org.apache.camel.PollingConsumer;
+import org.apache.camel.util.ServiceHelper;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+
+/**
+ * Cache containing created {@link org.apache.camel.Consumer}.
+ *
+ * @version $Revision$
+ */
+public class ConsumerCache extends ServiceSupport {
+ private static final transient Log LOG = LogFactory.getLog(ConsumerCache.class);
+
+ private final Map<String, PollingConsumer> consumers = new HashMap<String, PollingConsumer>();
+
+ public synchronized PollingConsumer getConsumer(Endpoint endpoint) {
+ String key = endpoint.getEndpointUri();
+ PollingConsumer answer = consumers.get(key);
+ if (answer == null) {
+ try {
+ answer = endpoint.createPollingConsumer();
+ answer.start();
+ } catch (Exception e) {
+ throw new FailedToCreateConsumerException(endpoint, e);
+ }
+ consumers.put(key, answer);
+ }
+ return answer;
+ }
+
+ public Exchange receive(Endpoint endpoint) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("<<<< " + endpoint);
+ }
+
+ PollingConsumer consumer = getConsumer(endpoint);
+ return consumer.receive();
+ }
+
+ public Exchange receive(Endpoint endpoint, long timeout) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("<<<< " + endpoint);
+ }
+
+ PollingConsumer consumer = getConsumer(endpoint);
+ return consumer.receive(timeout);
+ }
+
+ public Exchange receiveNoWait(Endpoint endpoint) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("<<<< " + endpoint);
+ }
+
+ PollingConsumer consumer = getConsumer(endpoint);
+ return consumer.receiveNoWait();
+ }
+
+ protected void doStop() throws Exception {
+ ServiceHelper.stopServices(consumers.values());
+ consumers.clear();
+ }
+
+ protected void doStart() throws Exception {
+ }
+
+}
Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ConsumerCache.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=759657&r1=759656&r2=759657&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java Sun Mar 29 09:35:59 2009
@@ -29,6 +29,7 @@
import org.apache.camel.CamelContext;
import org.apache.camel.Component;
+import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Endpoint;
import org.apache.camel.IsSingleton;
import org.apache.camel.NoFactoryAvailableException;
@@ -347,15 +348,15 @@
ObjectHelper.notEmpty(uri, "uri");
Endpoint answer;
+ String scheme = null;
synchronized (endpoints) {
answer = endpoints.get(uri);
if (answer == null) {
try {
-
// Use the URI prefix to find the component.
String splitURI[] = ObjectHelper.splitOnCharacter(uri, ":", 2);
if (splitURI[1] != null) {
- String scheme = splitURI[0];
+ scheme = splitURI[0];
Component component = getComponent(scheme);
// Ask the component to resolve the endpoint.
@@ -368,7 +369,9 @@
}
}
}
+
if (answer == null) {
+ // no component then try in registry and elsewhere
answer = createEndpoint(uri);
}
@@ -384,6 +387,12 @@
}
}
}
+
+ // unknown scheme
+ if (answer == null && scheme != null) {
+ throw new ResolveEndpointFailedException(uri, "No component found with scheme: " + scheme);
+ }
+
return answer;
}
@@ -739,6 +748,10 @@
return new DefaultProducerTemplate(this);
}
+ public ConsumerTemplate createConsumerTemplate() {
+ return new DefaultConsumerTemplate(this);
+ }
+
public ErrorHandlerBuilder getErrorHandlerBuilder() {
return errorHandlerBuilder;
}
Added: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultConsumerTemplate.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultConsumerTemplate.java?rev=759657&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultConsumerTemplate.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultConsumerTemplate.java Sun Mar 29 09:35:59 2009
@@ -0,0 +1,178 @@
+/**
+ * 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.impl;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ConsumerTemplate;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.util.CamelContextHelper;
+import static org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException;
+
+/**
+ * @version $Revision$
+ */
+public class DefaultConsumerTemplate implements ConsumerTemplate {
+
+ private final CamelContext context;
+ private final ConsumerCache consumerCache = new ConsumerCache();
+
+ public DefaultConsumerTemplate(CamelContext context) {
+ this.context = context;
+ }
+
+ public void start() throws Exception {
+ consumerCache.start();
+ }
+
+ public void stop() throws Exception {
+ consumerCache.stop();
+ }
+
+ public Exchange receive(String endpointUri) {
+ Endpoint endpoint = resolveMandatoryEndpoint(endpointUri);
+ return consumerCache.receive(endpoint);
+ }
+
+ public Exchange receive(Endpoint endpoinit) {
+ return receive(endpoinit.getEndpointUri());
+ }
+
+ public Exchange receive(String endpointUri, long timeout) {
+ Endpoint endpoint = resolveMandatoryEndpoint(endpointUri);
+ return consumerCache.receive(endpoint, timeout);
+ }
+
+ public Exchange receive(Endpoint endpoint, long timeout) {
+ return receive(endpoint.getEndpointUri(), timeout);
+ }
+
+ public Exchange receiveNoWait(String endpointUri) {
+ Endpoint endpoint = resolveMandatoryEndpoint(endpointUri);
+ return consumerCache.receiveNoWait(endpoint);
+ }
+
+ public Exchange receiveNoWait(Endpoint endpoint) {
+ return receiveNoWait(endpoint.getEndpointUri());
+ }
+
+ public Object receiveBody(String endpointUri) {
+ Exchange exchange = receive(endpointUri);
+ return extractResultBody(exchange);
+ }
+
+ public Object receiveBody(Endpoint endpoint) {
+ return receiveBody(endpoint.getEndpointUri());
+ }
+
+ public Object receiveBody(String endpointUri, long timeout) {
+ Exchange exchange = receive(endpointUri, timeout);
+ return extractResultBody(exchange);
+ }
+
+ public Object receiveBody(Endpoint endpoint, long timeout) {
+ return receiveBody(endpoint.getEndpointUri(), timeout);
+ }
+
+ public Object receiveBodyNoWait(String endpointUri) {
+ Exchange exchange = receiveNoWait(endpointUri);
+ return extractResultBody(exchange);
+ }
+
+ public Object receiveBodyNoWait(Endpoint endpoint) {
+ return receiveBodyNoWait(endpoint.getEndpointUri());
+ }
+
+ public <T> T receiveBody(String endpointUri, Class<T> type) {
+ Object body = receiveBody(endpointUri);
+ return context.getTypeConverter().convertTo(type, body);
+ }
+
+ public <T> T receiveBody(Endpoint endpoint, Class<T> type) {
+ return receiveBody(endpoint.getEndpointUri(), type);
+ }
+
+ public <T> T receiveBody(String endpointUri, long timeout, Class<T> type) {
+ Object body = receiveBody(endpointUri, timeout);
+ return context.getTypeConverter().convertTo(type, body);
+ }
+
+ public <T> T receiveBody(Endpoint endpoint, long timeout, Class<T> type) {
+ return receiveBody(endpoint.getEndpointUri(), timeout, type);
+ }
+
+ public <T> T receiveBodyNoWait(String endpointUri, Class<T> type) {
+ Object body = receiveBodyNoWait(endpointUri);
+ return context.getTypeConverter().convertTo(type, body);
+ }
+
+ public <T> T receiveBodyNoWait(Endpoint endpoint, Class<T> type) {
+ return receiveBodyNoWait(endpoint.getEndpointUri(), type);
+ }
+
+ protected Endpoint resolveMandatoryEndpoint(String endpointUri) {
+ return CamelContextHelper.getMandatoryEndpoint(context, endpointUri);
+ }
+
+ /**
+ * Extracts the body from the given result.
+ * <p/>
+ * If the exchange pattern is provided it will try to honor it and retrive the body
+ * from either IN or OUT according to the pattern.
+ *
+ * @param result the result
+ * @return the result, can be <tt>null</tt>.
+ */
+ protected Object extractResultBody(Exchange result) {
+ Object answer = null;
+ if (result != null) {
+ // rethrow if there was an exception
+ if (result.getException() != null) {
+ throw wrapRuntimeCamelException(result.getException());
+ }
+
+ // result could have a fault message
+ if (hasFaultMessage(result)) {
+ return result.getFault().getBody();
+ }
+
+ // okay no fault then return the response
+ boolean hasOut = result.getOut(false) != null;
+ if (hasOut) {
+ // use OUT as the response
+ answer = result.getOut().getBody();
+ } else {
+ // use IN as the response
+ answer = result.getIn().getBody();
+ }
+ }
+ return answer;
+ }
+
+ protected boolean hasFaultMessage(Exchange result) {
+ Message faultMessage = result.getFault(false);
+ if (faultMessage != null) {
+ Object faultBody = faultMessage.getBody();
+ if (faultBody != null) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+}
Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultConsumerTemplate.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultConsumerTemplate.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/ContextTestSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/ContextTestSupport.java?rev=759657&r1=759656&r2=759657&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/ContextTestSupport.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/ContextTestSupport.java Sun Mar 29 09:35:59 2009
@@ -39,6 +39,7 @@
public abstract class ContextTestSupport extends TestSupport {
protected CamelContext context;
protected ProducerTemplate template;
+ protected ConsumerTemplate consumer;
private boolean useRouteBuilder = true;
private Service camelContextService;
@@ -69,6 +70,7 @@
assertValidContext(context);
template = context.createProducerTemplate();
+ consumer = context.createConsumerTemplate();
if (isUseRouteBuilder()) {
RouteBuilder[] builders = createRouteBuilders();
@@ -86,7 +88,12 @@
@Override
protected void tearDown() throws Exception {
log.debug("tearDown test: " + getName());
- template.stop();
+ if (consumer != null) {
+ consumer.stop();
+ }
+ if (template != null) {
+ template.stop();
+ }
stopCamelContext();
}
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanConsumerTemplateTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanConsumerTemplateTest.java?rev=759657&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanConsumerTemplateTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanConsumerTemplateTest.java Sun Mar 29 09:35:59 2009
@@ -0,0 +1,75 @@
+/**
+ * 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.bean;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
+
+/**
+ * @version $Revision$
+ */
+public class BeanConsumerTemplateTest extends ContextTestSupport {
+
+ @Override
+ protected JndiRegistry createRegistry() throws Exception {
+ JndiRegistry jndi = super.createRegistry();
+ jndi.bind("cool", new MyCoolBean());
+ return jndi;
+ }
+
+ public void testConsumeBeanFromRoute() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("Hello World");
+
+ template.sendBody("direct:start", "start");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ public void testConsumeBeanFromTemplate() throws Exception {
+ Exchange out = consumer.receive("bean:cool");
+ assertEquals("Hello World", out.getOut().getBody());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .process(new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ Exchange consumed = consumer.receive("bean:cool");
+ exchange.getIn().setBody(consumed.getOut().getBody());
+ }
+ }).to("mock:result");
+ }
+ };
+ }
+
+ public class MyCoolBean {
+
+ public String say() {
+ return "Hello World";
+ }
+ }
+
+}
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanConsumerTemplateTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanConsumerTemplateTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeTemplateTest.java (from r759434, camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerBatchTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeTemplateTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeTemplateTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerBatchTest.java&r1=759434&r2=759657&rev=759657&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerBatchTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeTemplateTest.java Sun Mar 29 09:35:59 2009
@@ -18,41 +18,24 @@
import org.apache.camel.ContextTestSupport;
import org.apache.camel.Exchange;
-import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.mock.MockEndpoint;
/**
- * Unit test for consuming a batch of files (multiple files in one consume)
+ * Using ConsumerTemplate to consume a file
*/
-public class FileConsumerBatchTest extends ContextTestSupport {
+public class FileConsumeTemplateTest extends ContextTestSupport {
@Override
protected void setUp() throws Exception {
- deleteDirectory("target/file-batch");
+ deleteDirectory("target/template");
super.setUp();
- template.sendBodyAndHeader("file://target/file-batch/", "Hello World", Exchange.FILE_NAME, "hello.txt");
- template.sendBodyAndHeader("file://target/file-batch/", "Bye World", Exchange.FILE_NAME, "bye.txt");
+ template.sendBodyAndHeader("file://target/template/", "Hello World", Exchange.FILE_NAME, "hello.txt");
+ template.sendBodyAndHeader("file://target/template/", "Bye World", Exchange.FILE_NAME, "bye.txt");
}
- @Override
- protected RouteBuilder createRouteBuilder() throws Exception {
- return new RouteBuilder() {
- public void configure() throws Exception {
- from("file://target/file-batch?consumer.delay=1000").to("mock:result");
- }
- };
- }
-
- public void testConsumeBatch() throws Exception {
- MockEndpoint mock = getMockEndpoint("mock:result");
- mock.expectedBodiesReceivedInAnyOrder("Hello World", "Bye World");
-
- // test header keys
- mock.message(0).header(Exchange.FILE_BATCH_SIZE).isEqualTo(2);
- mock.message(0).header(Exchange.FILE_BATCH_INDEX).isEqualTo(0);
- mock.message(1).header(Exchange.FILE_BATCH_INDEX).isEqualTo(1);
-
- assertMockEndpointsSatisfied();
+ public void testConsumeFileWithTemplate() throws Exception {
+ Exchange out = consumer.receive("file://target/template?fileName=hello.txt");
+ assertNotNull(out);
+ assertEquals("Hello World", out.getIn().getBody(String.class));
}
}
\ No newline at end of file
Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextTest.java?rev=759657&r1=759656&r2=759657&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextTest.java Sun Mar 29 09:35:59 2009
@@ -20,7 +20,11 @@
import junit.framework.TestCase;
import org.apache.camel.Component;
+import org.apache.camel.Endpoint;
+import org.apache.camel.NoSuchEndpointException;
+import org.apache.camel.ResolveEndpointFailedException;
import org.apache.camel.component.bean.BeanComponent;
+import org.apache.camel.util.CamelContextHelper;
/**
* @version $Revision$
@@ -51,4 +55,30 @@
assertEquals("bean", list.get(0));
}
+ public void testGetEndpoint() throws Exception {
+ DefaultCamelContext ctx = new DefaultCamelContext();
+ Endpoint endpoint = ctx.getEndpoint("log:foo");
+ assertNotNull(endpoint);
+ }
+
+ public void testGetEndpointNotFound() throws Exception {
+ DefaultCamelContext ctx = new DefaultCamelContext();
+ try {
+ ctx.getEndpoint("xxx:foo");
+ fail("Should have thrown a ResolveEndpointFailedException");
+ } catch (ResolveEndpointFailedException e) {
+ assertTrue(e.getMessage().contains("No component found with scheme: xxx"));
+ }
+ }
+
+ public void testGetEndpointNoScheme() throws Exception {
+ DefaultCamelContext ctx = new DefaultCamelContext();
+ try {
+ CamelContextHelper.getMandatoryEndpoint(ctx, "log.foo");
+ fail("Should have thrown a NoSuchEndpointException");
+ } catch (NoSuchEndpointException e) {
+ // expected
+ }
+ }
+
}
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultConsumerTemplateTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultConsumerTemplateTest.java?rev=759657&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultConsumerTemplateTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultConsumerTemplateTest.java Sun Mar 29 09:35:59 2009
@@ -0,0 +1,154 @@
+/**
+ * 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.impl;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+
+/**
+ * @version $Revision$
+ */
+public class DefaultConsumerTemplateTest extends ContextTestSupport {
+
+ private DefaultConsumerTemplate consumer;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ consumer = new DefaultConsumerTemplate(context);
+ consumer.start();
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ consumer.stop();
+ super.tearDown();
+ }
+
+ public void testConsumeReceive() throws Exception {
+ template.sendBody("seda:foo", "Hello");
+
+ Exchange out = consumer.receive("seda:foo");
+ assertNotNull(out);
+ assertEquals("Hello", out.getIn().getBody());
+ }
+
+ public void testConsumeTwiceReceive() throws Exception {
+ template.sendBody("seda:foo", "Hello");
+
+ Exchange out = consumer.receive("seda:foo");
+ assertNotNull(out);
+ assertEquals("Hello", out.getIn().getBody());
+
+ template.sendBody("seda:foo", "Bye");
+
+ out = consumer.receive("seda:foo");
+ assertNotNull(out);
+ assertEquals("Bye", out.getIn().getBody());
+ }
+
+ public void testConsumeReceiveNoWait() throws Exception {
+ Exchange out = consumer.receiveNoWait("seda:foo");
+ assertNull(out);
+
+ template.sendBody("seda:foo", "Hello");
+
+ // a little delay to let the consumer see it
+ Thread.sleep(10);
+
+ out = consumer.receiveNoWait("seda:foo");
+ assertEquals("Hello", out.getIn().getBody());
+ }
+
+ public void testConsumeReceiveTimeout() throws Exception {
+ long start = System.currentTimeMillis();
+ Exchange out = consumer.receive("seda:foo", 1000);
+ assertNull(out);
+ long delta = System.currentTimeMillis() - start;
+ assertTrue("Should take about 1 sec: " + delta, delta < 1500);
+
+ template.sendBody("seda:foo", "Hello");
+
+ out = consumer.receive("seda:foo");
+ assertEquals("Hello", out.getIn().getBody());
+ }
+
+ public void testConsumeReceiveBody() throws Exception {
+ template.sendBody("seda:foo", "Hello");
+
+ Object body = consumer.receiveBody("seda:foo");
+ assertEquals("Hello", body);
+ }
+
+ public void testConsumeTwiceReceiveBody() throws Exception {
+ template.sendBody("seda:foo", "Hello");
+
+ Object body = consumer.receiveBody("seda:foo");
+ assertEquals("Hello", body);
+
+ template.sendBody("seda:foo", "Bye");
+
+ body = consumer.receiveBody("seda:foo");
+ assertEquals("Bye", body);
+ }
+
+ public void testConsumeReceiveBodyNoWait() throws Exception {
+ Object body = consumer.receiveBodyNoWait("seda:foo");
+ assertNull(body);
+
+ template.sendBody("seda:foo", "Hello");
+
+ // a little delay to let the consumer see it
+ Thread.sleep(10);
+
+ body = consumer.receiveBodyNoWait("seda:foo");
+ assertEquals("Hello", body);
+ }
+
+ public void testConsumeReceiveBodyString() throws Exception {
+ template.sendBody("seda:foo", "Hello");
+
+ String body = consumer.receiveBody("seda:foo", String.class);
+ assertEquals("Hello", body);
+ }
+
+ public void testConsumeTwiceReceiveBodyString() throws Exception {
+ template.sendBody("seda:foo", "Hello");
+
+ String body = consumer.receiveBody("seda:foo", String.class);
+ assertEquals("Hello", body);
+
+ template.sendBody("seda:foo", "Bye");
+
+ body = consumer.receiveBody("seda:foo", String.class);
+ assertEquals("Bye", body);
+ }
+
+ public void testConsumeReceiveBodyStringNoWait() throws Exception {
+ String body = consumer.receiveBodyNoWait("seda:foo", String.class);
+ assertNull(body);
+
+ template.sendBody("seda:foo", "Hello");
+
+ // a little delay to let the consumer see it
+ Thread.sleep(10);
+
+ body = consumer.receiveBodyNoWait("seda:foo", String.class);
+ assertEquals("Hello", body);
+ }
+
+}
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultConsumerTemplateTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultConsumerTemplateTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/RouteWithMistypedComponentNameTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/RouteWithMistypedComponentNameTest.java?rev=759657&r1=759656&r2=759657&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/RouteWithMistypedComponentNameTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/RouteWithMistypedComponentNameTest.java Sun Mar 29 09:35:59 2009
@@ -17,7 +17,8 @@
package org.apache.camel.impl;
import org.apache.camel.CamelContext;
-import org.apache.camel.NoSuchEndpointException;
+import org.apache.camel.Endpoint;
+import org.apache.camel.ResolveEndpointFailedException;
import org.apache.camel.TestSupport;
import org.apache.camel.builder.RouteBuilder;
@@ -28,11 +29,10 @@
public void testNoSuchComponent() throws Exception {
CamelContext context = new DefaultCamelContext();
- context.addRoutes(createRouteBuilder());
try {
- context.start();
- fail("Should have thrown a NoSuchEndpointException");
- } catch (NoSuchEndpointException e) {
+ context.addRoutes(createRouteBuilder());
+ fail("Should have thrown a ResolveEndpointFailedException");
+ } catch (ResolveEndpointFailedException e) {
// expected
}
}
@@ -40,7 +40,10 @@
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
public void configure() throws Exception {
- from("mistyped://hello").to("mock:result");
+ from("direct:hello").to("mock:result");
+
+ // unknown component
+ Endpoint endpoint = endpoint("mistyped:hello");
}
};
}
Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/management/CamelChoiceWithManagementTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/CamelChoiceWithManagementTest.java?rev=759657&r1=759656&r2=759657&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/management/CamelChoiceWithManagementTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/management/CamelChoiceWithManagementTest.java Sun Mar 29 09:35:59 2009
@@ -34,18 +34,8 @@
private MockEndpoint d;
private MockEndpoint e;
-
protected void setUp() throws Exception {
- context = createCamelContext();
- assertValidContext(context);
-
- template = context.createProducerTemplate();
- RouteBuilder[] builders = createRouteBuilders();
- for (RouteBuilder builder : builders) {
- context.addRoutes(builder);
- }
- startCamelContext();
-
+ super.setUp();
a = getMockEndpoint("mock:a");
b = getMockEndpoint("mock:b");
c = getMockEndpoint("mock:c");
@@ -53,10 +43,6 @@
e = getMockEndpoint("mock:e");
}
- protected void tearDown() throws Exception {
- super.tearDown();
- }
-
public void testFirstChoiceRoute() throws Exception {
final String body = "<one/>";
a.expectedBodiesReceived(body);
Copied: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelConsumerTemplateFactoryBean.java (from r759434, camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelTemplateFactoryBean.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelConsumerTemplateFactoryBean.java?p2=camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelConsumerTemplateFactoryBean.java&p1=camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelTemplateFactoryBean.java&r1=759434&r2=759657&rev=759657&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelTemplateFactoryBean.java (original)
+++ camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelConsumerTemplateFactoryBean.java Sun Mar 29 09:35:59 2009
@@ -18,29 +18,25 @@
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
-import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
-import org.apache.camel.Endpoint;
-import org.apache.camel.impl.DefaultProducerTemplate;
+import org.apache.camel.impl.DefaultConsumerTemplate;
import org.apache.camel.model.IdentifiedType;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
/**
- * A Spring {@link FactoryBean} for creating a new {@link org.apache.camel.ProducerTemplate}
+ * A Spring {@link org.springframework.beans.factory.FactoryBean} for creating a new {@link org.apache.camel.ConsumerTemplate}
* instance with a minimum of XML
- *
+ *
* @version $Revision$
*/
-@XmlRootElement(name = "template")
+@XmlRootElement(name = "consumer")
@XmlAccessorType(XmlAccessType.FIELD)
-public class CamelTemplateFactoryBean extends IdentifiedType implements FactoryBean, InitializingBean, CamelContextAware {
- @XmlAttribute(required = false)
- private String defaultEndpoint;
+public class CamelConsumerTemplateFactoryBean extends IdentifiedType implements FactoryBean, InitializingBean, CamelContextAware {
@XmlTransient
private CamelContext camelContext;
@@ -51,20 +47,11 @@
}
public Object getObject() throws Exception {
- CamelContext context = getCamelContext();
- if (defaultEndpoint != null) {
- Endpoint endpoint = context.getEndpoint(defaultEndpoint);
- if (endpoint == null) {
- throw new IllegalArgumentException("No endpoint found for URI: " + defaultEndpoint);
- } else {
- return new DefaultProducerTemplate(context, endpoint);
- }
- }
- return new DefaultProducerTemplate(context);
+ return new DefaultConsumerTemplate(getCamelContext());
}
public Class getObjectType() {
- return DefaultProducerTemplate.class;
+ return DefaultConsumerTemplate.class;
}
public boolean isSingleton() {
@@ -81,15 +68,4 @@
this.camelContext = camelContext;
}
- public String getDefaultEndpoint() {
- return defaultEndpoint;
- }
-
- /**
- * Sets the default endpoint URI used by default for sending message
- * exchanges
- */
- public void setDefaultEndpoint(String defaultEndpoint) {
- this.defaultEndpoint = defaultEndpoint;
- }
-}
+}
\ No newline at end of file
Modified: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java?rev=759657&r1=759656&r2=759657&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java (original)
+++ camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java Sun Mar 29 09:35:59 2009
@@ -18,7 +18,6 @@
import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
@@ -97,18 +96,19 @@
private CamelJMXAgentDefinition camelJMXAgent;
@XmlElements({
@XmlElement(name = "beanPostProcessor", type = CamelBeanPostProcessor.class, required = false),
- @XmlElement(name = "template", type = CamelTemplateFactoryBean.class, required = false),
+ @XmlElement(name = "template", type = CamelProducerTemplateFactoryBean.class, required = false),
+ @XmlElement(name = "consumerTemplate", type = CamelConsumerTemplateFactoryBean.class, required = false),
@XmlElement(name = "proxy", type = CamelProxyFactoryDefinition.class, required = false),
@XmlElement(name = "export", type = CamelServiceExporterDefinition.class, required = false)})
private List beans;
@XmlElement(name = "routeBuilder", required = false)
private List<RouteBuilderDefinition> builderRefs = new ArrayList<RouteBuilderDefinition>();
@XmlElement(name = "endpoint", required = false)
- private List<EndpointFactoryBean> endpoints;
+ private List<CamelEndpointFactoryBean> endpoints;
@XmlElement(name = "dataFormats", required = false)
private DataFormatsDefinition dataFormats;
@XmlElement(name = "onException", required = false)
- private List<OnExceptionDefinition> exceptionClauses = new ArrayList<OnExceptionDefinition>();
+ private List<OnExceptionDefinition> onExceptions = new ArrayList<OnExceptionDefinition>();
@XmlElement(name = "intercept", required = false)
private List<InterceptDefinition> intercepts = new ArrayList<InterceptDefinition>();
@XmlElement(name = "route", required = false)
@@ -215,8 +215,8 @@
// setup the intercepts
for (RouteDefinition route : routes) {
- if (exceptionClauses != null) {
- route.getOutputs().addAll(exceptionClauses);
+ if (onExceptions != null) {
+ route.getOutputs().addAll(onExceptions);
}
for (InterceptDefinition intercept : intercepts) {
@@ -560,11 +560,11 @@
return dataFormats;
}
- public void setExceptionClauses(List<OnExceptionDefinition> exceptionClauses) {
- this.exceptionClauses = exceptionClauses;
+ public void setOnExceptions(List<OnExceptionDefinition> onExceptions) {
+ this.onExceptions = onExceptions;
}
- public List<OnExceptionDefinition> getExceptionClauses() {
- return exceptionClauses;
+ public List<OnExceptionDefinition> getOnExceptions() {
+ return onExceptions;
}
}
Copied: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelEndpointFactoryBean.java (from r759434, camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/EndpointFactoryBean.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelEndpointFactoryBean.java?p2=camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelEndpointFactoryBean.java&p1=camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/EndpointFactoryBean.java&r1=759434&r2=759657&rev=759657&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/EndpointFactoryBean.java (original)
+++ camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelEndpointFactoryBean.java Sun Mar 29 09:35:59 2009
@@ -39,7 +39,7 @@
*/
@XmlRootElement(name = "endpoint")
@XmlAccessorType(XmlAccessType.FIELD)
-public class EndpointFactoryBean extends IdentifiedType implements FactoryBean, CamelContextAware {
+public class CamelEndpointFactoryBean extends IdentifiedType implements FactoryBean, CamelContextAware {
@XmlAttribute
private String uri;
@XmlTransient
Propchange: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelEndpointFactoryBean.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelEndpointFactoryBean.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Copied: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelProducerTemplateFactoryBean.java (from r759434, camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelTemplateFactoryBean.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelProducerTemplateFactoryBean.java?p2=camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelProducerTemplateFactoryBean.java&p1=camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelTemplateFactoryBean.java&r1=759434&r2=759657&rev=759657&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelTemplateFactoryBean.java (original)
+++ camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelProducerTemplateFactoryBean.java Sun Mar 29 09:35:59 2009
@@ -38,7 +38,7 @@
*/
@XmlRootElement(name = "template")
@XmlAccessorType(XmlAccessType.FIELD)
-public class CamelTemplateFactoryBean extends IdentifiedType implements FactoryBean, InitializingBean, CamelContextAware {
+public class CamelProducerTemplateFactoryBean extends IdentifiedType implements FactoryBean, InitializingBean, CamelContextAware {
@XmlAttribute(required = false)
private String defaultEndpoint;
@XmlTransient
Propchange: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelProducerTemplateFactoryBean.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelProducerTemplateFactoryBean.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java?rev=759657&r1=759656&r2=759657&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java (original)
+++ camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java Sun Mar 29 09:35:59 2009
@@ -32,7 +32,6 @@
import org.apache.camel.builder.xml.Namespaces;
import org.apache.camel.model.FromDefinition;
import org.apache.camel.model.SendDefinition;
-import org.apache.camel.model.config.PropertiesDefinition;
import org.apache.camel.model.dataformat.ArtixDSDataFormat;
import org.apache.camel.model.dataformat.JaxbDataFormat;
import org.apache.camel.model.dataformat.SerializationDataFormat;
@@ -41,12 +40,14 @@
import org.apache.camel.model.loadbalancer.RoundRobinLoadBalanceStrategy;
import org.apache.camel.model.loadbalancer.StickyLoadBalanceStrategy;
import org.apache.camel.model.loadbalancer.TopicLoadBalanceStrategy;
+import org.apache.camel.processor.loadbalancer.FailOverLoadBalancer;
import org.apache.camel.spi.NamespaceAware;
import org.apache.camel.spring.CamelBeanPostProcessor;
+import org.apache.camel.spring.CamelConsumerTemplateFactoryBean;
import org.apache.camel.spring.CamelContextFactoryBean;
+import org.apache.camel.spring.CamelEndpointFactoryBean;
import org.apache.camel.spring.CamelJMXAgentDefinition;
-import org.apache.camel.spring.CamelTemplateFactoryBean;
-import org.apache.camel.spring.EndpointFactoryBean;
+import org.apache.camel.spring.CamelProducerTemplateFactoryBean;
import org.apache.camel.spring.remoting.CamelProxyFactoryBean;
import org.apache.camel.spring.remoting.CamelServiceExporter;
import org.apache.camel.util.ObjectHelper;
@@ -64,7 +65,7 @@
*/
public class CamelNamespaceHandler extends NamespaceHandlerSupport {
- protected BeanDefinitionParser endpointParser = new BeanDefinitionParser(EndpointFactoryBean.class);
+ protected BeanDefinitionParser endpointParser = new BeanDefinitionParser(CamelEndpointFactoryBean.class);
protected BeanDefinitionParser beanPostProcessorParser = new BeanDefinitionParser(CamelBeanPostProcessor.class);
protected Set<String> parserElementNames = new HashSet<String>();
protected Binder<Node> binder;
@@ -78,10 +79,12 @@
public void init() {
// remoting
addBeanDefinitionParser("proxy", CamelProxyFactoryBean.class);
- addBeanDefinitionParser("template", CamelTemplateFactoryBean.class);
+ addBeanDefinitionParser("template", CamelProducerTemplateFactoryBean.class);
+ addBeanDefinitionParser("consumerTemplate", CamelConsumerTemplateFactoryBean.class);
addBeanDefinitionParser("export", CamelServiceExporter.class);
// data types
+ // TODO: why do we have this for data types, and only these 4 out of the 10+ data types we have in total?
addBeanDefinitionParser("artixDS", ArtixDSDataFormat.class);
addBeanDefinitionParser("jaxb", JaxbDataFormat.class);
addBeanDefinitionParser("serialization", SerializationDataFormat.class);
@@ -92,12 +95,13 @@
addBeanDefinitionParser("random", RandomLoadBalanceStrategy.class);
addBeanDefinitionParser("sticky", StickyLoadBalanceStrategy.class);
addBeanDefinitionParser("topic", TopicLoadBalanceStrategy.class);
+ addBeanDefinitionParser("failover", FailOverLoadBalancer.class);
// jmx agent
addBeanDefinitionParser("jmxAgent", CamelJMXAgentDefinition.class);
// endpoint
- addBeanDefinitionParser("endpoint", EndpointFactoryBean.class);
+ addBeanDefinitionParser("endpoint", CamelEndpointFactoryBean.class);
// camel context
Class cl = CamelContextFactoryBean.class;
@@ -200,7 +204,7 @@
builder.addPropertyValue("routes", factoryBean.getRoutes());
builder.addPropertyValue("intercepts", factoryBean.getIntercepts());
builder.addPropertyValue("dataFormats", factoryBean.getDataFormats());
- builder.addPropertyValue("exceptionClauses", factoryBean.getExceptionClauses());
+ builder.addPropertyValue("onExceptions", factoryBean.getOnExceptions());
builder.addPropertyValue("builderRefs", factoryBean.getBuilderRefs());
builder.addPropertyValue("properties", factoryBean.getProperties());
@@ -240,7 +244,7 @@
}
// register as endpoint defined indirectly in the routes by from/to types having id explict set
- registerEndpointsWithIdsDefinedInFromToTypes(element, parserContext, contextId);
+ registerEndpointsWithIdsDefinedInFromOrToTypes(element, parserContext, contextId);
// lets inject the namespaces into any namespace aware POJOs
injectNamespaces(element);
@@ -274,7 +278,10 @@
}
}
- protected void registerEndpointsWithIdsDefinedInFromToTypes(Element element, ParserContext parserContext, String contextId) {
+ /**
+ * Uses for auto registering endpoints from the <tt>from</tt> or <tt>to</tt> DSL if they have an id attribute set
+ */
+ protected void registerEndpointsWithIdsDefinedInFromOrToTypes(Element element, ParserContext parserContext, String contextId) {
NodeList list = element.getChildNodes();
int size = list.getLength();
for (int i = 0; i < size; i++) {
@@ -287,7 +294,7 @@
registerEndpoint(childElement, parserContext, contextId);
}
// recursive
- registerEndpointsWithIdsDefinedInFromToTypes(childElement, parserContext, contextId);
+ registerEndpointsWithIdsDefinedInFromOrToTypes(childElement, parserContext, contextId);
}
}
}
Modified: camel/trunk/components/camel-spring/src/main/resources/org/apache/camel/spring/jaxb.index
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/resources/org/apache/camel/spring/jaxb.index?rev=759657&r1=759656&r2=759657&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/main/resources/org/apache/camel/spring/jaxb.index (original)
+++ camel/trunk/components/camel-spring/src/main/resources/org/apache/camel/spring/jaxb.index Sun Mar 29 09:35:59 2009
@@ -15,9 +15,10 @@
## limitations under the License.
## ------------------------------------------------------------------------
CamelBeanPostProcessor
+CamelConsumerTemplateFactoryBean
CamelContextFactoryBean
+CamelEndpointFactoryBean
CamelJMXAgentDefinition
+CamelProducerTemplateFactoryBean
CamelProxyFactoryDefinition
CamelServiceExporterDefinition
-CamelTemplateFactoryBean
-EndpointFactoryBean
Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/SpringConsumerTemplateTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/SpringConsumerTemplateTest.java?rev=759657&view=auto
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/SpringConsumerTemplateTest.java (added)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/SpringConsumerTemplateTest.java Sun Mar 29 09:35:59 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.spring;
+
+import org.apache.camel.ConsumerTemplate;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests;
+
+/**
+ * Unit test using the ConsumerTemplate
+ *
+ * @version $Revision$
+ */
+// START SNIPPET: e1
+@ContextConfiguration
+public class SpringConsumerTemplateTest extends AbstractJUnit38SpringContextTests {
+
+ @Autowired
+ private ProducerTemplate producer;
+
+ @Autowired
+ private ConsumerTemplate consumer;
+
+ @EndpointInject(name = "result")
+ private MockEndpoint mock;
+
+ public void testConsumeTemplate() throws Exception {
+ // we expect Hello World received in our mock endpoint
+ mock.expectedBodiesReceived("Hello World");
+
+ // we use the producer template to send a message to the seda:start endpoint
+ producer.sendBody("seda:start", "Hello World");
+
+ // we consume the body from seda:start
+ String body = consumer.receiveBody("seda:start", String.class);
+ assertEquals("Hello World", body);
+
+ // and then we send the body again to seda:foo so it will be routed to the mock
+ // endpoint so our unit test can complete
+ producer.sendBody("seda:foo", body);
+
+ // assert mock received the body
+ mock.assertIsSatisfied();
+ }
+
+}
+// END SNIPPET: e1
Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/SpringConsumerTemplateTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/SpringConsumerTemplateTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/SpringConsumerTemplateTest-context.xml (from r759434, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/RegistryInjection.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/SpringConsumerTemplateTest-context.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/SpringConsumerTemplateTest-context.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/RegistryInjection.xml&r1=759434&r2=759657&rev=759657&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/RegistryInjection.xml (original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/SpringConsumerTemplateTest-context.xml Sun Mar 29 09:35:59 2009
@@ -22,14 +22,19 @@
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
">
-
- <bean id="registry" class="org.apache.camel.impl.JndiRegistry"/>
-
- <camel:camelContext id="camel">
- <camel:route>
- <camel:from uri="seda:start"/>
- <camel:to uri="mock:result"/>
- </camel:route>
- </camel:camelContext>
+
+ <!-- START SNIPPET: e1 -->
+ <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
+ <!-- define a producer template -->
+ <template id="producer"/>
+ <!-- define a consumer template -->
+ <consumerTemplate id="consumer"/>
+
+ <route>
+ <from uri="seda:foo"/>
+ <to id="result" uri="mock:result"/>
+ </route>
+ </camelContext>
+ <!-- END SNIPPET: e1 -->
</beans>
\ No newline at end of file
Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/SpringConsumerTemplateTest-context.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/SpringConsumerTemplateTest-context.xml
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/SpringConsumerTemplateTest-context.xml
------------------------------------------------------------------------------
svn:mime-type = text/xml