使用WILDFLY中的分布式緩存INFISHPAN
項目部署的應用服務器:WILDFLY- 通過http://127.0.0.1:9991/console/App.html#infinispan添加CACHE
<cache-container name="tickets" default-cache="default" jndi-name="java:jboss/infinispan/tickets">
<local-cache name="default" batching="true">
<locking isolation="REPEATABLE_READ"/>
</local-cache>
</cache-container>
- pom.xml添加依賴包
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-client-hotrod</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jgroups</groupId>
<artifactId>jgroups</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-spring</artifactId>
<version>6.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-jcache</artifactId>
<version>6.0.2.Final</version>
</dependency>
- 添加攔截器,WEB-INF/beans.xml
<?xml version="1.0"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://jboss.org/schema/cdi/beans_1_0.xsd">
<interceptors>
<class>org.infinispan.jcache.annotation.CacheResultInterceptor</class>
<class>org.infinispan.jcache.annotation.CachePutInterceptor</class>
<class>org.infinispan.jcache.annotation.CacheRemoveEntryInterceptor</class>
<class>org.infinispan.jcache.annotation.CacheRemoveAllInterceptor</class>
</interceptors>
</beans>
- 添加項目的全局依賴,WEB-INF/jboss-deployment-structure.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<dependencies>
<module name="org.jboss.xnio" />
<module name="org.infinispan" export="true"/>
<module name="org.infinispan.commons" export="true"/>
<module name="org.infinispan.client.hotrod" export="true"/>
</dependencies>
</deployment>
</jboss-deployment-structure>
- 在CDI BEAN中使用CACHE
package com.paul.myejb;
import javax.annotation.Resource;
import javax.cache.annotation.CacheResult;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import org.infinispan.Cache;
import org.infinispan.manager.EmbeddedCacheManager;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor;
/**
* Session Bean implementation class HelloWorldBean
*/
@Stateless
//@Local(HelloWorld.class)
@Remote(HelloWorld.class)
@Interceptors(SpringBeanAutowiringInterceptor.class)
//@RolesAllowed({Roles.ADMIN})
public class HelloWorldBean implements HelloWorld {
@Resource(lookup = "java:jboss/infinispan/tickets")
private EmbeddedCacheManager container;
/**
* Default constructor.
*/
public HelloWorldBean() {
}
// @Transactional
// @Cacheable(value = "books", key = "#name")
@CacheResult
public String sayHello(String name) {
System.out.println("NO CACHE
");
String result = "Hello " + name + ", I am HelloWorldBean.";
Cache<String, String> cache = this.container.getCache();
cache.put(name, result);
return result;
}
}
- 修改modules/system/layers/base/org/infinispan/client/hotrod/main/modules.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ JBoss, Home of Professional Open Source.
~ Copyright 2010, Red Hat, Inc., and individual contributors
~ as indicated by the @author tags. See the copyright.txt file in the
~ distribution for a full listing of individual contributors.
~
~ This is free software; you can redistribute it and/or modify it
~ under the terms of the GNU Lesser General Public License as
~ published by the Free Software Foundation; either version 2.1 of
~ the License, or (at your option) any later version.
~
~ This software is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
~ Lesser General Public License for more details.
~
~ You should have received a copy of the GNU Lesser General Public
~ License along with this software; if not, write to the Free
~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
~ 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-->
<module xmlns="urn:jboss:module:1.3" name="org.infinispan.client.hotrod">
<properties>
<property name="jboss.api" value="private"/>
</properties>
<resources>
<resource-root path="infinispan-client-hotrod-6.0.2.Final.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<!--下面這一行注釋掉-->
<!--<module name="com.google.protobuf"/>-->
<module name="org.apache.commons.pool"/>
<module name="org.infinispan.commons"/>
<module name="org.infinispan.query.dsl"/>
<module name="org.jboss.logging"/>
</dependencies>
</module>
-
添加依賴的SPRING BEAN<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd">
<cache:annotation-driven />
<bean id="cacheManager"
class="org.infinispan.spring.provider.ContainerCacheManagerFactoryBean">
<constructor-arg ref="cacheContainer" />
</bean>
<jee:jndi-lookup id="cacheContainer" jndi-name="java:jboss/infinispan/tickets" >
</jee:jndi-lookup>
<!-- <bean id="cacheContainer"
class="com.paul.myejb.common.util.cache.JndiSpringCacheManagerFactoryBean"
p:infinispanJNDI="java:jboss/infinispan/tickets" /> -->
</beans>
-
使用CACHEpackage com.paul.myejb.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
@Component
public class MySpringBean {
@Autowired
private CacheManager cacheManager;
@Cacheable(value = "my-local-cache", key = "#name")
public String sayHello(String name)
{
System.out.println("MySpringBean NO CACHE
");
String result = "Hi " + name + ", I am Spring!";
org.springframework.cache.Cache springCache = this.cacheManager.getCache("my-local-cache");
System.out.println(springCache.get(name) == null ? "null" : springCache.get(name).get());
springCache.put(name, result);
return result;
}
}
posted on 2015-02-23 13:40 paulwong 閱讀(1005) 評論(0) 編輯 收藏 所屬分類: 分布式 、性能優化 、JBOSS 、緩存