Repository: camel Updated Branches: refs/heads/camel-2.17.x 61e3d46c3 -> 52a3377ea
CAMEL-9883 : Add a SpringCache based idempotent repository Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a6b354a1 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a6b354a1 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a6b354a1 Branch: refs/heads/camel-2.17.x Commit: a6b354a1e15eda282c83e4b7d3d3be606f08e2a9 Parents: 61e3d46 Author: lburgazzoli <lburgazz...@gmail.com> Authored: Tue Apr 26 13:40:54 2016 +0200 Committer: lburgazzoli <lburgazz...@gmail.com> Committed: Tue Apr 26 13:47:24 2016 +0200 ---------------------------------------------------------------------- components/camel-infinispan/pom.xml | 25 ++++++ .../SpringInfinispanIdempotentRpoositoryIT.java | 46 +++++++++++ .../SpringInfinispanIdempotentRepositoryIT.xml | 52 ++++++++++++ components/camel-spring/pom.xml | 10 +++ .../SpringCacheIdempotentRepository.java | 85 ++++++++++++++++++++ .../idempotent/SpringCacheIdempotentTest.java | 47 +++++++++++ .../idempotent/SpringCacheIdempotentTest.xml | 44 ++++++++++ 7 files changed, 309 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/a6b354a1/components/camel-infinispan/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-infinispan/pom.xml b/components/camel-infinispan/pom.xml index ff4eb99..c7311f7 100644 --- a/components/camel-infinispan/pom.xml +++ b/components/camel-infinispan/pom.xml @@ -63,6 +63,11 @@ <scope>test</scope> </dependency> <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-spring</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-core</artifactId> <version>${infinispan-version}</version> @@ -70,6 +75,26 @@ <scope>test</scope> </dependency> <dependency> + <groupId>org.infinispan</groupId> + <artifactId>infinispan-spring4-remote</artifactId> + <version>${infinispan-version}</version> + <scope>test</scope> + <exclusions> + <exclusion> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + </exclusion> + <exclusion> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-slf4j-impl</artifactId> + </exclusion> + <exclusion> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-jcl</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> <groupId>org.infinispan.server</groupId> <artifactId>infinispan-server-testsuite</artifactId> <version>${infinispan-version}</version> http://git-wip-us.apache.org/repos/asf/camel/blob/a6b354a1/components/camel-infinispan/src/test/java/org/apache/camel/component/infinispan/processor/idempotent/SpringInfinispanIdempotentRpoositoryIT.java ---------------------------------------------------------------------- diff --git a/components/camel-infinispan/src/test/java/org/apache/camel/component/infinispan/processor/idempotent/SpringInfinispanIdempotentRpoositoryIT.java b/components/camel-infinispan/src/test/java/org/apache/camel/component/infinispan/processor/idempotent/SpringInfinispanIdempotentRpoositoryIT.java new file mode 100644 index 0000000..60ea8e3 --- /dev/null +++ b/components/camel-infinispan/src/test/java/org/apache/camel/component/infinispan/processor/idempotent/SpringInfinispanIdempotentRpoositoryIT.java @@ -0,0 +1,46 @@ +/** + * 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.infinispan.processor.idempotent; + +import java.util.UUID; + +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.spring.CamelSpringTestSupport; +import org.junit.Test; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + + +public class SpringInfinispanIdempotentRpoositoryIT extends CamelSpringTestSupport { + @Override + protected AbstractApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/component/infinispan/processor/idempotent/SpringInfinispanIdempotentRepositoryIT.xml"); + } + + @Test + public void testIdempotent() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + + String messageId = UUID.randomUUID().toString(); + for (int i = 0; i < 5; i++) { + template.sendBodyAndHeader("direct:start", UUID.randomUUID().toString(), "MessageId", messageId); + } + + mock.assertIsSatisfied(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a6b354a1/components/camel-infinispan/src/test/resources/org/apache/camel/component/infinispan/processor/idempotent/SpringInfinispanIdempotentRepositoryIT.xml ---------------------------------------------------------------------- diff --git a/components/camel-infinispan/src/test/resources/org/apache/camel/component/infinispan/processor/idempotent/SpringInfinispanIdempotentRepositoryIT.xml b/components/camel-infinispan/src/test/resources/org/apache/camel/component/infinispan/processor/idempotent/SpringInfinispanIdempotentRepositoryIT.xml new file mode 100644 index 0000000..98cfc39 --- /dev/null +++ b/components/camel-infinispan/src/test/resources/org/apache/camel/component/infinispan/processor/idempotent/SpringInfinispanIdempotentRepositoryIT.xml @@ -0,0 +1,52 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring + http://camel.apache.org/schema/spring/camel-spring.xsd"> + + <bean id="cacheManager" class="org.infinispan.spring.provider.SpringRemoteCacheManagerFactoryBean"> + <property + name="configurationProperties"> + <props> + <prop key="infinispan.client.hotrod.server_list">127.0.0.1:11222</prop> + <prop key="infinispan.client.hotrod.force_return_values">true</prop> + </props> + </property> + </bean> + + <bean id="repo" class="org.apache.camel.spring.processor.idempotent.SpringCacheIdempotentRepository"> + <constructor-arg ref="cacheManager"/> + <constructor-arg value="idempotent"/> + </bean> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <route id="infinispan-remote"> + <from uri="direct:start" /> + <idempotentConsumer messageIdRepositoryRef="repo" skipDuplicate="true"> + <header>MessageId</header> + <to uri="log:org.apache.camel.component.infinispan.processor.idempotent?level=INFO&showAll=true&multiline=true" /> + <to uri="mock:result"/> + </idempotentConsumer> + </route> + </camelContext> + +</beans> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/a6b354a1/components/camel-spring/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-spring/pom.xml b/components/camel-spring/pom.xml index 5c2cee2..764a045 100644 --- a/components/camel-spring/pom.xml +++ b/components/camel-spring/pom.xml @@ -150,6 +150,11 @@ </dependency> <dependency> <groupId>org.springframework</groupId> + <artifactId>spring-context-support</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <scope>test</scope> </dependency> @@ -185,6 +190,11 @@ <version>${xmlunit-version}</version> <scope>test</scope> </dependency> + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + <scope>test</scope> + </dependency> <!-- for testing Spring AOP at class level --> <dependency> http://git-wip-us.apache.org/repos/asf/camel/blob/a6b354a1/components/camel-spring/src/main/java/org/apache/camel/spring/processor/idempotent/SpringCacheIdempotentRepository.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/processor/idempotent/SpringCacheIdempotentRepository.java b/components/camel-spring/src/main/java/org/apache/camel/spring/processor/idempotent/SpringCacheIdempotentRepository.java new file mode 100644 index 0000000..9e481a3 --- /dev/null +++ b/components/camel-spring/src/main/java/org/apache/camel/spring/processor/idempotent/SpringCacheIdempotentRepository.java @@ -0,0 +1,85 @@ +/** + * 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.idempotent; + +import org.apache.camel.api.management.ManagedAttribute; +import org.apache.camel.api.management.ManagedOperation; +import org.apache.camel.api.management.ManagedResource; +import org.apache.camel.spi.IdempotentRepository; +import org.apache.camel.support.ServiceSupport; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; + +@ManagedResource(description = "SpringCache based message id repository") +public class SpringCacheIdempotentRepository extends ServiceSupport implements IdempotentRepository<Object> { + private final CacheManager manager; + private final String cacheName; + private Cache cache; + + public SpringCacheIdempotentRepository(CacheManager manager, String cacheName) { + this.manager = manager; + this.cacheName = cacheName; + this.cache = null; + } + + @Override + @ManagedOperation(description = "Adds the key to the store") + public boolean add(Object key) { + return cache.putIfAbsent(key, true) == null; + } + + @Override + @ManagedOperation(description = "Does the store contain the given key") + public boolean contains(Object key) { + return cache.get(key) != null; + } + + @Override + @ManagedOperation(description = "Remove the key from the store") + public boolean remove(Object key) { + cache.evict(key); + return true; + } + + @Override + @ManagedOperation(description = "Clear the store") + public void clear() { + cache.clear(); + } + + @ManagedAttribute(description = "The processor name") + public String getCacheName() { + return cacheName; + } + + @Override + public boolean confirm(Object key) { + return true; + } + + @Override + protected void doStart() throws Exception { + if (cache == null) { + cache = manager.getCache(cacheName); + } + } + + @Override + protected void doStop() throws Exception { + cache = null; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/a6b354a1/components/camel-spring/src/test/java/org/apache/camel/spring/processor/idempotent/SpringCacheIdempotentTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/processor/idempotent/SpringCacheIdempotentTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/processor/idempotent/SpringCacheIdempotentTest.java new file mode 100644 index 0000000..279f69d --- /dev/null +++ b/components/camel-spring/src/test/java/org/apache/camel/spring/processor/idempotent/SpringCacheIdempotentTest.java @@ -0,0 +1,47 @@ +/** + * 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.idempotent; + +import java.util.UUID; + +import org.apache.camel.CamelContext; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.Test; + +import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext; + +public class SpringCacheIdempotentTest extends ContextTestSupport { + + protected CamelContext createCamelContext() throws Exception { + return createSpringCamelContext(this, "org/apache/camel/spring/processor/idempotent/SpringCacheIdempotentTest.xml"); + } + + @Test + public void testIdempotent() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + + String messageId = UUID.randomUUID().toString(); + for (int i = 0; i < 5; i++) { + template.sendBodyAndHeader("direct:start", UUID.randomUUID().toString(), "MessageId", messageId); + } + + mock.assertIsSatisfied(); + } +} + http://git-wip-us.apache.org/repos/asf/camel/blob/a6b354a1/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/idempotent/SpringCacheIdempotentTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/idempotent/SpringCacheIdempotentTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/idempotent/SpringCacheIdempotentTest.xml new file mode 100644 index 0000000..7be6c3e --- /dev/null +++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/idempotent/SpringCacheIdempotentTest.xml @@ -0,0 +1,44 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <!-- START SNIPPET: example --> + <bean id="repo" class="org.apache.camel.spring.processor.idempotent.SpringCacheIdempotentRepository"> + <constructor-arg> + <bean class="org.springframework.cache.guava.GuavaCacheManager"/> + </constructor-arg> + <constructor-arg value="idempotent"/> + </bean> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <route id="idempotent-cache"> + <from uri="direct:start" /> + <idempotentConsumer messageIdRepositoryRef="repo" skipDuplicate="true"> + <header>MessageId</header> + <to uri="log:org.apache.camel.spring.processor.idempotent?level=INFO&showAll=true&multiline=true" /> + <to uri="mock:result"/> + </idempotentConsumer> + </route> + </camelContext> + <!-- END SNIPPET: example --> +</beans>