|
| 1 | +package net.spy.memcached.spring; |
| 2 | + |
| 3 | +import java.util.Collection; |
| 4 | + |
| 5 | +import net.spy.memcached.AddrUtil; |
| 6 | +import net.spy.memcached.ConnectionFactoryBuilder; |
| 7 | +import net.spy.memcached.ConnectionFactoryBuilder.Locator; |
| 8 | +import net.spy.memcached.ConnectionFactoryBuilder.Protocol; |
| 9 | +import net.spy.memcached.ConnectionObserver; |
| 10 | +import net.spy.memcached.FailureMode; |
| 11 | +import net.spy.memcached.HashAlgorithm; |
| 12 | +import net.spy.memcached.MemcachedClient; |
| 13 | +import net.spy.memcached.OperationFactory; |
| 14 | +import net.spy.memcached.auth.AuthDescriptor; |
| 15 | +import net.spy.memcached.ops.OperationQueueFactory; |
| 16 | +import net.spy.memcached.transcoders.Transcoder; |
| 17 | + |
| 18 | +import org.springframework.beans.factory.FactoryBean; |
| 19 | + |
| 20 | +/** |
| 21 | + * A Spring {@link FactoryBean} creating {@link MemcachedClient} instances. |
| 22 | + * <p> |
| 23 | + * Usage example: |
| 24 | + * <pre> |
| 25 | + * {@code |
| 26 | + * <bean id="memcachedClient" class="net.spy.memcached.utils.MemcachedClientFactoryBean"> |
| 27 | + * <property name="servers" value="${pajamas.remoteHosts}"/> |
| 28 | + * <property name="protocol" value="${pajamas.client.protocol}"/> |
| 29 | + * <property name="transcoder"> |
| 30 | + * <bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder"/> |
| 31 | + * </property> |
| 32 | + * <property name="hashAlg" value="${pajamas.client.hashAlg}"/> |
| 33 | + * <property name="locatorType" value="${pajamas.client.locatorType}"/> |
| 34 | + * </bean> |
| 35 | + * } |
| 36 | + * </pre> |
| 37 | + * </p> |
| 38 | + * @author Eran Harel |
| 39 | + */ |
| 40 | +public class MemcachedClientFactoryBean implements FactoryBean<MemcachedClient> { |
| 41 | + private final ConnectionFactoryBuilder connectionFactoryBuilder = new ConnectionFactoryBuilder(); |
| 42 | + private String servers; |
| 43 | + |
| 44 | + @Override |
| 45 | + public MemcachedClient getObject() throws Exception { |
| 46 | + return new MemcachedClient(connectionFactoryBuilder.build(), AddrUtil.getAddresses(servers)); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Class<MemcachedClient> getObjectType() { |
| 51 | + return MemcachedClient.class; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean isSingleton() { |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + public void setServers(final String servers) { |
| 60 | + this.servers = servers; |
| 61 | + } |
| 62 | + |
| 63 | + public void setAuthDescriptor(final AuthDescriptor to) { |
| 64 | + connectionFactoryBuilder.setAuthDescriptor(to); |
| 65 | + } |
| 66 | + |
| 67 | + public void setDaemon(final boolean d) { |
| 68 | + connectionFactoryBuilder.setDaemon(d); |
| 69 | + } |
| 70 | + |
| 71 | + public void setFailureMode(final FailureMode fm) { |
| 72 | + connectionFactoryBuilder.setFailureMode(fm); |
| 73 | + } |
| 74 | + |
| 75 | + public void setHashAlg(final HashAlgorithm to) { |
| 76 | + connectionFactoryBuilder.setHashAlg(to); |
| 77 | + } |
| 78 | + |
| 79 | + public void setInitialObservers(final Collection<ConnectionObserver> obs) { |
| 80 | + connectionFactoryBuilder.setInitialObservers(obs); |
| 81 | + } |
| 82 | + |
| 83 | + public void setLocatorType(final Locator l) { |
| 84 | + connectionFactoryBuilder.setLocatorType(l); |
| 85 | + } |
| 86 | + |
| 87 | + public void setMaxReconnectDelay(final long to) { |
| 88 | + connectionFactoryBuilder.setMaxReconnectDelay(to); |
| 89 | + } |
| 90 | + |
| 91 | + public void setOpFact(final OperationFactory f) { |
| 92 | + connectionFactoryBuilder.setOpFact(f); |
| 93 | + } |
| 94 | + |
| 95 | + public void setOpQueueFactory(final OperationQueueFactory q) { |
| 96 | + connectionFactoryBuilder.setOpQueueFactory(q); |
| 97 | + } |
| 98 | + |
| 99 | + public void setOpQueueMaxBlockTime(final long t) { |
| 100 | + connectionFactoryBuilder.setOpQueueMaxBlockTime(t); |
| 101 | + } |
| 102 | + |
| 103 | + public void setOpTimeout(final long t) { |
| 104 | + connectionFactoryBuilder.setOpTimeout(t); |
| 105 | + } |
| 106 | + |
| 107 | + public void setProtocol(final Protocol prot) { |
| 108 | + connectionFactoryBuilder.setProtocol(prot); |
| 109 | + } |
| 110 | + |
| 111 | + public void setReadBufferSize(final int to) { |
| 112 | + connectionFactoryBuilder.setReadBufferSize(to); |
| 113 | + } |
| 114 | + |
| 115 | + public void setReadOpQueueFactory(final OperationQueueFactory q) { |
| 116 | + connectionFactoryBuilder.setReadOpQueueFactory(q); |
| 117 | + } |
| 118 | + |
| 119 | + public void setShouldOptimize(final boolean o) { |
| 120 | + connectionFactoryBuilder.setShouldOptimize(o); |
| 121 | + } |
| 122 | + |
| 123 | + public void setTimeoutExceptionThreshold(final int to) { |
| 124 | + connectionFactoryBuilder.setTimeoutExceptionThreshold(to); |
| 125 | + } |
| 126 | + |
| 127 | + public void setTranscoder(final Transcoder<Object> t) { |
| 128 | + connectionFactoryBuilder.setTranscoder(t); |
| 129 | + } |
| 130 | + |
| 131 | + public void setUseNagleAlgorithm(final boolean to) { |
| 132 | + connectionFactoryBuilder.setUseNagleAlgorithm(to); |
| 133 | + } |
| 134 | + |
| 135 | + public void setWriteOpQueueFactory(final OperationQueueFactory q) { |
| 136 | + connectionFactoryBuilder.setWriteOpQueueFactory(q); |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments