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 2012/05/29 17:46:26 UTC
svn commit: r1343788 - in /camel/trunk/components/camel-aws/src:
main/java/org/apache/camel/component/aws/sqs/
test/java/org/apache/camel/component/aws/sqs/
Author: davsclaus
Date: Tue May 29 15:46:25 2012
New Revision: 1343788
URL: http://svn.apache.org/viewvc?rev=1343788&view=rev
Log:
CAMEL-5306: Support for sending ChangeMessageVisibility calls while processing SQS messages. Thanks to Alex Hutter for the patch.
Added:
camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/sqs/SqsDoesNotExtendMessageVisibilityTest.java (with props)
camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/sqs/SqsExtendMessageVisibilityTest.java (with props)
Modified:
camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/sqs/SqsConfiguration.java
camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/sqs/SqsConsumer.java
camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/sqs/AmazonSQSClientMock.java
Modified: camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/sqs/SqsConfiguration.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/sqs/SqsConfiguration.java?rev=1343788&r1=1343787&r2=1343788&view=diff
==============================================================================
--- camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/sqs/SqsConfiguration.java (original)
+++ camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/sqs/SqsConfiguration.java Tue May 29 15:46:25 2012
@@ -38,6 +38,7 @@ public class SqsConfiguration {
private Integer visibilityTimeout;
private Collection<String> attributeNames;
private Integer defaultVisibilityTimeout;
+ private Boolean extendMessageVisibility = Boolean.FALSE;
// producer properties
private Integer delaySeconds;
@@ -151,6 +152,14 @@ public class SqsConfiguration {
this.policy = policy;
}
+ public boolean isExtendMessageVisibility() {
+ return this.extendMessageVisibility;
+ }
+
+ public void setExtendMessageVisibility(Boolean extendMessageVisibility) {
+ this.extendMessageVisibility = extendMessageVisibility;
+ }
+
@Override
public String toString() {
return "SqsConfiguration[queueName=" + queueName
@@ -165,6 +174,7 @@ public class SqsConfiguration {
+ ", messageRetentionPeriod=" + messageRetentionPeriod
+ ", delaySeconds=" + delaySeconds
+ ", policy=" + policy
+ + ", extendMessageVisibility=" + extendMessageVisibility
+ "]";
}
-}
\ No newline at end of file
+}
Modified: camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/sqs/SqsConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/sqs/SqsConsumer.java?rev=1343788&r1=1343787&r2=1343788&view=diff
==============================================================================
--- camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/sqs/SqsConsumer.java (original)
+++ camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/sqs/SqsConsumer.java Tue May 29 15:46:25 2012
@@ -19,11 +19,17 @@ package org.apache.camel.component.aws.s
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
import com.amazonaws.AmazonClientException;
import com.amazonaws.services.sqs.AmazonSQSClient;
+import com.amazonaws.services.sqs.model.ChangeMessageVisibilityRequest;
import com.amazonaws.services.sqs.model.DeleteMessageRequest;
import com.amazonaws.services.sqs.model.Message;
+import com.amazonaws.services.sqs.model.MessageNotInflightException;
+import com.amazonaws.services.sqs.model.ReceiptHandleIsInvalidException;
import com.amazonaws.services.sqs.model.ReceiveMessageRequest;
import com.amazonaws.services.sqs.model.ReceiveMessageResult;
@@ -47,6 +53,7 @@ import org.slf4j.LoggerFactory;
public class SqsConsumer extends ScheduledBatchPollingConsumer {
private static final transient Logger LOG = LoggerFactory.getLogger(SqsConsumer.class);
+ private ScheduledExecutorService scheduledExecutor;
public SqsConsumer(SqsEndpoint endpoint, Processor processor) throws NoFactoryAvailableException {
super(endpoint, processor);
@@ -115,9 +122,31 @@ public class SqsConsumer extends Schedul
}
});
- LOG.trace("Processing exchange [{}]...", exchange);
- getProcessor().process(exchange);
+ // schedule task to extend visibility if enabled
+ ScheduledFuture<?> scheduledFuture = null;
+ Integer visibilityTimeout = getConfiguration().getVisibilityTimeout();
+ if (scheduledExecutor != null && visibilityTimeout != null && (visibilityTimeout.intValue() / 2) > 0) {
+ int delay = visibilityTimeout.intValue() / 2;
+ int period = visibilityTimeout.intValue();
+ LOG.debug("Scheduled TimeoutExtender task to start after {} delay, and run with {} period (seconds) to extend exchangeId: {}",
+ new Object[]{delay, period, exchange.getExchangeId()});
+ scheduledFuture = this.scheduledExecutor.scheduleAtFixedRate(
+ new TimeoutExtender(exchange, visibilityTimeout), delay, period, TimeUnit.SECONDS);
+ }
+
+ LOG.trace("Processing exchange [{}]...", exchange);
+ try {
+ // This blocks while message is consumed.
+ getProcessor().process(exchange);
+ } finally {
+ LOG.trace("Processing exchange [{}] done.", exchange);
+ // cancel task as we are done
+ if (scheduledFuture != null) {
+ LOG.trace("Processing done so cancelling TimeoutExtender task for exchangeId: {}", exchange.getExchangeId());
+ scheduledFuture.cancel(true);
+ }
+ }
}
return total;
@@ -141,8 +170,7 @@ public class SqsConsumer extends Schedul
LOG.trace("Message deleted");
}
} catch (AmazonClientException e) {
- LOG.warn("Error occurred during deleting message", e);
- exchange.setException(e);
+ getExceptionHandler().handleException("Error occurred during deleting message.", e);
}
}
@@ -181,4 +209,52 @@ public class SqsConsumer extends Schedul
public String toString() {
return "SqsConsumer[" + URISupport.sanitizeUri(getEndpoint().getEndpointUri()) + "]";
}
-}
+
+ @Override
+ protected void doStart() throws Exception {
+ super.doStart();
+ if (getConfiguration().isExtendMessageVisibility() && scheduledExecutor == null) {
+ this.scheduledExecutor = getEndpoint().getCamelContext().getExecutorServiceManager().newSingleThreadScheduledExecutor(this, "SqsTimeoutExtender");
+ }
+ }
+
+ @Override
+ protected void doShutdown() throws Exception {
+ super.doShutdown();
+ if (scheduledExecutor != null) {
+ getEndpoint().getCamelContext().getExecutorServiceManager().shutdownNow(scheduledExecutor);
+ scheduledExecutor = null;
+ }
+ }
+
+ private class TimeoutExtender implements Runnable {
+
+ private final Exchange exchange;
+ private final int repeatSeconds;
+
+ public TimeoutExtender(Exchange exchange, int repeatSeconds) {
+ this.exchange = exchange;
+ this.repeatSeconds = repeatSeconds;
+ }
+
+ @Override
+ public void run() {
+ ChangeMessageVisibilityRequest request = new ChangeMessageVisibilityRequest(getQueueUrl(),
+ exchange.getIn().getHeader(SqsConstants.RECEIPT_HANDLE, String.class), repeatSeconds);
+
+ try {
+ LOG.trace("Extending visibility window by {} seconds for exchange {}", this.repeatSeconds, this.exchange);
+ getEndpoint().getClient().changeMessageVisibility(request);
+ LOG.debug("Extended visibility window by {} seconds for exchange {}", this.repeatSeconds, this.exchange);
+ } catch (ReceiptHandleIsInvalidException e) {
+ // Ignore.
+ } catch (MessageNotInflightException e) {
+ // Ignore.
+ } catch (Exception e) {
+ LOG.warn("Extending visibility window failed for exchange " + exchange
+ + ". Will not attempt to extend visibility further. This exception will be ignored.", e);
+ }
+ }
+ }
+
+}
\ No newline at end of file
Modified: camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/sqs/AmazonSQSClientMock.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/sqs/AmazonSQSClientMock.java?rev=1343788&r1=1343787&r2=1343788&view=diff
==============================================================================
--- camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/sqs/AmazonSQSClientMock.java (original)
+++ camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/sqs/AmazonSQSClientMock.java Tue May 29 15:46:25 2012
@@ -27,6 +27,7 @@ import com.amazonaws.AmazonClientExcepti
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.sqs.AmazonSQSClient;
+import com.amazonaws.services.sqs.model.ChangeMessageVisibilityRequest;
import com.amazonaws.services.sqs.model.CreateQueueRequest;
import com.amazonaws.services.sqs.model.CreateQueueResult;
import com.amazonaws.services.sqs.model.DeleteMessageRequest;
@@ -41,6 +42,7 @@ public class AmazonSQSClientMock extends
List<Message> messages = new ArrayList<Message>();
Map<String, Map<String, String>> queueAttributes = new HashMap<String, Map<String, String>>();
+ List<ChangeMessageVisibilityRequest> changeMessageVisibilityRequests = new ArrayList<ChangeMessageVisibilityRequest>();
public AmazonSQSClientMock() {
super(new BasicAWSCredentials("myAccessKey", "mySecretKey"));
@@ -106,4 +108,9 @@ public class AmazonSQSClientMock extends
}
}
}
+
+ @Override
+ public void changeMessageVisibility(ChangeMessageVisibilityRequest changeMessageVisibilityRequest) throws AmazonServiceException, AmazonClientException {
+ this.changeMessageVisibilityRequests.add(changeMessageVisibilityRequest);
+ }
}
\ No newline at end of file
Added: camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/sqs/SqsDoesNotExtendMessageVisibilityTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/sqs/SqsDoesNotExtendMessageVisibilityTest.java?rev=1343788&view=auto
==============================================================================
--- camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/sqs/SqsDoesNotExtendMessageVisibilityTest.java (added)
+++ camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/sqs/SqsDoesNotExtendMessageVisibilityTest.java Tue May 29 15:46:25 2012
@@ -0,0 +1,80 @@
+/**
+ * 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.aws.sqs;
+
+import com.amazonaws.services.sqs.model.Message;
+import org.apache.camel.EndpointInject;
+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;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class SqsDoesNotExtendMessageVisibilityTest extends CamelTestSupport {
+
+ private static final int TIMEOUT = 4; // 4 seconds.
+ private static final String RECEIPT_HANDLE = "0NNAq8PwvXsyZkR6yu4nQ07FGxNmOBWi5";
+
+ @EndpointInject(uri = "mock:result")
+ private MockEndpoint mock;
+
+ private AmazonSQSClientMock clientMock;
+
+ @Test
+ public void defaultsToDisabled() throws Exception {
+ this.mock.expectedMessageCount(1);
+ this.mock.whenAnyExchangeReceived(new Processor() {
+ @Override
+ public void process(Exchange exchange) throws Exception {
+ // Simulate message that takes a while to receive.
+ Thread.sleep(TIMEOUT * 1500L); // 150% of TIMEOUT.
+ }
+ });
+
+ Message message = new Message();
+ message.setBody("Message 1");
+ message.setMD5OfBody("6a1559560f67c5e7a7d5d838bf0272ee");
+ message.setMessageId("f6fb6f99-5eb2-4be4-9b15-144774141458");
+ message.setReceiptHandle(RECEIPT_HANDLE);
+ this.clientMock.messages.add(message);
+
+ assertMockEndpointsSatisfied(); // Wait for message to arrive.
+ assertTrue("Expected no changeMessageVisibility requests.", this.clientMock.changeMessageVisibilityRequests.size() == 0);
+ }
+
+ @Override
+ protected JndiRegistry createRegistry() throws Exception {
+ JndiRegistry registry = super.createRegistry();
+ this.clientMock = new AmazonSQSClientMock();
+ registry.bind("amazonSQSClient", this.clientMock);
+ return registry;
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("aws-sqs://MyQueue?amazonSQSClient=#amazonSQSClient")
+ .to("mock:result");
+ }
+ };
+ }
+
+}
Propchange: camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/sqs/SqsDoesNotExtendMessageVisibilityTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/sqs/SqsDoesNotExtendMessageVisibilityTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/sqs/SqsExtendMessageVisibilityTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/sqs/SqsExtendMessageVisibilityTest.java?rev=1343788&view=auto
==============================================================================
--- camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/sqs/SqsExtendMessageVisibilityTest.java (added)
+++ camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/sqs/SqsExtendMessageVisibilityTest.java Tue May 29 15:46:25 2012
@@ -0,0 +1,87 @@
+/**
+ * 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.aws.sqs;
+
+import com.amazonaws.services.sqs.model.ChangeMessageVisibilityRequest;
+import com.amazonaws.services.sqs.model.Message;
+import org.apache.camel.EndpointInject;
+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;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class SqsExtendMessageVisibilityTest extends CamelTestSupport {
+
+ private static final int TIMEOUT = 4; // 4 seconds.
+ private static final String RECEIPT_HANDLE = "0NNAq8PwvXsyZkR6yu4nQ07FGxNmOBWi5";
+
+ @EndpointInject(uri = "mock:result")
+ private MockEndpoint mock;
+
+ private AmazonSQSClientMock clientMock;
+
+ @Test
+ public void longReceiveExtendsMessageVisibility() throws Exception {
+ this.mock.expectedMessageCount(1);
+ this.mock.whenAnyExchangeReceived(new Processor() {
+ @Override
+ public void process(Exchange exchange) throws Exception {
+ // Simulate message that takes a while to receive.
+ Thread.sleep(TIMEOUT * 1500L); // 150% of TIMEOUT.
+ }
+ });
+
+ Message message = new Message();
+ message.setBody("Message 1");
+ message.setMD5OfBody("6a1559560f67c5e7a7d5d838bf0272ee");
+ message.setMessageId("f6fb6f99-5eb2-4be4-9b15-144774141458");
+ message.setReceiptHandle(RECEIPT_HANDLE);
+ this.clientMock.messages.add(message);
+
+ assertMockEndpointsSatisfied(); // Wait for message to arrive.
+
+ assertTrue("Expected at least one changeMessageVisibility request.", this.clientMock.changeMessageVisibilityRequests.size() >= 1);
+ for (ChangeMessageVisibilityRequest req : this.clientMock.changeMessageVisibilityRequests) {
+ assertEquals("https://queue.amazonaws.com/541925086079/MyQueue", req.getQueueUrl());
+ assertEquals(RECEIPT_HANDLE, req.getReceiptHandle());
+ assertEquals(new Integer(4), req.getVisibilityTimeout());
+ }
+ }
+
+ @Override
+ protected JndiRegistry createRegistry() throws Exception {
+ JndiRegistry registry = super.createRegistry();
+ this.clientMock = new AmazonSQSClientMock();
+ registry.bind("amazonSQSClient", this.clientMock);
+ return registry;
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("aws-sqs://MyQueue?amazonSQSClient=#amazonSQSClient&extendMessageVisibility=true&visibilityTimeout=" + TIMEOUT)
+ .to("mock:result");
+ }
+ };
+ }
+
+}
Propchange: camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/sqs/SqsExtendMessageVisibilityTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/sqs/SqsExtendMessageVisibilityTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date