Java 类redis.clients.jedis.Protocol 实例源码

项目:nighthawk    文件:JaRedisPool.java   
public JaRedisPool(final String host) {
    URI uri = URI.create(host);
    if (JedisURIHelper.isValid(uri)) {
        String h = uri.getHost();
        int port = uri.getPort();
        String password = JedisURIHelper.getPassword(uri);
        int database = JedisURIHelper.getDBIndex(uri);
        this.internalPool = new GenericObjectPool<>(new JaRedisFactory(h, port,
                Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, password, database, null),
                new GenericObjectPoolConfig());
    } else {
        this.internalPool = new GenericObjectPool<>(new JaRedisFactory(host,
                Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, null,
                Protocol.DEFAULT_DATABASE, null), new GenericObjectPoolConfig());
    }
}
项目:JRediClients    文件:GeoRadiusParam.java   
public byte[][] getByteParams(byte[]... args) {
    ArrayList<byte[]> byteParams = new ArrayList<byte[]>();
    for (byte[] arg : args) {
        byteParams.add(arg);
    }

    if (contains(WITHCOORD)) {
        byteParams.add(SafeEncoder.encode(WITHCOORD));
    }
    if (contains(WITHDIST)) {
        byteParams.add(SafeEncoder.encode(WITHDIST));
    }

    if (contains(COUNT)) {
        byteParams.add(SafeEncoder.encode(COUNT));
        byteParams.add(Protocol.toByteArray((Integer) getParam(COUNT)));
    }

    if (contains(ASC)) {
        byteParams.add(SafeEncoder.encode(ASC));
    } else if (contains(DESC)) {
        byteParams.add(SafeEncoder.encode(DESC));
    }

    return byteParams.toArray(new byte[byteParams.size()][]);
}
项目:JRediClients    文件:ProtocolBenchmark.java   
private static long measureInputMulti() throws Exception {
  long duration = 0;

  InputStream is = new ByteArrayInputStream(
      "*4\r\n$3\r\nfoo\r\n$13\r\nbarbarbarfooz\r\n$5\r\nHello\r\n$5\r\nWorld\r\n".getBytes());

  RedisInputStream in = new RedisInputStream(is);
  for (int n = 0; n <= TOTAL_OPERATIONS; n++) {
    long start = System.nanoTime();
    Protocol.read(in);
    duration += (System.nanoTime() - start);
    in.reset();
  }

  return duration;
}
项目:JRediClients    文件:ShardedJedisTest.java   
@Test
public void testMasterSlaveShardingConsistency() {
  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3);
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2));
  Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(shards,
      Hashing.MURMUR_HASH);

  List<JedisShardInfo> otherShards = new ArrayList<JedisShardInfo>(3);
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT));
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 1));
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 2));
  Sharded<Jedis, JedisShardInfo> sharded2 = new Sharded<Jedis, JedisShardInfo>(otherShards,
      Hashing.MURMUR_HASH);

  for (int i = 0; i < 1000; i++) {
    JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer.toString(i));
    JedisShardInfo jedisShardInfo2 = sharded2.getShardInfo(Integer.toString(i));
    assertEquals(shards.indexOf(jedisShardInfo), otherShards.indexOf(jedisShardInfo2));
  }

}
项目:JRediClients    文件:ShardedJedisTest.java   
@Test
public void testMasterSlaveShardingConsistencyWithShardNaming() {
  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3);
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT, "HOST1:1234"));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1, "HOST2:1234"));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2, "HOST3:1234"));
  Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(shards,
      Hashing.MURMUR_HASH);

  List<JedisShardInfo> otherShards = new ArrayList<JedisShardInfo>(3);
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT, "HOST2:1234"));
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 1, "HOST3:1234"));
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 2, "HOST1:1234"));
  Sharded<Jedis, JedisShardInfo> sharded2 = new Sharded<Jedis, JedisShardInfo>(otherShards,
      Hashing.MURMUR_HASH);

  for (int i = 0; i < 1000; i++) {
    JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer.toString(i));
    JedisShardInfo jedisShardInfo2 = sharded2.getShardInfo(Integer.toString(i));
    assertEquals(jedisShardInfo.getName(), jedisShardInfo2.getName());
  }
}
项目:JRediClients    文件:ProtocolTest.java   
@Test
public void buildACommand() throws IOException {
  PipedInputStream pis = new PipedInputStream();
  BufferedInputStream bis = new BufferedInputStream(pis);
  PipedOutputStream pos = new PipedOutputStream(pis);
  RedisOutputStream ros = new RedisOutputStream(pos);

  Protocol.sendCommand(ros, Protocol.Command.GET, "SOMEKEY".getBytes(Protocol.CHARSET));
  ros.flush();
  pos.close();
  String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n";

  int b;
  StringBuilder sb = new StringBuilder();
  while ((b = bis.read()) != -1) {
    sb.append((char) b);
  }

  assertEquals(expectedCommand, sb.toString());
}
项目:Elune    文件:RedisManager.java   
private void createJedisPool() {

        JedisPoolConfig config = new JedisPoolConfig();

        // 设置最大连接数
        config.setMaxTotal(500);

        // 设置最大阻塞时间(毫秒)
        config.setMaxWaitMillis(1000);

        // 设置空闲连接数
        config.setMaxIdle(20);
        config.setMinIdle(10);

        // 创建连接池
        pool = new JedisPool(config, appConfig.get(CONFIG_KEY_REDIS_HOST, DEFAULT_REDIS_HOST), appConfig.getInt(CONFIG_KEY_REDIS_PORT, DEFAULT_REDIS_PORT), Protocol.DEFAULT_TIMEOUT, appConfig.get(CONFIG_KEY_REDIS_PASS, ""));
    }
