Repository: camel Updated Branches: refs/heads/master 00bb43fe9 -> 34d92293b
camel-ehcache: add some example to configure ehcache via xml Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/34d92293 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/34d92293 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/34d92293 Branch: refs/heads/master Commit: 34d92293bc52fcf9cea9b6713695f09acd0d1c8c Parents: 00bb43f Author: lburgazzoli <lburgazz...@gmail.com> Authored: Mon Jan 9 11:06:23 2017 +0100 Committer: lburgazzoli <lburgazz...@gmail.com> Committed: Mon Jan 9 11:06:23 2017 +0100 ---------------------------------------------------------------------- components/camel-ehcache/pom.xml | 7 +- .../ehcache/EhcacheConfigurationTest.java | 37 +++++++--- .../EhcacheSpringConfigurationFactory.java | 61 ++++++++++++++++ .../ehcache/EhcacheSpringConfigurationTest.java | 77 ++++++++++++++++++++ .../resources/ehcache/ehcache-file-config.xml | 33 +++++++++ .../ehcache/EhcacheSpringConfigurationTest.xml | 41 +++++++++++ 6 files changed, 245 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/34d92293/components/camel-ehcache/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-ehcache/pom.xml b/components/camel-ehcache/pom.xml index 5f12c75..ebf66be 100644 --- a/components/camel-ehcache/pom.xml +++ b/components/camel-ehcache/pom.xml @@ -55,7 +55,12 @@ <groupId>org.apache.camel</groupId> <artifactId>camel-test</artifactId> <scope>test</scope> - </dependency> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-spring</artifactId> + <scope>test</scope> + </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> http://git-wip-us.apache.org/repos/asf/camel/blob/34d92293/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheConfigurationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheConfigurationTest.java b/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheConfigurationTest.java index f1e9df2..8a77fff 100644 --- a/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheConfigurationTest.java +++ b/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheConfigurationTest.java @@ -30,20 +30,18 @@ import org.ehcache.config.builders.ResourcePoolsBuilder; import org.ehcache.config.units.EntryUnit; import org.ehcache.config.units.MemoryUnit; import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class EhcacheConfigurationTest extends CamelTestSupport { - public static final Logger LOGGER = LoggerFactory.getLogger(EhcacheConfigurationTest.class); - - @EndpointInject(uri = "ehcache://myCacheConf?configuration=#myConf") + @EndpointInject(uri = "ehcache://myProgrammaticCacheConf?configuration=#myProgrammaticConfiguration") private EhcacheEndpoint ehcacheConf; + @EndpointInject(uri = "ehcache://myFileCacheConf?keyType=java.lang.String&valueType=java.lang.String&configUri=classpath:ehcache/ehcache-file-config.xml") + private EhcacheEndpoint ehcacheFileConf; @Override protected JndiRegistry createRegistry() throws Exception { JndiRegistry registry = super.createRegistry(); registry.bind( - "myConf", + "myProgrammaticConfiguration", CacheConfigurationBuilder.newCacheConfigurationBuilder( String.class, String.class, @@ -56,11 +54,13 @@ public class EhcacheConfigurationTest extends CamelTestSupport { return registry; } + // ***************************** + // Test + // ***************************** + @Test public void testProgrammaticConfiguration() throws Exception { - EhcacheManager manager = ehcacheConf.getManager(); - Cache<String, String> cache = manager.getCache("myCacheConf", String.class, String.class); - + Cache<String, String> cache = getCache(ehcacheConf, "myProgrammaticCacheConf"); ResourcePools pools = cache.getRuntimeConfiguration().getResourcePools(); SizedResourcePool h = pools.getPoolForResource(ResourceType.Core.HEAP); @@ -74,6 +74,21 @@ public class EhcacheConfigurationTest extends CamelTestSupport { assertEquals(MemoryUnit.MB, o.getUnit()); } + @Test + public void testFileConfiguration() throws Exception { + Cache<String, String> cache = getCache(ehcacheFileConf, "myFileCacheConf"); + ResourcePools pools = cache.getRuntimeConfiguration().getResourcePools(); + + SizedResourcePool h = pools.getPoolForResource(ResourceType.Core.HEAP); + assertNotNull(h); + assertEquals(150, h.getSize()); + assertEquals(EntryUnit.ENTRIES, h.getUnit()); + } + + protected Cache<String, String> getCache(EhcacheEndpoint endpoint, String cacheName) throws Exception { + return endpoint.getManager().getCache(cacheName, String.class, String.class); + } + // **************************** // Route // **************************** @@ -82,7 +97,9 @@ public class EhcacheConfigurationTest extends CamelTestSupport { protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() { - from("direct://start").to(ehcacheConf); + from("direct://start") + //.to(ehcacheConf) + .to(ehcacheFileConf); } }; } http://git-wip-us.apache.org/repos/asf/camel/blob/34d92293/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheSpringConfigurationFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheSpringConfigurationFactory.java b/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheSpringConfigurationFactory.java new file mode 100644 index 0000000..47bef1e --- /dev/null +++ b/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheSpringConfigurationFactory.java @@ -0,0 +1,61 @@ +/** + * 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.ehcache; + +import org.ehcache.config.CacheConfiguration; +import org.ehcache.config.builders.CacheConfigurationBuilder; +import org.ehcache.config.builders.ResourcePoolsBuilder; +import org.ehcache.config.units.EntryUnit; +import org.ehcache.config.units.MemoryUnit; +import org.springframework.beans.factory.config.AbstractFactoryBean; + +public class EhcacheSpringConfigurationFactory extends AbstractFactoryBean<CacheConfiguration> { + private Class<?> keyType = Object.class; + private Class<?> valueType = Object.class; + + public Class<?> getKeyType() { + return keyType; + } + + public void setKeyType(Class<?> keyType) { + this.keyType = keyType; + } + + public Class<?> getValueType() { + return valueType; + } + + public void setValueType(Class<?> valueType) { + this.valueType = valueType; + } + + @Override + public Class<?> getObjectType() { + return CacheConfiguration.class; + } + + @Override + protected CacheConfiguration createInstance() throws Exception { + return CacheConfigurationBuilder.newCacheConfigurationBuilder( + keyType, + valueType, + ResourcePoolsBuilder.newResourcePoolsBuilder() + .heap(100, EntryUnit.ENTRIES) + .offheap(1, MemoryUnit.MB)) + .build(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/34d92293/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheSpringConfigurationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheSpringConfigurationTest.java b/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheSpringConfigurationTest.java new file mode 100644 index 0000000..b76cf42 --- /dev/null +++ b/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheSpringConfigurationTest.java @@ -0,0 +1,77 @@ +/** + * 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.ehcache; + +import org.apache.camel.EndpointInject; +import org.apache.camel.test.spring.CamelSpringTestSupport; +import org.ehcache.Cache; +import org.ehcache.config.ResourcePools; +import org.ehcache.config.ResourceType; +import org.ehcache.config.SizedResourcePool; +import org.ehcache.config.units.EntryUnit; +import org.ehcache.config.units.MemoryUnit; +import org.junit.Test; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class EhcacheSpringConfigurationTest extends CamelSpringTestSupport { + + @EndpointInject(uri = "ehcache://myProgrammaticCacheConf?configuration=#myProgrammaticConfiguration") + private EhcacheEndpoint ehcacheConf; + @EndpointInject(uri = "ehcache://myFileCacheConf?keyType=java.lang.String&valueType=java.lang.String&configUri=classpath:ehcache/ehcache-file-config.xml") + private EhcacheEndpoint ehcacheFileConf; + + @Override + protected AbstractApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/component/ehcache/EhcacheSpringConfigurationTest.xml"); + } + + // ***************************** + // Test + // ***************************** + + @Test + public void testProgrammaticConfiguration() throws Exception { + Cache<String, String> cache = getCache(ehcacheConf, "myProgrammaticCacheConf"); + ResourcePools pools = cache.getRuntimeConfiguration().getResourcePools(); + + SizedResourcePool h = pools.getPoolForResource(ResourceType.Core.HEAP); + assertNotNull(h); + assertEquals(100, h.getSize()); + assertEquals(EntryUnit.ENTRIES, h.getUnit()); + + SizedResourcePool o = pools.getPoolForResource(ResourceType.Core.OFFHEAP); + assertNotNull(o); + assertEquals(1, o.getSize()); + assertEquals(MemoryUnit.MB, o.getUnit()); + } + + @Test + public void testFileConfiguration() throws Exception { + Cache<String, String> cache = getCache(ehcacheFileConf, "myFileCacheConf"); + ResourcePools pools = cache.getRuntimeConfiguration().getResourcePools(); + + SizedResourcePool h = pools.getPoolForResource(ResourceType.Core.HEAP); + assertNotNull(h); + assertEquals(150, h.getSize()); + assertEquals(EntryUnit.ENTRIES, h.getUnit()); + } + + protected Cache<String, String> getCache(EhcacheEndpoint endpoint, String cacheName) throws Exception { + return endpoint.getManager().getCache(cacheName, String.class, String.class); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/34d92293/components/camel-ehcache/src/test/resources/ehcache/ehcache-file-config.xml ---------------------------------------------------------------------- diff --git a/components/camel-ehcache/src/test/resources/ehcache/ehcache-file-config.xml b/components/camel-ehcache/src/test/resources/ehcache/ehcache-file-config.xml new file mode 100644 index 0000000..7af9a42 --- /dev/null +++ b/components/camel-ehcache/src/test/resources/ehcache/ehcache-file-config.xml @@ -0,0 +1,33 @@ +<!-- + ~ 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. + --> +<ehcache:config + xmlns:ehcache="http://www.ehcache.org/v3" + xmlns:jcache="http://www.ehcache.org/v3/jsr107"> + + <ehcache:cache alias="myFileCacheConf" uses-template="default-template"> + <ehcache:key-type>java.lang.String</ehcache:key-type> + <ehcache:value-type>java.lang.String</ehcache:value-type> + <ehcache:heap unit="entries">150</ehcache:heap> + </ehcache:cache> + + <ehcache:cache-template name="default-template"> + <ehcache:expiry> + <ehcache:none/> + </ehcache:expiry> + </ehcache:cache-template> + +</ehcache:config> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/34d92293/components/camel-ehcache/src/test/resources/org/apache/camel/component/ehcache/EhcacheSpringConfigurationTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-ehcache/src/test/resources/org/apache/camel/component/ehcache/EhcacheSpringConfigurationTest.xml b/components/camel-ehcache/src/test/resources/org/apache/camel/component/ehcache/EhcacheSpringConfigurationTest.xml new file mode 100644 index 0000000..8316113 --- /dev/null +++ b/components/camel-ehcache/src/test/resources/org/apache/camel/component/ehcache/EhcacheSpringConfigurationTest.xml @@ -0,0 +1,41 @@ +<?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="myProgrammaticConfiguration" class="org.apache.camel.component.ehcache.EhcacheSpringConfigurationFactory"> + <property name="keyType" value="java.lang.String"/> + <property name="valueType" value="java.lang.String"/> + </bean> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="direct:programmatic"/> + <to uri="ehcache://myProgrammaticCacheConf?configuration=#myProgrammaticConfiguration"/> + </route> + <route> + <from uri="direct:file"/> + <to uri="ehcache://myFileCacheConf?keyType=java.lang.String&valueType=java.lang.String&configUri=classpath:ehcache/ehcache-file-config.xml"/> + </route> + </camelContext> +</beans>