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 org.apache.commons.logging.Log; 021import org.apache.commons.logging.LogFactory; 022import org.nuxeo.ecm.core.api.NuxeoException; 023import org.nuxeo.ecm.core.redis.RedisAdmin; 024import org.nuxeo.ecm.core.redis.RedisCallable; 025import org.nuxeo.ecm.core.redis.RedisExecutor; 026import org.nuxeo.ecm.core.uidgen.AbstractUIDSequencer; 027import org.nuxeo.runtime.api.Framework; 028 029import redis.clients.jedis.Jedis; 030import redis.clients.jedis.exceptions.JedisException; 031 032/** 033 * Redis-based UID generator. 034 * 035 * @since 7.4 036 */ 037public class RedisUIDSequencer extends AbstractUIDSequencer { 038 039 protected static final Log log = LogFactory.getLog(RedisUIDSequencer.class); 040 041 protected String namespace; 042 043 @Override 044 public void init() { 045 RedisAdmin redisAdmin = Framework.getService(RedisAdmin.class); 046 namespace = redisAdmin.namespace("counters"); 047 } 048 049 @Override 050 public void dispose() { 051 } 052 053 @Override 054 public int getNext(String key) { 055 RedisExecutor executor = Framework.getService(RedisExecutor.class); 056 try { 057 return executor.execute(new RedisCallable<Long>() { 058 @Override 059 public Long call(Jedis jedis) { 060 return jedis.incr(namespace + key); 061 } 062 }).intValue(); 063 } catch (JedisException e) { 064 throw new NuxeoException(e); 065 } 066 } 067 068}