All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.zinin.redis.factory.JedisPoolFactory Maven / Gradle / Ivy

/*
 * Copyright 2011 Alexander V. Zinin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ru.zinin.redis.factory;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Protocol;

import javax.naming.Context;
import javax.naming.Name;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;
import java.util.Hashtable;

/**
 * 

ObjectFactory for creation JedisPool objects.

*

Support properties:

*
    *
  • host - redis server hostname, "localhost" by default
  • *
  • port - redis server port, {@link redis.clients.jedis.Protocol#DEFAULT_PORT} by default
  • *
  • timeout - connection timeout in ms, {@link redis.clients.jedis.Protocol#DEFAULT_TIMEOUT} by default
  • *
  • password - redis server password
  • *
  • max-active - {@link org.apache.commons.pool.impl.GenericObjectPool.Config#maxActive}, if not set default value used
  • *
*

*

Date: 30.10.11 22:15

* * @author Alexander V. Zinin ([email protected]) */ public class JedisPoolFactory implements ObjectFactory { private final Log log = LogFactory.getLog(JedisPoolFactory.class); /** {@inheritDoc} */ @Override public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception { if ((obj == null) || !(obj instanceof Reference)) { return (null); } Reference ref = (Reference) obj; if (!"redis.clients.jedis.JedisPool".equals(ref.getClassName())) { return (null); } String host = "localhost"; int port = Protocol.DEFAULT_PORT; int timeout = Protocol.DEFAULT_TIMEOUT; String password = null; RefAddr hostRefAddr = ref.get("host"); if (hostRefAddr != null) { host = hostRefAddr.getContent().toString(); } RefAddr portRefAddr = ref.get("port"); if (portRefAddr != null) { port = Integer.parseInt(portRefAddr.getContent().toString()); } RefAddr timeoutRefAddr = ref.get("timeout"); if (timeoutRefAddr != null) { timeout = Integer.parseInt(timeoutRefAddr.getContent().toString()); } RefAddr passwordRefAddr = ref.get("password"); if (passwordRefAddr != null) { password = passwordRefAddr.getContent().toString(); } log.debug("Creating pool..."); log.debug("Host: " + host); log.debug("Port: " + port); log.debug("Timeout: " + timeout); log.trace("Password: " + password); JedisPoolConfig config = new JedisPoolConfig(); RefAddr maxActiveRefAddr = ref.get("max-active"); if (maxActiveRefAddr != null) { int maxActive = Integer.parseInt(maxActiveRefAddr.getContent().toString()); log.debug("Setting maxActive to " + maxActive); config.maxActive = maxActive; } return new JedisPool(config, host, port, timeout, password); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy