Author: davsclaus
Date: Sat Apr 10 16:33:11 2010
New Revision: 932757
URL: http://svn.apache.org/viewvc?rev=932757&view=rev
Log:
CAMEL-2568: RecoverableAggregationRepository now has max redeliveries and dead
letter channel.
Added:
camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregateRecoverDeadLetterChannelTest.java
- copied, changed from r932710,
camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregateRecoverTest.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java
camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RecoverableAggregationRepository.java
camel/trunk/components/camel-hawtdb/src/main/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepository.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java?rev=932757&r1=932756&r2=932757&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java
Sat Apr 10 16:33:11 2010
@@ -88,6 +88,8 @@ public class AggregateProcessor extends
private Map<Object, Object> closedCorrelationKeys;
private final Set<String> inProgressCompleteExchanges = new
HashSet<String>();
private final Map<String, RedeliveryData> redeliveryState = new
ConcurrentHashMap<String, RedeliveryData>();
+ // optional dead letter channel for exhausted recovered exchanges
+ private Processor deadLetterProcessor;
// keep booking about redelivery
private class RedeliveryData {
@@ -363,6 +365,10 @@ public class AggregateProcessor extends
if (exchange.getException() != null) {
// if there was an exception then let the exception
handler handle it
getExceptionHandler().handleException("Error processing
aggregated exchange", exchange, exchange.getException());
+ } else {
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Processing aggregated exchange: " +
exchange + " complete.");
+ }
}
}
});
@@ -476,12 +482,20 @@ public class AggregateProcessor extends
}
public void onFailure(Exchange exchange) {
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Aggregated exchange onFailure: " + exchange);
+ }
+
// must remember to remove in progress when we failed
inProgressCompleteExchanges.remove(exchangeId);
// do not remove redelivery state as we need it when we redeliver
again later
}
public void onComplete(Exchange exchange) {
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Aggregated exchange onComplete: " + exchange);
+ }
+
// only confirm if we processed without a problem
try {
aggregationRepository.confirm(exchange.getContext(),
exchangeId);
@@ -562,11 +576,11 @@ public class AggregateProcessor extends
boolean inProgress =
inProgressCompleteExchanges.contains(exchangeId);
if (inProgress) {
if (LOG.isTraceEnabled()) {
- LOG.trace("Aggregated exchange with id " + exchangeId
+ " is already in progress.");
+ LOG.trace("Aggregated exchange with id: " + exchangeId
+ " is already in progress.");
}
} else {
if (LOG.isDebugEnabled()) {
- LOG.debug("Recovering aggregated exchange with id " +
exchangeId);
+ LOG.debug("Loading aggregated exchange with id: " +
exchangeId + " to be recovered.");
}
Exchange exchange = recoverable.recover(camelContext,
exchangeId);
if (exchange != null) {
@@ -587,12 +601,28 @@ public class AggregateProcessor extends
// set redelivery counter
exchange.getIn().setHeader(Exchange.REDELIVERY_COUNTER, data.redeliveryCounter);
- // resubmit the recovered exchange
- try {
- lock.lock();
- onSubmitCompletion(key, exchange);
- } finally {
- lock.unlock();
+ // if we are exhausted, then move to dead letter
channel
+ if (recoverable.getMaximumRedeliveries() > 0 &&
data.redeliveryCounter > recoverable.getMaximumRedeliveries()) {
+ LOG.warn("The recovered exchange is exhausted
after " + recoverable.getMaximumRedeliveries()
+ + " attempts, will now be moved to dead
letter channel: " + recoverable.getDeadLetterUri());
+ try {
+ deadLetterProcessor.process(exchange);
+ // confirm after it has been moved to dead
letter channel
+ recoverable.confirm(camelContext, exchangeId);
+ } catch (Exception e) {
+ getExceptionHandler().handleException("Failed
to move recovered Exchange to dead letter channel: " +
recoverable.getDeadLetterUri(), e);
+ }
+ } else {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Delivery attempt: " +
data.redeliveryCounter + " to recover aggregated exchange with id: " +
exchangeId + "");
+ }
+ // not exhaust so resubmit the recovered exchange
+ try {
+ lock.lock();
+ onSubmitCompletion(key, exchange);
+ } finally {
+ lock.unlock();
+ }
}
}
}
@@ -639,6 +669,15 @@ public class AggregateProcessor extends
LOG.info("Using RecoverableAggregationRepository by scheduling
recover checker to run every " + interval + " millis.");
// use fixed delay so there is X interval between each run
recoverService.scheduleWithFixedDelay(recoverTask, 1000L,
interval, TimeUnit.MILLISECONDS);
+
+ if (recoverable.getDeadLetterUri() != null) {
+ int max = recoverable.getMaximumRedeliveries();
+ if (max <= 0) {
+ throw new IllegalArgumentException("Option
maximumRedeliveries must be a positive number, was: " + max);
+ }
+ LOG.info("After " + max + " failed redelivery attempts
Exchanges will be moved to deadLetterUri: " + recoverable.getDeadLetterUri());
+ deadLetterProcessor =
camelContext.getEndpoint(recoverable.getDeadLetterUri()).createProducer();
+ }
}
}
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RecoverableAggregationRepository.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RecoverableAggregationRepository.java?rev=932757&r1=932756&r2=932757&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RecoverableAggregationRepository.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RecoverableAggregationRepository.java
Sat Apr 10 16:33:11 2010
@@ -70,9 +70,54 @@ public interface RecoverableAggregationR
long getRecoveryIntervalInMillis();
/**
+ * Sets whether or not recovery is enabled
+ *
+ * @param useRecovery whether or not recovery is enabled
+ */
+ void setUseRecovery(boolean useRecovery);
+
+ /**
* Whether or not recovery is enabled or not
*
* @return <tt>true</tt> to use recovery, <tt>false</tt> otherwise.
*/
boolean isUseRecovery();
+
+ /**
+ * Sets an optional dead letter channel which exhausted recovered
{...@link Exchange}
+ * should be send to.
+ * <p/>
+ * By default this option is disabled
+ *
+ * @param deadLetterUri the uri of the dead letter channel
+ */
+ void setDeadLetterUri(String deadLetterUri);
+
+ /**
+ * Gets the dead letter channel
+ *
+ * @return the uri of the dead letter channel
+ */
+ String getDeadLetterUri();
+
+ /**
+ * Sets an optional limit of the number of redelivery attempt of recovered
{...@link Exchange}
+ * should be attempted, before its exhausted.
+ * <p/>
+ * When this limit is hit, then the {...@link Exchange} is moved to the
dead letter channel.
+ * <p/>
+ * By default this option is disabled
+ *
+ * @param maximumRedeliveries the maximum redeliveries
+ */
+ void setMaximumRedeliveries(int maximumRedeliveries);
+
+ /**
+ * Gets the maximum redelivery attempts to do before a recovered {...@link
Exchange} is doomed
+ * as exhausted and moved to the dead letter channel.
+ *
+ * @return the maximum redeliveries
+ */
+ int getMaximumRedeliveries();
+
}
Modified:
camel/trunk/components/camel-hawtdb/src/main/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepository.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-hawtdb/src/main/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepository.java?rev=932757&r1=932756&r2=932757&view=diff
==============================================================================
---
camel/trunk/components/camel-hawtdb/src/main/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepository.java
(original)
+++
camel/trunk/components/camel-hawtdb/src/main/java/org/apache/camel/component/hawtdb/HawtDBAggregationRepository.java
Sat Apr 10 16:33:11 2010
@@ -52,6 +52,8 @@ public class HawtDBAggregationRepository
private HawtDBCamelMarshaller<K> marshaller = new
HawtDBCamelMarshaller<K>();
private long recoveryInterval = 5000;
private boolean useRecovery = true;
+ private int maximumRedeliveries;
+ private String deadLetterUri;
/**
* Creates an aggregation repository
@@ -364,6 +366,22 @@ public class HawtDBAggregationRepository
this.useRecovery = useRecovery;
}
+ public int getMaximumRedeliveries() {
+ return maximumRedeliveries;
+ }
+
+ public void setMaximumRedeliveries(int maximumRedeliveries) {
+ this.maximumRedeliveries = maximumRedeliveries;
+ }
+
+ public String getDeadLetterUri() {
+ return deadLetterUri;
+ }
+
+ public void setDeadLetterUri(String deadLetterUri) {
+ this.deadLetterUri = deadLetterUri;
+ }
+
@Override
protected void doStart() throws Exception {
// either we have a HawtDB configured or we use a provided fileName
Copied:
camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregateRecoverDeadLetterChannelTest.java
(from r932710,
camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregateRecoverTest.java)
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregateRecoverDeadLetterChannelTest.java?p2=camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregateRecoverDeadLetterChannelTest.java&p1=camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregateRecoverTest.java&r1=932710&r2=932757&rev=932757&view=diff
==============================================================================
---
camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregateRecoverTest.java
(original)
+++
camel/trunk/components/camel-hawtdb/src/test/java/org/apache/camel/component/hawtdb/HawtDBAggregateRecoverDeadLetterChannelTest.java
Sat Apr 10 16:33:11 2010
@@ -17,18 +17,15 @@
package org.apache.camel.component.hawtdb;
import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicInteger;
import org.apache.camel.Exchange;
-import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.processor.aggregate.AggregationStrategy;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
-public class HawtDBAggregateRecoverTest extends CamelTestSupport {
+public class HawtDBAggregateRecoverDeadLetterChannelTest extends
CamelTestSupport {
- private static AtomicInteger counter = new AtomicInteger(0);
private HawtDBAggregationRepository<String> repo;
@Override
@@ -37,20 +34,24 @@ public class HawtDBAggregateRecoverTest
repo = new HawtDBAggregationRepository<String>("repo1",
"target/data/hawtdb.dat");
// enable recovery
repo.setUseRecovery(true);
+ // exhaust after at most 3 attempts
+ repo.setMaximumRedeliveries(3);
+ // and move to this dead letter channel
+ repo.setDeadLetterUri("mock:dead");
// check faster
repo.setRecoveryInterval(500, TimeUnit.MILLISECONDS);
+
super.setUp();
}
@Test
- public void testHawtDBAggregateRecover() throws Exception {
- // should fail the first 2 times and then recover
- getMockEndpoint("mock:aggregated").expectedMessageCount(3);
- getMockEndpoint("mock:result").expectedBodiesReceived("ABCDE");
- // should be marked as redelivered
-
getMockEndpoint("mock:result").message(0).header(Exchange.REDELIVERED).isEqualTo(Boolean.TRUE);
- // on the 2nd redelivery attempt we success
-
getMockEndpoint("mock:result").message(0).header(Exchange.REDELIVERY_COUNTER).isEqualTo(2);
+ public void testHawtDBAggregateRecoverDeadLetterChannel() throws Exception
{
+ // should fail all times
+ getMockEndpoint("mock:result").expectedMessageCount(0);
+ getMockEndpoint("mock:aggregated").expectedMessageCount(4);
+ getMockEndpoint("mock:dead").expectedBodiesReceived("ABCDE");
+
getMockEndpoint("mock:dead").message(0).header(Exchange.REDELIVERED).isEqualTo(Boolean.TRUE);
+
getMockEndpoint("mock:dead").message(0).header(Exchange.REDELIVERY_COUNTER).isEqualTo(4);
template.sendBodyAndHeader("direct:start", "A", "id", 123);
template.sendBodyAndHeader("direct:start", "B", "id", 123);
@@ -71,16 +72,7 @@ public class HawtDBAggregateRecoverTest
.completionSize(5).aggregationRepository(repo)
.log("aggregated exchange id ${exchangeId} with
${body}")
.to("mock:aggregated")
- .delay(1000)
- // simulate errors the first two times
- .process(new Processor() {
- public void process(Exchange exchange) throws
Exception {
- int count = counter.incrementAndGet();
- if (count <= 2) {
- throw new IllegalArgumentException("Damn");
- }
- }
- })
+ .throwException(new IllegalArgumentException("Damn"))
.to("mock:result")
.end();
}