springMVC整合memcached

非原创,文章转自:http://www.cnblogs.com/xiaoqingxin/p/4132391.html

 文章我就不全copy了,摘抄下我关注的部分,想看原文的请移步上面文章链接

  applicationContext.xml配置

<?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:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.1.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">


<!-- 客户端:java_memcached-release_2.6.3 -->
<bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize" lazy-init="false" destroy-method="shutDown">
<constructor-arg>
<value>memcachedPool</value>
</constructor-arg>
<!-- 可以设置多个memcached服务器 -->
<property name="servers">
<list>
<value>127.0.0.1:11211</value>
</list>
</property>
<!-- 每个服务器初始连接数 -->
<property name="initConn">
<value>20</value>
</property>
<!-- 每个服务器最小连接数 -->
<property name="minConn">
<value>20</value>
</property>
<!-- 每个服务器最大连接数 -->
<property name="maxConn">
<value>1000</value>
</property>
<!-- 主线程睡眠时间 -->
<property name="maintSleep">
<value>30000</value>
</property>
<!-- TCP/Socket的参数,如果是true在写数据时不缓冲,立即发送出去参数 -->
<property name="nagle">
<value>false</value>
</property>
<!-- 连接超时/阻塞读取数据的超时间是 -->
<property name="socketTO">
<value>3000</value>
</property>
</bean>

<bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient" >
<constructor-arg>
<value>memcachedPool</value>
</constructor-arg>
</bean>

</beans>

在整合的时候遇到一个问题  [ERROR] attempting to get SockIO from uninitialized pool!

原因是:spring-memcached.xml中的 memcachedPool 的名字要和MemcachedUtils.java中new MemCachedClient("memcachedPool"); 的名字对应起来。

具体原因已经有人分析了:http://blog.csdn.net/maerdym/article/details/10297993,我直接引用原作者的。

Memecached JavaClient在使用前需初始化SockIOPool,该类只有一个protected的构造方法,因此外部需使用其提供的静态方法getInstance来获取SockIOPool实例,getInstance方法允许传入poolname来指明SockIOPool名称. SockIOPool本身只是作为SchoonerSockIOPool的代理类,SchoonerSockIOPool内维护了一个连接池Map,其中poolname作为key,Pool实例作为值.因此在使用Spring整合Memcacheds时,如果在Spring配置文件中指明了poolname,则在初始化MemecachedClient时,需要在其构造函数中指明poolname.,如果没有声明poolname,则MemechachedClient则或获取名为default的Pool实例.

如以下配置,必须在实例化MemechacedClient时传入poolname.否则将无法进行数据操作或数据操作无效。

Spring配置文件,该配置文件声明了SockIOPool,由于该类的构造方法为protected类型,无法直接访问,因此需要使用工厂方法getInstance()来获取其实例,注:此处的<constructor-arg>标签不是作为构造函数的参数,而是作为工厂方法getInstance()的参数,即指明poolname为memcache

    <bean id="memcache" class="com.whalin.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize" destroy-method="shutDown">  
        <constructor-arg>  
            <value>memcache</value>  
        </constructor-arg></span></strong></em></span>  
        <property name="servers">  
            <list>  
                <value>${memcache.server}</value>  
            </list>  
        </property>  
        <property name="initConn">  
            <value>${memcache.initConn}</value>  
        </property>  
        <property name="minConn">  
            <value>${memcache.minConn}</value>  
        </property>  
        <property name="maxConn">  
            <value>${memcache.maxConn}</value>  
        </property>  
        <property name="maintSleep">  
            <value>${memcache.maintSleep}</value>  
        </property>  
        <property name="nagle">  
            <value>${memcache.nagle}</value>  
        </property>  
        <property name="socketTO">  
            <value>${memcache.socketTO}</value>  
        </property>  
    </bean>  
在使用memcachedClient访问memchached时,需指明poolname为memcache(默认为default,但配置文件中没有对default进行配置)
        MemCachedClient memCachedClient = new MemCachedClient("memcache");  
        memCachedClient.set("name", "simple");  
        System.out.println(memCachedClient.get("name"));  
此处实例化MemCachedClient时,必须传入参数‘memcache’类指明pool实例名称,否则在插入的时候不报错,但读取的值始终为null
原文地址:https://www.cnblogs.com/cl1234/p/5391401.html