001/* 002 * (C) Copyright 2006-2014 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.nuxeo.ecm.core.redis; 017 018import java.util.HashSet; 019import java.util.Set; 020 021import org.apache.commons.pool2.impl.GenericObjectPoolConfig; 022import org.nuxeo.common.xmap.annotation.XNode; 023import org.nuxeo.common.xmap.annotation.XNodeList; 024import org.nuxeo.common.xmap.annotation.XObject; 025import org.nuxeo.runtime.model.Descriptor; 026 027import redis.clients.jedis.Jedis; 028import redis.clients.jedis.JedisPoolConfig; 029import redis.clients.jedis.JedisSentinelPool; 030import redis.clients.util.Pool; 031 032@XObject("sentinel") 033public class RedisSentinelDescriptor extends RedisPoolDescriptor { 034 035 @XObject("host") 036 public static class RedisHostDescriptor implements Descriptor { 037 038 @XNode("@name") 039 public String name; 040 041 @XNode("@port") 042 public int port; 043 044 /** Empty constructor. */ 045 public RedisHostDescriptor() { 046 } 047 048 protected RedisHostDescriptor(String name, int port) { 049 this.name = name; 050 this.port = port; 051 } 052 053 @Override 054 public String getId() { 055 return name; 056 } 057 } 058 059 @XNodeList(value = "host", type = RedisHostDescriptor[].class, componentType = RedisHostDescriptor.class) 060 public RedisHostDescriptor[] hosts = new RedisHostDescriptor[0]; 061 062 @XNode("master") 063 public String master = "master"; 064 065 @XNode("failoverTimeout") 066 public int failoverTimeout = 300; 067 068 @SuppressWarnings("resource") // JedisSentinelPool closed by RedisComponent.stop() 069 @Override 070 public RedisExecutor newExecutor() throws RuntimeException { 071 Set<String> sentinels = new HashSet<>(); 072 for (RedisHostDescriptor host : hosts) { 073 sentinels.add(host.name + ":" + host.port); 074 } 075 GenericObjectPoolConfig<?> cfg = new JedisPoolConfig(); 076 Pool<Jedis> sentinel = new JedisSentinelPool(master, sentinels, cfg, timeout, password, database); 077 RedisExecutor base = new RedisPoolExecutor(sentinel); 078 return new RedisFailoverExecutor(failoverTimeout, base); 079 } 080 081}