项目:gorm-redis    文件:RedisEntityPersister.java   
@Override
protected EntityAccess createEntityAccess(PersistentEntity persistentEntity, Object obj, Map nativeEntry) {
    final NativeEntryModifyingEntityAccess ea = new NativeEntryModifyingEntityAccess(persistentEntity, obj) {
        public void setProperty(String name, Object value) {
            Class type = getPropertyType(name);
            if(type.isArray() && byte.class.isAssignableFrom(type.getComponentType()) && value instanceof CharSequence) {
                try {
                    super.setProperty(name, value.toString().getBytes(Protocol.CHARSET));
                } catch (UnsupportedEncodingException e) {
                    // ignore
                }
            }
            else {
                super.setProperty(name, value);
            }

        }
    };
    ea.setConversionService(getMappingContext().getConversionService());
    ea.setNativeEntry(nativeEntry);
    return ea;

}
项目:fiat    文件:RedisConfig.java   
private static JedisPool createPool(GenericObjectPoolConfig redisPoolConfig,
                                    String connection,
                                    int timeout) {
  URI redisConnection = URI.create(connection);

  String host = redisConnection.getHost();
  int port = redisConnection.getPort() == -1 ? Protocol.DEFAULT_PORT : redisConnection.getPort();

  String path = redisConnection.getPath();
  if (StringUtils.isEmpty(path)) {
    path = "/" + String.valueOf(Protocol.DEFAULT_DATABASE);
  }
  int database = Integer.parseInt(path.split("/", 2)[1]);

  String password = null;
  if (redisConnection.getUserInfo() != null) {
    password = redisConnection.getUserInfo().split(":", 2)[1];
  }

  if (redisPoolConfig == null) {
    redisPoolConfig = new GenericObjectPoolConfig();
  }

  return new JedisPool(redisPoolConfig, host, port, timeout, password, database, null);
}
项目:cachecloud    文件:GeoRadiusParam.java   
public byte[][] getByteParams(byte[]... args) {
  ArrayList<byte[]> byteParams = new ArrayList<byte[]>();
  for (byte[] arg : args) {
    byteParams.add(arg);
  }

  if (contains(WITHCOORD)) {
    byteParams.add(SafeEncoder.encode(WITHCOORD));
  }
  if (contains(WITHDIST)) {
    byteParams.add(SafeEncoder.encode(WITHDIST));
  }

  if (contains(COUNT)) {
    byteParams.add(SafeEncoder.encode(COUNT));
    byteParams.add(Protocol.toByteArray((Integer) getParam(COUNT)));
  }

  if (contains(ASC)) {
    byteParams.add(SafeEncoder.encode(ASC));
  } else if (contains(DESC)) {
    byteParams.add(SafeEncoder.encode(DESC));
  }

  return byteParams.toArray(new byte[byteParams.size()][]);
}
项目:cachecloud    文件:ProtocolBenchmark.java   
private static long measureInputMulti() throws Exception {
  long duration = 0;

  InputStream is = new ByteArrayInputStream(
      "*4\r\n$3\r\nfoo\r\n$13\r\nbarbarbarfooz\r\n$5\r\nHello\r\n$5\r\nWorld\r\n".getBytes());

  RedisInputStream in = new RedisInputStream(is);
  for (int n = 0; n <= TOTAL_OPERATIONS; n++) {
    long start = System.nanoTime();
    Protocol.read(in);
    duration += (System.nanoTime() - start);
    in.reset();
  }

  return duration;
}
项目:cachecloud    文件:ShardedJedisTest.java   
@Test
public void testMasterSlaveShardingConsistency() {
  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3);
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2));
  Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(shards,
      Hashing.MURMUR_HASH);

  List<JedisShardInfo> otherShards = new ArrayList<JedisShardInfo>(3);
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT));
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 1));
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 2));
  Sharded<Jedis, JedisShardInfo> sharded2 = new Sharded<Jedis, JedisShardInfo>(otherShards,
      Hashing.MURMUR_HASH);

  for (int i = 0; i < 1000; i++) {
    JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer.toString(i));
    JedisShardInfo jedisShardInfo2 = sharded2.getShardInfo(Integer.toString(i));
    assertEquals(shards.indexOf(jedisShardInfo), otherShards.indexOf(jedisShardInfo2));
  }

}
项目:cachecloud    文件:ShardedJedisTest.java   
@Test
public void testMasterSlaveShardingConsistencyWithShardNaming() {
  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3);
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT, "HOST1:1234"));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1, "HOST2:1234"));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2, "HOST3:1234"));
  Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(shards,
      Hashing.MURMUR_HASH);

  List<JedisShardInfo> otherShards = new ArrayList<JedisShardInfo>(3);
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT, "HOST2:1234"));
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 1, "HOST3:1234"));
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 2, "HOST1:1234"));
  Sharded<Jedis, JedisShardInfo> sharded2 = new Sharded<Jedis, JedisShardInfo>(otherShards,
      Hashing.MURMUR_HASH);

  for (int i = 0; i < 1000; i++) {
    JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer.toString(i));
    JedisShardInfo jedisShardInfo2 = sharded2.getShardInfo(Integer.toString(i));
    assertEquals(jedisShardInfo.getName(), jedisShardInfo2.getName());
  }
}
项目:cachecloud    文件:ProtocolTest.java   
@Test
public void buildACommand() throws IOException {
  PipedInputStream pis = new PipedInputStream();
  BufferedInputStream bis = new BufferedInputStream(pis);
  PipedOutputStream pos = new PipedOutputStream(pis);
  RedisOutputStream ros = new RedisOutputStream(pos);

  Protocol.sendCommand(ros, Protocol.Command.GET, "SOMEKEY".getBytes(Protocol.CHARSET));
  ros.flush();
  pos.close();
  String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n";

  int b;
  StringBuilder sb = new StringBuilder();
  while ((b = bis.read()) != -1) {
    sb.append((char) b);
  }

  assertEquals(expectedCommand, sb.toString());
}
项目:cachecloud    文件:RedisDeployCenterImpl.java   
private boolean slaveOf(final long appId, final String masterHost, final int masterPort, final String slaveHost,
        final int slavePort) {
        final Jedis slave = redisCenter.getJedis(appId, slaveHost, slavePort, Protocol.DEFAULT_TIMEOUT * 3, Protocol.DEFAULT_TIMEOUT * 3);
    try {
        boolean isSlave = new IdempotentConfirmer() {
            @Override
            public boolean execute() {
                String result = slave.slaveof(masterHost, masterPort);
                return result != null && result.equalsIgnoreCase("OK");
            }
        }.run();
        if (!isSlave) {
            logger.error(String.format("modifyAppConfig:ip=%s,port=%s failed", slaveHost, slavePort));
            return false;
        }
        redisCenter.configRewrite(appId, slaveHost, slavePort);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        return false;
    } finally {
        if (slave != null)
            slave.close();
    }

    return true;
}
项目:jahhan    文件:JedisSentinelPoolExt.java   
public JedisSentinelPoolExt(String masterName, String sentinels, final GenericObjectPoolConfig poolConfig,
        final String password, int connectionTimeout, int failMax, boolean masterWriteOnly, String clientName) {

    String[] strings = sentinels.split(",");
    Collections.addAll(_sentinels, strings);

    this.poolConfig = poolConfig;
    this.connectionTimeout = connectionTimeout;
    if (!StringUtils.isBlank(password)) {
        this.password = password;
    }
    this.database = Protocol.DEFAULT_DATABASE;
    this.failMax = failMax;
    this.masterName = masterName;
    this.masterWriteOnly = masterWriteOnly;
    if (!StringUtils.isBlank(clientName)) {
        this.clientName = clientName;
    }

    HostAndPort master = initSentinels(_sentinels, masterName);
    initPool(master);

    initReadPool();
}
项目:jahhan    文件:JedisSentinelPoolExt.java   
/**
 * 读池增加新节点
 * 
 * @param hostAndPort
 */
