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 2011/01/18 14:40:31 UTC
svn commit: r1060366 - in /camel/trunk:
camel-core/src/main/java/org/apache/camel/processor/idempotent/
components/camel-spring/src/test/java/org/apache/camel/spring/processor/
components/camel-spring/src/test/resources/org/apache/camel/spring/processor/
Author: davsclaus
Date: Tue Jan 18 13:40:31 2011
New Revision: 1060366
URL: http://svn.apache.org/viewvc?rev=1060366&view=rev
Log:
Added spring XML idempotent consumer test and example.
Added:
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringIdempotentConsumerTest.java
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringIdempotentConsumerTest.xml
- copied, changed from r1060292, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/choice.xml
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/idempotent/MemoryIdempotentRepository.java
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/idempotent/MemoryIdempotentRepository.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/idempotent/MemoryIdempotentRepository.java?rev=1060366&r1=1060365&r2=1060366&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/idempotent/MemoryIdempotentRepository.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/idempotent/MemoryIdempotentRepository.java Tue Jan 18 13:40:31 2011
@@ -21,7 +21,6 @@ import java.util.Map;
import org.apache.camel.impl.ServiceSupport;
import org.apache.camel.spi.IdempotentRepository;
import org.apache.camel.util.LRUCache;
-
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;
@@ -36,7 +35,8 @@ import org.springframework.jmx.export.an
*/
@ManagedResource("MemoryIdempotentRepository")
public class MemoryIdempotentRepository extends ServiceSupport implements IdempotentRepository<String> {
- private final Map<String, Object> cache;
+ private Map<String, Object> cache;
+ private int cacheSize;
public MemoryIdempotentRepository() {
this.cache = new LRUCache<String, Object>(1000);
@@ -116,8 +116,15 @@ public class MemoryIdempotentRepository
return cache.size();
}
+ public void setCacheSize(int cacheSize) {
+ this.cacheSize = cacheSize;
+ }
+
@Override
protected void doStart() throws Exception {
+ if (cacheSize > 0) {
+ cache = new LRUCache<String, Object>(cacheSize);
+ }
}
@Override
Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringIdempotentConsumerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringIdempotentConsumerTest.java?rev=1060366&view=auto
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringIdempotentConsumerTest.java (added)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringIdempotentConsumerTest.java Tue Jan 18 13:40:31 2011
@@ -0,0 +1,62 @@
+/**
+ * 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.processor;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spring.SpringTestSupport;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @version $Revision$
+ */
+public class SpringIdempotentConsumerTest extends SpringTestSupport {
+
+ @Override
+ protected AbstractXmlApplicationContext createApplicationContext() {
+ return new ClassPathXmlApplicationContext("org/apache/camel/spring/processor/SpringIdempotentConsumerTest.xml");
+ }
+
+ public void testDuplicateMessagesAreFilteredOut() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("one", "two", "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("direct:start", new Processor() {
+ public void process(Exchange exchange) {
+ // now lets fire in a message
+ Message in = exchange.getIn();
+ in.setBody(body);
+ in.setHeader("messageId", messageId);
+ }
+ });
+ }
+
+}
Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringIdempotentConsumerTest.xml (from r1060292, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/choice.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringIdempotentConsumerTest.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringIdempotentConsumerTest.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/choice.xml&r1=1060292&r2=1060366&rev=1060366&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/choice.xml (original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringIdempotentConsumerTest.xml Tue Jan 18 13:40:31 2011
@@ -22,26 +22,21 @@
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
">
- <!-- START SNIPPET: example -->
- <camelContext xmlns="http://camel.apache.org/schema/spring">
- <route>
- <from uri="direct:start"/>
- <choice>
- <when>
- <xpath>$foo = 'bar'</xpath>
- <to uri="mock:x"/>
- </when>
- <when>
- <xpath>$foo = 'cheese'</xpath>
- <to uri="mock:y"/>
- </when>
- <otherwise>
- <to uri="mock:z"/>
- </otherwise>
- </choice>
- <to uri="mock:end"/>
- </route>
- </camelContext>
- <!-- END SNIPPET: example -->
+ <!-- repository for the idempotent consumer -->
+ <bean id="myRepo" class="org.apache.camel.processor.idempotent.MemoryIdempotentRepository"/>
+
+ <!-- START SNIPPET: e1 -->
+ <camelContext xmlns="http://camel.apache.org/schema/spring">
+ <route>
+ <from uri="direct:start"/>
+ <idempotentConsumer messageIdRepositoryRef="myRepo">
+ <!-- use the messageId header as key for identifying duplicate messages -->
+ <header>messageId</header>
+ <!-- if not a duplicate send it to this mock endpoint -->
+ <to uri="mock:result"/>
+ </idempotentConsumer>
+ </route>
+ </camelContext>
+ <!-- END SNIPPET: e1 -->
</beans>