paulwong

          My Links

          Blog Stats

          常用鏈接

          留言簿(67)

          隨筆分類(1389)

          隨筆檔案(1147)

          文章分類(7)

          文章檔案(10)

          相冊(cè)

          收藏夾(2)

          AI

          Develop

          E-BOOK

          Other

          養(yǎng)生

          微服務(wù)

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          60天內(nèi)閱讀排行

          使用WILDFLY中的分布式緩存INFISHPAN

          項(xiàng)目部署的應(yīng)用服務(wù)器:WILDFLY
          1. 通過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>

          2. 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>

          3. 添加攔截器,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>

          4. 添加項(xiàng)目的全局依賴,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>

          5. 在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;
                }

            }


          6. 修改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版本
          1. 添加依賴的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>

          2. 使用CACHE
            package 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) 評(píng)論(0)  編輯  收藏 所屬分類: 分布式 、性能優(yōu)化 、JBOSS 、緩存

          主站蜘蛛池模板: 措勤县| 施秉县| 迭部县| 濮阳县| 吉水县| 德清县| 阿荣旗| 通州市| 无棣县| 陇南市| 东乌| 文安县| 郸城县| 桂林市| 西林县| 公安县| 罗源县| 公主岭市| 沂南县| 通州市| 安国市| 和硕县| 兴业县| 武清区| 达孜县| 兴化市| 正安县| 江源县| 革吉县| 兴隆县| 修武县| 泾川县| 门头沟区| 新化县| 道孚县| 弥渡县| 孟村| 墨竹工卡县| 沛县| 招远市| 广丰县|