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 2015/05/14 20:34:10 UTC
camel git commit: CAMEL-8761: Idempotent Consumer EIP - Allow to
commit when EIP scope ends
Repository: camel
Updated Branches:
refs/heads/master 134507a38 -> 4a4b6a21a
CAMEL-8761: Idempotent Consumer EIP - Allow to commit when EIP scope ends
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/4a4b6a21
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/4a4b6a21
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/4a4b6a21
Branch: refs/heads/master
Commit: 4a4b6a21af299c630b1194a2c5903914c787d03f
Parents: 134507a
Author: Claus Ibsen <da...@apache.org>
Authored: Thu May 14 20:32:54 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu May 14 20:38:20 2015 +0200
----------------------------------------------------------------------
.../model/IdempotentConsumerDefinition.java | 65 +++------
.../camel/model/IdempotentConsumerScope.java | 31 ----
.../idempotent/IdempotentConsumer.java | 20 +--
.../resources/org/apache/camel/model/jaxb.index | 1 -
.../IdempotentConsumerCompletionEagerTest.java | 140 +++++++++++++++++++
.../processor/IdempotentConsumerScopeTest.java | 140 -------------------
6 files changed, 167 insertions(+), 230 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/4a4b6a21/camel-core/src/main/java/org/apache/camel/model/IdempotentConsumerDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/IdempotentConsumerDefinition.java b/camel-core/src/main/java/org/apache/camel/model/IdempotentConsumerDefinition.java
index 8652390..5d42ea3 100644
--- a/camel-core/src/main/java/org/apache/camel/model/IdempotentConsumerDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/IdempotentConsumerDefinition.java
@@ -43,8 +43,8 @@ public class IdempotentConsumerDefinition extends ExpressionNode {
private String messageIdRepositoryRef;
@XmlAttribute @Metadata(defaultValue = "true")
private Boolean eager;
- @XmlAttribute @Metadata(defaultValue = "OnCompletion")
- private IdempotentConsumerScope scope;
+ @XmlAttribute
+ private Boolean completionEager;
@XmlAttribute @Metadata(defaultValue = "true")
private Boolean skipDuplicate;
@XmlAttribute @Metadata(defaultValue = "true")
@@ -109,53 +109,22 @@ public class IdempotentConsumerDefinition extends ExpressionNode {
}
/**
- * Sets the scope of this idempotent consumer where its boundaries ends.
+ * Sets whether to complete the idempotent consumer eager or when the exchange is done.
* <p/>
- * The default mode is <tt>OnCompletion</tt> which means the idempotent consumer will
- * only trigger its completion at the end of the routing of the exchange, when the exchange completes.
- * So if the exchange is continued routed after the block ends, then whatever happens there <b>also</b> affect the state.
- * For example if the exchange failed due to an exception, then the state of the idempotent consumer will be a rollback.
- * <p/>
- * The other mode <tt>BlockOnly</tt> means that the idempotent consumer will trigger its completion
+ * If this option is <tt>true</tt> to complete eager, then the idempotent consumer will trigger its completion
* when the exchange reached the end of the block of the idempotent consumer pattern. So if the exchange
* is continued routed after the block ends, then whatever happens there does not affect the state.
- *
- * @param scope the scope to use
- * @return builder
- */
- public IdempotentConsumerDefinition scope(IdempotentConsumerScope scope) {
- setScope(scope);
- return this;
- }
-
- /**
- * Sets the scope of this idempotent consumer where its boundaries ends to <tt>BlockOnly</tt>.
- * <p/>
- * The <tt>BlockOnly</tt> mode means that the idempotent consumer will trigger its completion
- * when the exchange reached the end of the block of the idempotent consumer pattern. So if the exchange
- * is continued routed after the block ends, then whatever happens there does not affect the state.
- *
- * @see #scope(IdempotentConsumerScope)
- * @return builder
- */
- public IdempotentConsumerDefinition scopeBlockOnly() {
- setScope(IdempotentConsumerScope.BlockOnly);
- return this;
- }
-
- /**
- * Sets the scope of this idempotent consumer where its boundaries ends to <tt>onCompletion</tt>.
* <p/>
- * The <tt>OnCompletion</tt> mode means the idempotent consumer will
- * only trigger its completion at the end of the routing of the exchange, when the exchange completes.
- * So if the exchange is continued routed after the block ends, then whatever happens there <b>also</b> affect the state.
+ * If this option is <tt>false</tt> (default) to <b>not</b> complete eager, then the idempotent consumer
+ * will complete when the exchange is done being routed. So if the exchange is continued routed after the block ends,
+ * then whatever happens there <b>also</b> affect the state.
* For example if the exchange failed due to an exception, then the state of the idempotent consumer will be a rollback.
*
- * @see #scope(IdempotentConsumerScope)
+ * @param completionEager whether to complete eager or complete when the exchange is done
* @return builder
*/
- public IdempotentConsumerDefinition scopeOnCompletion() {
- setScope(IdempotentConsumerScope.OnCompletion);
+ public IdempotentConsumerDefinition completionEager(boolean completionEager) {
+ setCompletionEager(completionEager);
return this;
}
@@ -239,12 +208,12 @@ public class IdempotentConsumerDefinition extends ExpressionNode {
this.removeOnFailure = removeOnFailure;
}
- public IdempotentConsumerScope getScope() {
- return scope;
+ public Boolean getCompletionEager() {
+ return completionEager;
}
- public void setScope(IdempotentConsumerScope scope) {
- this.scope = scope;
+ public void setCompletionEager(Boolean completionEager) {
+ this.completionEager = completionEager;
}
@Override
@@ -265,10 +234,10 @@ public class IdempotentConsumerDefinition extends ExpressionNode {
boolean eager = getEager() == null || getEager();
boolean duplicate = getSkipDuplicate() == null || getSkipDuplicate();
boolean remove = getRemoveOnFailure() == null || getRemoveOnFailure();
- // and this is not true by default
- boolean scopeBlockOnly = getScope() != null && IdempotentConsumerScope.BlockOnly == getScope();
+ // these boolean should be false by default
+ boolean completionEager = getCompletionEager() != null && getCompletionEager();
- return new IdempotentConsumer(expression, idempotentRepository, eager, duplicate, remove, scopeBlockOnly, childProcessor);
+ return new IdempotentConsumer(expression, idempotentRepository, eager, completionEager, duplicate, remove, childProcessor);
}
/**
http://git-wip-us.apache.org/repos/asf/camel/blob/4a4b6a21/camel-core/src/main/java/org/apache/camel/model/IdempotentConsumerScope.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/IdempotentConsumerScope.java b/camel-core/src/main/java/org/apache/camel/model/IdempotentConsumerScope.java
deleted file mode 100644
index b42f28b..0000000
--- a/camel-core/src/main/java/org/apache/camel/model/IdempotentConsumerScope.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/**
- * 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.model;
-
-import javax.xml.bind.annotation.XmlEnum;
-import javax.xml.bind.annotation.XmlType;
-
-/**
- * Represents the scopes supported by the idempotent consumer EIP
- */
-@XmlType
-@XmlEnum
-public enum IdempotentConsumerScope {
-
- BlockOnly, OnCompletion
-
-}
http://git-wip-us.apache.org/repos/asf/camel/blob/4a4b6a21/camel-core/src/main/java/org/apache/camel/processor/idempotent/IdempotentConsumer.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/processor/idempotent/IdempotentConsumer.java b/camel-core/src/main/java/org/apache/camel/processor/idempotent/IdempotentConsumer.java
index 8b47556..acbfc27 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/idempotent/IdempotentConsumer.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/idempotent/IdempotentConsumer.java
@@ -57,19 +57,19 @@ public class IdempotentConsumer extends ServiceSupport implements AsyncProcessor
private final AsyncProcessor processor;
private final IdempotentRepository<String> idempotentRepository;
private final boolean eager;
+ private final boolean completionEager;
private final boolean skipDuplicate;
private final boolean removeOnFailure;
- private final boolean scopeBlockOnly;
private final AtomicLong duplicateMessageCount = new AtomicLong();
public IdempotentConsumer(Expression messageIdExpression, IdempotentRepository<String> idempotentRepository,
- boolean eager, boolean skipDuplicate, boolean removeOnFailure, boolean scopeBlockOnly, Processor processor) {
+ boolean eager, boolean completionEager, boolean skipDuplicate, boolean removeOnFailure, Processor processor) {
this.messageIdExpression = messageIdExpression;
this.idempotentRepository = idempotentRepository;
this.eager = eager;
+ this.completionEager = completionEager;
this.skipDuplicate = skipDuplicate;
this.removeOnFailure = removeOnFailure;
- this.scopeBlockOnly = scopeBlockOnly;
this.processor = AsyncProcessorConverterHelper.convert(processor);
}
@@ -132,8 +132,8 @@ public class IdempotentConsumer extends ServiceSupport implements AsyncProcessor
}
final Synchronization onCompletion = new IdempotentOnCompletion(idempotentRepository, messageId, eager, removeOnFailure);
- final AsyncCallback target = new IdempotentConsumerCallback(exchange, onCompletion, callback, scopeBlockOnly);
- if (!scopeBlockOnly) {
+ final AsyncCallback target = new IdempotentConsumerCallback(exchange, onCompletion, callback, completionEager);
+ if (!completionEager) {
// the scope is to do the idempotent completion work as an unit of work on the exchange when its done being routed
exchange.addOnCompletion(onCompletion);
}
@@ -215,26 +215,26 @@ public class IdempotentConsumer extends ServiceSupport implements AsyncProcessor
private final Exchange exchange;
private final Synchronization onCompletion;
private final AsyncCallback callback;
- private final boolean scopeBlockOnly;
+ private final boolean completionEager;
- public IdempotentConsumerCallback(Exchange exchange, Synchronization onCompletion, AsyncCallback callback, boolean scopeBlockOnly) {
+ public IdempotentConsumerCallback(Exchange exchange, Synchronization onCompletion, AsyncCallback callback, boolean completionEager) {
this.exchange = exchange;
this.onCompletion = onCompletion;
this.callback = callback;
- this.scopeBlockOnly = scopeBlockOnly;
+ this.completionEager = completionEager;
}
@Override
public void done(boolean doneSync) {
try {
- if (scopeBlockOnly) {
+ if (completionEager) {
if (exchange.isFailed()) {
onCompletion.onFailure(exchange);
} else {
onCompletion.onComplete(exchange);
}
}
- // if scope is not block only then the onCompletion is invoked as part of the UoW of the Exchange
+ // if completion is not eager then the onCompletion is invoked as part of the UoW of the Exchange
} finally {
callback.done(doneSync);
}
http://git-wip-us.apache.org/repos/asf/camel/blob/4a4b6a21/camel-core/src/main/resources/org/apache/camel/model/jaxb.index
----------------------------------------------------------------------
diff --git a/camel-core/src/main/resources/org/apache/camel/model/jaxb.index b/camel-core/src/main/resources/org/apache/camel/model/jaxb.index
index 5e6a3c0..fc79da2 100644
--- a/camel-core/src/main/resources/org/apache/camel/model/jaxb.index
+++ b/camel-core/src/main/resources/org/apache/camel/model/jaxb.index
@@ -31,7 +31,6 @@ FilterDefinition
FinallyDefinition
FromDefinition
IdempotentConsumerDefinition
-IdempotentConsumerScope
InOnlyDefinition
InOutDefinition
InterceptDefinition
http://git-wip-us.apache.org/repos/asf/camel/blob/4a4b6a21/camel-core/src/test/java/org/apache/camel/processor/IdempotentConsumerCompletionEagerTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/processor/IdempotentConsumerCompletionEagerTest.java b/camel-core/src/test/java/org/apache/camel/processor/IdempotentConsumerCompletionEagerTest.java
new file mode 100644
index 0000000..1003e15
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/processor/IdempotentConsumerCompletionEagerTest.java
@@ -0,0 +1,140 @@
+/**
+ * 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.processor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+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.mock.MockEndpoint;
+import org.apache.camel.processor.idempotent.MemoryIdempotentRepository;
+import org.apache.camel.spi.IdempotentRepository;
+
+/**
+ * @version
+ */
+public class IdempotentConsumerCompletionEagerTest extends ContextTestSupport {
+ protected Endpoint startEndpoint;
+ protected MockEndpoint resultEndpoint;
+ protected MockEndpoint a;
+ protected MockEndpoint b;
+ protected MockEndpoint dead;
+ protected IdempotentRepository<String> repo;
+
+ @Override
+ public boolean isUseRouteBuilder() {
+ return false;
+ }
+
+ public void testCompletionEager() throws Exception {
+ repo = MemoryIdempotentRepository.memoryIdempotentRepository(200);
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ errorHandler(deadLetterChannel("mock:dead"));
+
+ from("direct:start")
+ .idempotentConsumer(header("messageId"), repo).completionEager(true)
+ .to("log:a", "mock:a")
+ .to("log:b", "mock:b")
+ .end()
+ .filter(simple("${header.messageId} == '2'"))
+ .throwException(new IllegalArgumentException("Forced"))
+ .end()
+ .to("log:result", "mock:result");
+ }
+ });
+ context.start();
+
+ // we are on block only scope as "two" was success in the block, and then "two" failed afterwards does not matter
+ // the idempotent consumer will not receive "two" again
+ a.expectedBodiesReceived("one", "two", "three");
+ b.expectedBodiesReceived("one", "two", "three");
+ dead.expectedBodiesReceived("two", "two");
+ resultEndpoint.expectedBodiesReceived("one", "one", "one", "three");
+
+ sendMessage("1", "one");
+ sendMessage("2", "two");
+ sendMessage("1", "one");
+ sendMessage("2", "two");
+ sendMessage("1", "one");
+ sendMessage("3", "three");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ public void testNotCompletionEager() throws Exception {
+ repo = MemoryIdempotentRepository.memoryIdempotentRepository(200);
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ errorHandler(deadLetterChannel("mock:dead"));
+
+ from("direct:start")
+ .idempotentConsumer(header("messageId"), repo).completionEager(false)
+ .to("log:a", "mock:a")
+ .to("log:b", "mock:b")
+ .end()
+ .filter(simple("${header.messageId} == '2'"))
+ .throwException(new IllegalArgumentException("Forced"))
+ .end()
+ .to("log:result", "mock:result");
+ }
+ });
+ context.start();
+
+ // we are on completion scope so the "two" will rollback and therefore the idempotent consumer receives those again
+ a.expectedBodiesReceived("one", "two", "two", "three");
+ b.expectedBodiesReceived("one", "two", "two", "three");
+ dead.expectedBodiesReceived("two", "two");
+ resultEndpoint.expectedBodiesReceived("one", "one", "one", "three");
+
+ sendMessage("1", "one");
+ sendMessage("2", "two");
+ sendMessage("1", "one");
+ sendMessage("2", "two");
+ sendMessage("1", "one");
+ sendMessage("3", "three");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ protected void sendMessage(final Object messageId, final Object body) {
+ template.send(startEndpoint, new Processor() {
+ public void process(Exchange exchange) {
+ // now lets fire in a message
+ Message in = exchange.getIn();
+ in.setBody(body);
+ in.setHeader("messageId", messageId);
+ }
+ });
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ startEndpoint = resolveMandatoryEndpoint("direct:start");
+ resultEndpoint = getMockEndpoint("mock:result");
+ a = getMockEndpoint("mock:a");
+ b = getMockEndpoint("mock:b");
+ dead = getMockEndpoint("mock:dead");
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/camel/blob/4a4b6a21/camel-core/src/test/java/org/apache/camel/processor/IdempotentConsumerScopeTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/processor/IdempotentConsumerScopeTest.java b/camel-core/src/test/java/org/apache/camel/processor/IdempotentConsumerScopeTest.java
deleted file mode 100644
index 21b727a..0000000
--- a/camel-core/src/test/java/org/apache/camel/processor/IdempotentConsumerScopeTest.java
+++ /dev/null
@@ -1,140 +0,0 @@
-/**
- * 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.processor;
-
-import org.apache.camel.ContextTestSupport;
-import org.apache.camel.Endpoint;
-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.mock.MockEndpoint;
-import org.apache.camel.processor.idempotent.MemoryIdempotentRepository;
-import org.apache.camel.spi.IdempotentRepository;
-
-/**
- * @version
- */
-public class IdempotentConsumerScopeTest extends ContextTestSupport {
- protected Endpoint startEndpoint;
- protected MockEndpoint resultEndpoint;
- protected MockEndpoint a;
- protected MockEndpoint b;
- protected MockEndpoint dead;
- protected IdempotentRepository<String> repo;
-
- @Override
- public boolean isUseRouteBuilder() {
- return false;
- }
-
- public void testScopeBlockOnly() throws Exception {
- repo = MemoryIdempotentRepository.memoryIdempotentRepository(200);
- context.addRoutes(new RouteBuilder() {
- @Override
- public void configure() throws Exception {
- errorHandler(deadLetterChannel("mock:dead"));
-
- from("direct:start")
- .idempotentConsumer(header("messageId"), repo).scopeBlockOnly()
- .to("log:a", "mock:a")
- .to("log:b", "mock:b")
- .end()
- .filter(simple("${header.messageId} == '2'"))
- .throwException(new IllegalArgumentException("Forced"))
- .end()
- .to("log:result", "mock:result");
- }
- });
- context.start();
-
- // we are on block only scope as "two" was success in the block, and then "two" failed afterwards does not matter
- // the idempotent consumer will not receive "two" again
- a.expectedBodiesReceived("one", "two", "three");
- b.expectedBodiesReceived("one", "two", "three");
- dead.expectedBodiesReceived("two", "two");
- resultEndpoint.expectedBodiesReceived("one", "one", "one", "three");
-
- sendMessage("1", "one");
- sendMessage("2", "two");
- sendMessage("1", "one");
- sendMessage("2", "two");
- sendMessage("1", "one");
- sendMessage("3", "three");
-
- assertMockEndpointsSatisfied();
- }
-
- public void testScopeOnCompletion() throws Exception {
- repo = MemoryIdempotentRepository.memoryIdempotentRepository(200);
- context.addRoutes(new RouteBuilder() {
- @Override
- public void configure() throws Exception {
- errorHandler(deadLetterChannel("mock:dead"));
-
- from("direct:start")
- .idempotentConsumer(header("messageId"), repo).scopeOnCompletion()
- .to("log:a", "mock:a")
- .to("log:b", "mock:b")
- .end()
- .filter(simple("${header.messageId} == '2'"))
- .throwException(new IllegalArgumentException("Forced"))
- .end()
- .to("log:result", "mock:result");
- }
- });
- context.start();
-
- // we are on completion scope so the "two" will rollback and therefore the idempotent consumer receives those again
- a.expectedBodiesReceived("one", "two", "two", "three");
- b.expectedBodiesReceived("one", "two", "two", "three");
- dead.expectedBodiesReceived("two", "two");
- resultEndpoint.expectedBodiesReceived("one", "one", "one", "three");
-
- sendMessage("1", "one");
- sendMessage("2", "two");
- sendMessage("1", "one");
- sendMessage("2", "two");
- sendMessage("1", "one");
- sendMessage("3", "three");
-
- assertMockEndpointsSatisfied();
- }
-
- protected void sendMessage(final Object messageId, final Object body) {
- template.send(startEndpoint, new Processor() {
- public void process(Exchange exchange) {
- // now lets fire in a message
- Message in = exchange.getIn();
- in.setBody(body);
- in.setHeader("messageId", messageId);
- }
- });
- }
-
- @Override
- protected void setUp() throws Exception {
- super.setUp();
-
- startEndpoint = resolveMandatoryEndpoint("direct:start");
- resultEndpoint = getMockEndpoint("mock:result");
- a = getMockEndpoint("mock:a");
- b = getMockEndpoint("mock:b");
- dead = getMockEndpoint("mock:dead");
- }
-
-}
\ No newline at end of file