protected synchronized void insertReadPool(HostAndPort hostAndPort) {
    boolean exists = false;
    for (JedisPoolExt oldJedisPool : jedisReadPools) {
        if (oldJedisPool.getHostAndPort().equals(hostAndPort)) {
            exists = true;
            break;
        }
    }
    if (!exists) {
        JedisPoolExt newJedisPool = new JedisPoolExt(poolConfig, hostAndPort.getHost(), hostAndPort.getPort(),
                connectionTimeout, password, Protocol.DEFAULT_DATABASE, clientName);
        jedisReadPools.add(newJedisPool);

        // 新来的节点,可能是之前挂掉后恢复的节点,所以要检查其是否存在于失败列表中,要去除
        failMap.remove(hostAndPort);

        log.info("Add JedisReadPool at " + hostAndPort);
    }
}
项目:Jedis    文件:ProtocolBenchmark.java   
private static long measureInputMulti() throws Exception {
  long duration = 0;

  InputStream is = new ByteArrayInputStream(
      "*4\r\n$3\r\nfoo\r\n$13\r\nbarbarbarfooz\r\n$5\r\nHello\r\n$5\r\nWorld\r\n".getBytes());

  RedisInputStream in = new RedisInputStream(is);
  for (int n = 0; n <= TOTAL_OPERATIONS; n++) {
    long start = System.nanoTime();
    Protocol.read(in);
    duration += (System.nanoTime() - start);
    in.reset();
  }

  return duration;
}
项目:Jedis    文件:ShardedJedisTest.java   
@Test
public void testMasterSlaveShardingConsistency() {
  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3);
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2));
  Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(shards,
      Hashing.MURMUR_HASH);

  List<JedisShardInfo> otherShards = new ArrayList<JedisShardInfo>(3);
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT));
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 1));
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 2));
  Sharded<Jedis, JedisShardInfo> sharded2 = new Sharded<Jedis, JedisShardInfo>(otherShards,
      Hashing.MURMUR_HASH);

  for (int i = 0; i < 1000; i++) {
    JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer.toString(i));
    JedisShardInfo jedisShardInfo2 = sharded2.getShardInfo(Integer.toString(i));
    assertEquals(shards.indexOf(jedisShardInfo), otherShards.indexOf(jedisShardInfo2));
  }

}
项目:Jedis    文件:ShardedJedisTest.java   
@Test
public void testMasterSlaveShardingConsistencyWithShardNaming() {
  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3);
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT, "HOST1:1234"));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1, "HOST2:1234"));
  shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2, "HOST3:1234"));
  Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(shards,
      Hashing.MURMUR_HASH);

  List<JedisShardInfo> otherShards = new ArrayList<JedisShardInfo>(3);
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT, "HOST2:1234"));
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 1, "HOST3:1234"));
  otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 2, "HOST1:1234"));
  Sharded<Jedis, JedisShardInfo> sharded2 = new Sharded<Jedis, JedisShardInfo>(otherShards,
      Hashing.MURMUR_HASH);

  for (int i = 0; i < 1000; i++) {
    JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer.toString(i));
    JedisShardInfo jedisShardInfo2 = sharded2.getShardInfo(Integer.toString(i));
    assertEquals(jedisShardInfo.getName(), jedisShardInfo2.getName());
  }
}
项目:Jedis    文件:ProtocolTest.java   
@Test
public void buildACommand() throws IOException {
  PipedInputStream pis = new PipedInputStream();
  BufferedInputStream bis = new BufferedInputStream(pis);
  PipedOutputStream pos = new PipedOutputStream(pis);
  RedisOutputStream ros = new RedisOutputStream(pos);

  Protocol.sendCommand(ros, Protocol.Command.GET, "SOMEKEY".getBytes(Protocol.CHARSET));
  ros.flush();
  pos.close();
  String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n";

  int b;
  StringBuilder sb = new StringBuilder();
  while ((b = bis.read()) != -1) {
    sb.append((char) b);
  }

  assertEquals(expectedCommand, sb.toString());
}
项目:commons-jkit    文件:JedisFacade.java   
@Override
public Long getNoIncr(String key) {
    final Jedis resource = this.getResource();
    try {
        final byte[] rawKey = this.serializeKey(key);

        final byte[] rawValue = resource.get(rawKey);
        if (ArrayUtils.isEmpty(rawValue)) {
            return null;
        }

        return Long.valueOf(new String(rawValue, Protocol.CHARSET));
    }
    catch (Exception e) {
        throw new CacheException("redis:get", e);
    }
    finally {
        this.returnResource(resource);
    }
}
项目:commons-jkit    文件:JedisClusterFacade.java   
@Override
public Long getNoIncr(String key) {
    try {
        final byte[] rawKey = this.serializeKey(key);

        final byte[] rawValue = this.jedisCluster.get(rawKey);
        if (ArrayUtils.isEmpty(rawValue)) {
            return null;
        }

        return Long.valueOf(new String(rawValue, Protocol.CHARSET));
    }
    catch (Exception e) {
        throw new CacheException("redis:get", e);
    }
}
项目:distributed-session-manager    文件:RedisSession.java   
@Override
public void setAttribute(String name, Object value) {
    super.setAttribute(name, value);

    if (value == null || name.startsWith("javax.zkoss.zk.ui.Session") || !_manager.isStarted()) {
        return;
    }

    try {
        byte[] bytesValue = KryoSerializer.write(value);
        if (_manager.debug) {
            log.info("id=" + this.id + ",name=" + name + ",strValue=" + new String(bytesValue, Protocol.CHARSET));
        }
        _manager.jedisHset(RedisManager.TOMCAT_SESSION_PREFIX + this.id, name, bytesValue);

    } catch (Exception ex) {
        log.error("error:name=" + name + ";value=" + value, ex);
    }
}
项目:quartz-redis-jobstore    文件:BaseTest.java   
@Before
public void setUpRedis() throws IOException, SchedulerConfigException {
    port = getPort();
    logger.debug("Attempting to start embedded Redis server on port " + port);
    redisServer = RedisServer.builder()
            .port(port)
            .build();
    redisServer.start();
    final short database = 1;
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setTestOnBorrow(true);
    jedisPool = new JedisPool(jedisPoolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null, database);

    jobStore = new RedisJobStore();
    jobStore.setHost(host);
    jobStore.setLockTimeout(2000);
    jobStore.setPort(port);
    jobStore.setInstanceId("testJobStore1");
    jobStore.setDatabase(database);
    mockScheduleSignaler = mock(SchedulerSignaler.class);
    jobStore.initialize(null, mockScheduleSignaler);
    schema = new RedisJobStoreSchema();

    jedis = jedisPool.getResource();
    jedis.flushDB();
}
项目:sharded-spring-data-redis    文件:RedisShardingStrategyImpl.java   
@Override
public int getShardIdByKey(String key) {
    // 1. Get bytes
    byte[] vinBytes;
    try {
        vinBytes = key.getBytes(Protocol.CHARSET);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e.getMessage());
    }

    // 2. Get VIN hash
    MurmurHash murmurHash = new MurmurHash();
    long vinHash = murmurHash.hash(vinBytes);

    // 3. Get shard id from tail map of shardsMap by vinHash
    SortedMap<Long, Integer> tailMap = shardsMap.tailMap(vinHash);
    if (tailMap.isEmpty()) {
        return shardsMap.get(shardsMap.firstKey());
    }

    return tailMap.get(tailMap.firstKey());
}
项目:redis-quartz    文件:RedisJobStore.java   
@Override
public void initialize(ClassLoadHelper loadHelper,
        SchedulerSignaler signaler) throws SchedulerConfigException {

    this.loadHelper = loadHelper;
    this.signaler = signaler;       

    // initializing a connection pool
    JedisPoolConfig config = new JedisPoolConfig();
    if (password != null)
        pool = new JedisPool(config, host, port, Protocol.DEFAULT_TIMEOUT, password);               
    else
        pool = new JedisPool(config, host, port, Protocol.DEFAULT_TIMEOUT);

    // initializing a locking connection pool with a longer timeout
    if (lockTimeout == 0)
        lockTimeout = 10 * 60 * 1000; // 10 Minutes locking timeout

     lockPool = new JedisLock(pool.getResource(), "JobLock", lockTimeout);

}
项目:solr-redis    文件:RedisQParserPlugin.java   
@Override
public void init(final NamedList args) {
  final GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
  poolConfig.setMaxTotal(getInt(args, MAX_CONNECTIONS_FIELD, DEFAULT_MAX_CONNECTIONS));

  final String host = getString(args, HOST_FIELD, HostAndPort.LOCALHOST_STR);
  final int timeout = getInt(args, TIMEOUT_FIELD, Protocol.DEFAULT_TIMEOUT);
  final String password = getString(args, PASSWORD_FIELD, null);
  final int database = getInt(args, DATABASE_FIELD, Protocol.DEFAULT_DATABASE);
  final int retries = getInt(args, RETRIES_FIELD, DEFAULT_RETRIES);

  final String[] hostAndPort = host.split(":");
  final JedisPool jedisConnectionPool = createPool(poolConfig, hostAndPort[0],
      hostAndPort.length == 2 ? Integer.parseInt(hostAndPort[1]) : Protocol.DEFAULT_PORT, timeout, password,
      database);

  connectionHandler = createCommandHandler(jedisConnectionPool, retries);

  log.info("Initialized RedisQParserPlugin with host: " + host);
}
项目:jframe    文件:JedisServiceImpl.java   
public void start(JedisPoolConfig config) {
    LOG.info("JedisServiceImpl starting");
    String[] hosts = conf.getGroupIds();
    for (String h : hosts) {
        if ("".equals(h))
            continue;
        try {
            String ip = conf.getConf(h, "ip");
            // if ("127.0.0.1".equals(ip)) {
            // continue;
            // }
            int port = conf.getConfInt(h, "port", "6379");

            int timeout = conf.getConfInt(h, "timeout", "2000");
            if (port == 0) {
                _jedis.put(h, new JedisPool(config, ip, Protocol.DEFAULT_PORT, timeout));
            } else {
                _jedis.put(h, new JedisPool(config, ip, port, timeout));
            }
        } catch (Exception e) {
            LOG.error(e.getMessage());
            continue;
        }
    }
    LOG.info("JedisServiceImpl start successfully");
}
项目:ddth-cache-adapter    文件:RedisCacheFactory.java   
/**
 * Creates a new {@link JedisPool}.
 * 
 * @param hostAndPort
 * @param password
 * @param db
 * @param timeoutMs
 * @return
 */
public static JedisPool newJedisPool(String hostAndPort, String password, int db,
        long timeoutMs) {
    final int maxTotal = Runtime.getRuntime().availableProcessors();
    final int maxIdle = maxTotal / 2;

    JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxTotal(maxTotal);
    poolConfig.setMinIdle(1);
    poolConfig.setMaxIdle(maxIdle > 0 ? maxIdle : 1);
    poolConfig.setMaxWaitMillis(timeoutMs + 1000);
    // poolConfig.setTestOnBorrow(true);
    poolConfig.setTestWhileIdle(true);

    String[] tokens = hostAndPort.split(":");
    String host = tokens.length > 0 ? tokens[0] : Protocol.DEFAULT_HOST;
    int port = tokens.length > 1 ? Integer.parseInt(tokens[1]) : Protocol.DEFAULT_PORT;
    JedisPool jedisPool = new JedisPool(poolConfig, host, port, (int) timeoutMs, password, db);
    return jedisPool;
}
项目:ddth-id    文件:RedisIdGenerator.java   
/**
 * Creates a new {@link JedisPool}.
 * 
 * @param hostAndPort
 * @param password
 * @param db
 * @param timeoutMs
 * @return
 * @since 0.5.0
 */
public static JedisPool newJedisPool(String hostAndPort, String password, int db,
        long timeoutMs) {
    final int maxTotal = Runtime.getRuntime().availableProcessors();
    final int maxIdle = maxTotal / 2;

    JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxTotal(maxTotal);
    poolConfig.setMinIdle(1);
    poolConfig.setMaxIdle(maxIdle > 0 ? maxIdle : 1);
    poolConfig.setMaxWaitMillis(timeoutMs);
    // poolConfig.setTestOnBorrow(true);
    poolConfig.setTestWhileIdle(true);

    String[] tokens = hostAndPort.split(":");
    String host = tokens.length > 0 ? tokens[0] : Protocol.DEFAULT_HOST;
    int port = tokens.length > 1 ? Integer.parseInt(tokens[1]) : Protocol.DEFAULT_PORT;
    JedisPool jedisPool = new JedisPool(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT,
            password, db);
    return jedisPool;
}
项目:hat-vldb2014-code    文件:RedisClient.java   
public void init() throws DBException {
    Properties props = getProperties();
    int port;

    String portString = props.getProperty(PORT_PROPERTY);
    if (portString != null) {
        port = Integer.parseInt(portString);
    }
    else {
        port = Protocol.DEFAULT_PORT;
    }
    String host = props.getProperty(HOST_PROPERTY);

    jedis = new Jedis(host, port);
    jedis.connect();

    String password = props.getProperty(PASSWORD_PROPERTY);
    if (password != null) {
        jedis.auth(password);
    }
}
项目:rdbi    文件:StrictRateLimiterTest.java   
private RateLimiter buildRateLimiter(double permitsPerSecond) {
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxTotal(30);
    JedisPool jedisPool = new JedisPool(jedisPoolConfig, "localhost", 6379, Protocol.DEFAULT_TIMEOUT);

    RDBI rdbi = new RDBI(jedisPool);

    // Verify our loading of the LUA script upon initial start.
    rdbi.withHandle(new Callback<Void>() {
        @Override
        public Void run(Handle handle) {
            handle.jedis().scriptFlush();
            return null;
        }
    });

    return new StrictRateLimiter("d:test:rdbi", rdbi, UUID.randomUUID().toString(), permitsPerSecond);
}
项目:rdbi    文件:TokenBucketRateLimiterTest.java   
private RDBI getRdbi() {
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxTotal(30);
    JedisPool jedisPool = new JedisPool(jedisPoolConfig, "localhost", 6379, Protocol.DEFAULT_TIMEOUT);

    RDBI rdbi = new RDBI(jedisPool);

    // Verify our loading of the LUA script upon initial start.
    rdbi.withHandle(new Callback<Void>() {
        @Override
        public Void run(Handle handle) {
            handle.jedis().scriptFlush();
            return null;
        }
    });
    return rdbi;
}
项目:bolton-sigmod2013-code    文件:RedisClient.java   
public void init() throws DBException {
    Properties props = getProperties();
    int port;

    String portString = props.getProperty(PORT_PROPERTY);
    if (portString != null) {
        port = Integer.parseInt(portString);
    }
    else {
        port = Protocol.DEFAULT_PORT;
    }
    String host = props.getProperty(HOST_PROPERTY);

    jedis = new Jedis(host, port);
    jedis.connect();

    String password = props.getProperty(PASSWORD_PROPERTY);
    if (password != null) {
        jedis.auth(password);
    }
}
项目:logstash-gelf    文件:RedisGelfSenderProvider.java   
@Override
public GelfSender create(GelfSenderConfiguration configuration) throws IOException {

    String graylogHost = configuration.getHost();

    URI hostUri = URI.create(graylogHost);
    int port = hostUri.getPort();
    if (port <= 0) {
        port = configuration.getPort();
    }

    if (port <= 0) {
        port = Protocol.DEFAULT_PORT;
    }

    if (hostUri.getFragment() == null || hostUri.getFragment().trim().equals("")) {
        throw new IllegalArgumentException("Redis URI must specify fragment");
    }

    if (hostUri.getHost() == null) {
        throw new IllegalArgumentException("Redis URI must specify host");
    }

    Pool<Jedis> pool = RedisSenderPoolProvider.getJedisPool(hostUri, port);
    return new GelfREDISSender(pool, hostUri.getFragment(), configuration.getErrorReporter());
}
项目:ddth-commons    文件:JedisUtils.java   
/**
 * Create a new {@link ShardedJedisPool}.
 * 
 * @param poolConfig
 *            format {@code host1:port1,host2:port2,...}, default Redis port is used if not
 *            specified
 * @param password
 * @param timeoutMs
 * @return
 */
