Author: jstrachan
Date: Wed Oct 1 07:30:48 2008
New Revision: 700786
URL: http://svn.apache.org/viewvc?rev=700786&view=rev
Log:
added an initial implementation of CAMEL-953
Added:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/RecipientList.java
- copied, changed from r700455,
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Consume.java
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListTest.java
(contents, props changed)
- copied, changed from r700455,
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRouteTest.java
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RecipientList.java
Copied:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/RecipientList.java
(from r700455,
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Consume.java)
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/RecipientList.java?p2=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/RecipientList.java&p1=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Consume.java&r1=700455&r2=700786&rev=700786&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Consume.java
(original)
+++
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/RecipientList.java
Wed Oct 1 07:30:48 2008
@@ -22,20 +22,23 @@
import java.lang.annotation.Target;
/**
- * Subscribes a method to an [EMAIL PROTECTED] Endpoint} either via its
- * <a href="http://activemq.apache.org/camel/uris.html">URI</a> or via the
name of the endpoint reference
- * which is then resolved in a registry such as the Spring Application Context.
+ * Indicates that this method is to be used as a
+ * <a href="http://activemq.apache.org/camel/recipient-list.html">Dynamic
Recipient List</a> routing the incoming message
+ * to one or more endpoints.
*
- * When a message [EMAIL PROTECTED] Exchange} is received from the [EMAIL
PROTECTED] Endpoint} then the
+ * When a message [EMAIL PROTECTED] org.apache.camel.Exchange} is received
from an [EMAIL PROTECTED] org.apache.camel.Endpoint} then the
* <a href="http://activemq.apache.org/camel/bean-integration.html">Bean
Integration</a>
- * mechanism is used to map the incoming [EMAIL PROTECTED] Message} to the
method parameters.
- *
+ * mechanism is used to map the incoming [EMAIL PROTECTED]
org.apache.camel.Message} to the method parameters.
+ *
+ * The return value of the method is then converted to either a [EMAIL
PROTECTED] java.util.Collection} or array of objects where each
+ * element is converted to an [EMAIL PROTECTED] Endpoint} or a [EMAIL
PROTECTED] String}, or if it is not a collection/array then it is converted
+ * to an [EMAIL PROTECTED] Endpoint} or [EMAIL PROTECTED] String}.
+ *
+ * Then for each endpoint or URI the message is forwarded a separate copy.
+ *
* @version $Revision$
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR })
-public @interface Consume {
- String uri() default "";
-
- String ref() default "";
-}
+public @interface RecipientList {
+}
\ No newline at end of file
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java?rev=700786&r1=700785&r2=700786&view=diff
==============================================================================
---
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
(original)
+++
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
Wed Oct 1 07:30:48 2008
@@ -29,6 +29,9 @@
import org.apache.camel.ExchangePattern;
import org.apache.camel.Expression;
import org.apache.camel.Pattern;
+import org.apache.camel.model.language.MethodCallExpression;
+import org.apache.camel.model.language.ConstantExpression;
+import org.apache.camel.processor.RecipientList;
import org.apache.camel.util.ExchangeHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.commons.logging.Log;
@@ -51,6 +54,7 @@
private final boolean hasCustomAnnotation;
private Expression parametersExpression;
private ExchangePattern pattern = ExchangePattern.InOut;
+ private RecipientList recipientList;
public MethodInfo(Class type, Method method, List<ParameterInfo>
parameters, List<ParameterInfo> bodyParameters, boolean hasCustomAnnotation) {
this.type = type;
@@ -63,14 +67,17 @@
if (oneway != null) {
pattern = oneway.value();
}
+ if (method.getAnnotation(org.apache.camel.RecipientList.class) !=
null) {
+ recipientList = new RecipientList(new ConstantExpression(null));
+ }
}
public String toString() {
return method.toString();
}
- public MethodInvocation createMethodInvocation(final Object pojo, final
Exchange messageExchange) {
- final Object[] arguments = (Object[])
parametersExpression.evaluate(messageExchange);
+ public MethodInvocation createMethodInvocation(final Object pojo, final
Exchange exchange) {
+ final Object[] arguments = (Object[])
parametersExpression.evaluate(exchange);
return new MethodInvocation() {
public Method getMethod() {
return method;
@@ -82,9 +89,13 @@
public Object proceed() throws Throwable {
if (LOG.isTraceEnabled()) {
- LOG.trace(">>>> invoking: " + method + " on bean: " + pojo
+ " with arguments: " + asString(arguments) + " for exchange: " +
messageExchange);
+ LOG.trace(">>>> invoking: " + method + " on bean: " + pojo
+ " with arguments: " + asString(arguments) + " for exchange: " + exchange);
+ }
+ Object result = invoke(method, pojo, arguments, exchange);
+ if (recipientList != null) {
+ recipientList.sendToRecipientList(exchange, result);
}
- return invoke(method, pojo, arguments, messageExchange);
+ return result;
}
public Object getThis() {
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RecipientList.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RecipientList.java?rev=700786&r1=700785&r2=700786&view=diff
==============================================================================
---
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RecipientList.java
(original)
+++
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RecipientList.java
Wed Oct 1 07:30:48 2008
@@ -58,6 +58,17 @@
public void process(Exchange exchange) throws Exception {
Object receipientList = expression.evaluate(exchange);
+ sendToRecipientList(exchange, receipientList);
+ }
+
+ /**
+ * Sends the given exchange to the recipient list
+ *
+ * @param exchange
+ * @param receipientList
+ * @throws Exception
+ */
+ public void sendToRecipientList(Exchange exchange, Object receipientList)
throws Exception {
Iterator iter = ObjectConverter.iterator(receipientList);
List<Processor> processors = new ArrayList<Processor>();
while (iter.hasNext()) {
Copied:
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListTest.java
(from r700455,
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRouteTest.java)
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListTest.java?p2=activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListTest.java&p1=activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRouteTest.java&r1=700455&r2=700786&rev=700786&view=diff
==============================================================================
---
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRouteTest.java
(original)
+++
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListTest.java
Wed Oct 1 07:30:48 2008
@@ -16,46 +16,31 @@
*/
package org.apache.camel.processor;
-import java.util.concurrent.atomic.AtomicInteger;
-
-import javax.naming.Context;
-
import org.apache.camel.ContextTestSupport;
-import org.apache.camel.Exchange;
-import org.apache.camel.Message;
-import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.bean.BeanProcessor;
import org.apache.camel.util.jndi.JndiContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import javax.naming.Context;
+import java.util.concurrent.atomic.AtomicInteger;
+
/**
* @version $Revision$
*/
-public class BeanRouteTest extends ContextTestSupport {
- private static final transient Log LOG =
LogFactory.getLog(BeanRouteTest.class);
+public class BeanRecipientListTest extends ContextTestSupport {
+ private static final transient Log LOG =
LogFactory.getLog(BeanRecipientListTest.class);
protected MyBean myBean = new MyBean();
- public void testSendingMessageWithMethodNameHeader() throws Exception {
- String expectedBody = "Wobble";
-
- template.sendBodyAndHeader("direct:in", expectedBody,
BeanProcessor.METHOD_NAME, "read");
+ public void testSendMessage() throws Exception {
+ final String expectedBody = "Wibble";
- assertEquals("bean received correct value for: " + myBean,
expectedBody, myBean.body);
- }
+ getMockEndpoint("mock:a").expectedBodiesReceived(expectedBody);
+ getMockEndpoint("mock:b").expectedBodiesReceived(expectedBody);
- public void testSendingMessageWithMethodNameHeaderWithMoreVerboseCoe()
throws Exception {
- final String expectedBody = "Wibble";
+ template.sendBody("direct:in", expectedBody);
- template.send("direct:in", new Processor() {
- public void process(Exchange exchange) {
- Message in = exchange.getIn();
- in.setBody(expectedBody);
- in.setHeader(BeanProcessor.METHOD_NAME, "read");
- }
- });
- assertEquals("bean received correct value", expectedBody, myBean.body);
+ assertMockEndpointsSatisfied();
}
@Override
@@ -76,14 +61,13 @@
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
- from("direct:in").beanRef("myBean");
+ from("direct:in").beanRef("myBean", "route");
}
};
}
public static class MyBean {
private static AtomicInteger counter = new AtomicInteger(0);
- public String body;
private int id;
public MyBean() {
@@ -95,13 +79,10 @@
return "MyBean:" + id;
}
- public void read(String body) {
- this.body = body;
- LOG.info("read() method on " + this + " with body: " + body);
- }
-
- public void wrongMethod(String body) {
- fail("wrongMethod() called with: " + body);
+ @org.apache.camel.RecipientList
+ public String[] route(String body) {
+ System.out.println("Called " + this + " with body: " + body);
+ return new String[] { "mock:a", "mock:b" };
}
}
}
\ No newline at end of file
Propchange:
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange:
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanRecipientListTest.java
------------------------------------------------------------------------------
svn:mergeinfo =