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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM