xmemcached与spring3.0集成使用

原文地址:http://www.blogjava.net/killme2008/archive/2009/12/24/307095.html
昨天收到一个xmc的issue报告,大概的意思是将Xmemcached与spring 2.5集成没有任何问题,但是将spring升级到3.0就会抛出一个异常,并且spring容器无法正常启动,异常信息类似“Couldn't find a destroy method named 'shutdown' on bean XMemcachedClientFactoryBean”。更详细的情况可以看这里,这是这位朋友分析的结果,简单来说就是spring 3.0对于查找destroy method为空的情况处理不同了,过去是打个日志,现在是抛出一个异常。
   问题说完,这里主要是介绍下这个问题的解决方式,事实上Xmemcached有一个没有被文档化的Spring配置方式,没有写入文档的主要考虑是以为wiki介绍的第一种方式已经足够,而builder的方式相对繁琐一些。通过XmemcachedClientBuilder的这个factory bean的factory-method,也就是build方法来构建MemcachedClient,这就可以绕开spring 3.0的这个问题。一个示范配置如下:

<bean name="memcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
        <constructor-arg>
            <list>
                <bean class="java.net.InetSocketAddress">
                    <constructor-arg>
                        <value>localhost</value>
                    </constructor-arg>
                    <constructor-arg>
                        <value>12000</value>
                    </constructor-arg>
                </bean>
            </list>
        </constructor-arg>
    </bean>
    <bean name="memcachedClient" factory-bean="memcachedClientBuilder"
        factory-method="build" destroy-method="shutdown" />


  memcachedClientBuilder作为一个factory-bean,接受一个InetSocketAddress列表作为构造函数传入,最后MemcachedClient就可以通过factory-method——也就是build方法创建了。

   多个节点情况下,可能你想设置权重,那么传入memcachedClientBuilder的第二个构造函数参数权重数组即可

<bean name="memcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
        <constructor-arg>
            <list>
                <bean class="java.net.InetSocketAddress">
                    <constructor-arg>
                        <value>localhost</value>
                    </constructor-arg>
                    <constructor-arg>
                        <value>12000</value>
                    </constructor-arg>
                </bean>
                <bean class="java.net.InetSocketAddress">
                    <constructor-arg>
                        <value>localhost</value>
                    </constructor-arg>
                    <constructor-arg>
                        <value>12001</value>
                    </constructor-arg>
                </bean>
            </list>
        </constructor-arg>
        <constructor-arg>
            <list>
                <value>1</value>
                <value>2</value>
            </list>
        </constructor-arg>
    </bean>
    <bean name="memcachedClient" factory-bean="memcachedClientBuilder"
        factory-method="build" destroy-method="shutdown" />

上面的例子将localhost:12000的权重设置为1,而localhost:12001的权重设置为2。除了这些配置外,XmemcachedClientBuilder还有其他选项,如配置一致性哈希算法、连接池等,完整的配置例子如下:
<bean name="memcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
 <!-- XMemcachedClientBuilder have two arguments.First is server list,and second is weights array. -->
                <constructor-arg>
                        <list>
                                <bean class="java.net.InetSocketAddress">
                                        <constructor-arg>
                                                <value>localhost</value>
                                        </constructor-arg>
                                        <constructor-arg>
                                                <value>12000</value>
                                        </constructor-arg>
                                </bean>
                                <bean class="java.net.InetSocketAddress">
                                        <constructor-arg>
                                                <value>localhost</value>
                                        </constructor-arg>
                                        <constructor-arg>
                                                <value>12001</value>
                                        </constructor-arg>
                                </bean>
                        </list>
                </constructor-arg>
                <constructor-arg>
                        <list>
                                <value>1</value>
                                <value>2</value>
                        </list>
                </constructor-arg>
                <property name="connectionPoolSize" value="2"></property>
                <property name="commandFactory">
               <bean class="net.rubyeye.xmemcached.command.TextCommandFactory"></bean>
                </property>
                <property name="sessionLocator">
                <bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator">
               </bean>
                </property>
                <property name="transcoder">
               <bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
                </property>
        </bean>
        <!-- Use factory bean to build memcached client -->
        <bean name="memcachedClient" factory-bean="memcachedClientBuilder"
                factory-method="build" destroy-method="shutdown"/>

你可能感兴趣的:(xmemcached)