001/* 002 * (C) Copyright 2014-2015 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Thierry Delprat 016 * Florent Guillaume 017 */ 018package org.nuxeo.ecm.core.redis.contribs; 019 020import java.io.IOException; 021 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024import org.nuxeo.ecm.core.api.NuxeoException; 025import org.nuxeo.ecm.core.redis.RedisAdmin; 026import org.nuxeo.ecm.core.redis.RedisCallable; 027import org.nuxeo.ecm.core.redis.RedisExecutor; 028import org.nuxeo.ecm.core.uidgen.AbstractUIDSequencer; 029import org.nuxeo.runtime.api.Framework; 030 031import redis.clients.jedis.Jedis; 032import redis.clients.jedis.exceptions.JedisException; 033 034/** 035 * Redis-based UID generator. 036 * 037 * @since 7.4 038 */ 039public class RedisUIDSequencer extends AbstractUIDSequencer { 040 041 protected static final Log log = LogFactory.getLog(RedisUIDSequencer.class); 042 043 protected String namespace; 044 045 @Override 046 public void init() { 047 RedisAdmin redisAdmin = Framework.getService(RedisAdmin.class); 048 namespace = redisAdmin.namespace("counters"); 049 } 050 051 @Override 052 public void dispose() { 053 } 054 055 @Override 056 public int getNext(String key) { 057 RedisExecutor executor = Framework.getService(RedisExecutor.class); 058 try { 059 return executor.execute(new RedisCallable<Long>() { 060 @Override 061 public Long call(Jedis jedis) { 062 return jedis.incr(namespace + key); 063 } 064 }).intValue(); 065 } catch (JedisException e) { 066 throw new NuxeoException(e); 067 } 068 } 069 070}