public static ShardedJedisPool newShardedJedisPool(JedisPoolConfig poolConfig,
        String hostsAndPorts, String password, int timeoutMs) {
    List<JedisShardInfo> shards = new ArrayList<>();
    String[] hapList = hostsAndPorts.split("[,;\\s]+");
    for (String hostAndPort : hapList) {
        String[] tokens = hostAndPort.split(":");
        String host = tokens.length > 0 ? tokens[0] : Protocol.DEFAULT_HOST;
        int port = tokens.length > 1 ? Integer.parseInt(tokens[1]) : Protocol.DEFAULT_PORT;
        JedisShardInfo shardInfo = new JedisShardInfo(host, port, timeoutMs);
        shardInfo.setPassword(password);
        shards.add(shardInfo);
    }
    ShardedJedisPool jedisPool = new ShardedJedisPool(poolConfig, shards);
    return jedisPool;
}
项目:argo    文件:RedisPool.java   
public RedisPool(final String host) {
    URI uri = URI.create(host);
    if (uri.getScheme() != null && uri.getScheme().equals("redis")) {
        String h = uri.getHost();
        int port = uri.getPort();
        String password = JedisURIHelper.getPassword(uri);
        int database = 0;
        Integer dbIndex = JedisURIHelper.getDBIndex(uri);
        if (dbIndex != null) {
            database = dbIndex.intValue();
        }
        this.internalPool = new GenericObjectPool<BinaryJedis>(
                new BinaryJedisFactory(h, port, Protocol.DEFAULT_TIMEOUT,
                        password, database, null),
                new GenericObjectPoolConfig());
    } else {
        this.internalPool = new GenericObjectPool<BinaryJedis>(new BinaryJedisFactory(
                host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT,
                null, Protocol.DEFAULT_DATABASE, null),
                new GenericObjectPoolConfig());
    }
}
项目:flume-redis    文件:TestRedisSourceCreation.java   
/**
 * Verify that setting a timeout in the configuration does not impact the database setting
 * 
 * @throws EventDeliveryException
 */
@Test
public void timeoutConfiguredTest() throws EventDeliveryException {

    String host = "localhost";
    int timeout = 10;

    JedisPoolFactory mockJedisPoolFactory = mock(JedisPoolFactory.class);

    RedisSource redisSource = new RedisSource(mockJedisPoolFactory);
    Context context = new Context();
    context.put(RedisSourceConfigurationConstant.HOST, host);
    context.put(RedisSourceConfigurationConstant.BATCH_SIZE, "10");
    context.put(RedisSourceConfigurationConstant.TIMEOUT, Integer.toString(timeout));
    redisSource.configure(context);
    redisSource.doStart();

    verify(mockJedisPoolFactory).create(any(JedisPoolConfig.class), eq(host), eq(Protocol.DEFAULT_PORT),
            eq(timeout), isNull(String.class), eq(Protocol.DEFAULT_DATABASE));
}