Java Code Examples for redis.clients.jedis.Protocol

The following are top voted examples for showing how to use redis.clients.jedis.Protocol. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: nighthawk   File: JaRedisPool.java   View source code 6 votes vote down vote up
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());
    }
}
 
Example 2
Project: JRediClients   File: GeoRadiusParam.java   View source code 6 votes vote down vote up
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()][]);
}
 
Example 3
Project: JRediClients   File: ProtocolBenchmark.java   View source code 6 votes vote down vote up
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;
}
 
Example 4
Project: JRediClients   File: ShardedJedisTest.java   View source code 6 votes vote down vote up
@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));
  }

}
 
Example 5
Project: JRediClients   File: ShardedJedisTest.java   View source code 6 votes vote down vote up
@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());
  }
}
 
Example 6
Project: JRediClients   File: ProtocolTest.java   View source code 6 votes vote down vote up
@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());
}
 
Example 7
Project: Elune   File: RedisManager.java   View source code 6 votes vote down vote up
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, ""));
    }
 
Example 8
Project: gorm-redis   File: RedisEntityPersister.java   View source code 6 votes vote down vote up
@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;

}
 
Example 9
Project: fiat   File: RedisConfig.java   View source code 6 votes vote down vote up
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);
}
 
Example 10
Project: cachecloud   File: GeoRadiusParam.java   View source code 6 votes vote down vote up
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()][]);
}
 
Example 11
Project: cachecloud   File: ProtocolBenchmark.java   View source code 6 votes vote down vote up
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;
}
 
Example 12
Project: cachecloud   File: ShardedJedisTest.java   View source code 6 votes vote down vote up
@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));
  }

}
 
Example 13
Project: cachecloud   File: ShardedJedisTest.java   View source code 6 votes vote down vote up
@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());
  }
}
 
Example 14
Project: cachecloud   File: ProtocolTest.java   View source code 6 votes vote down vote up
@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());
}
 
Example 15
Project: cachecloud   File: RedisDeployCenterImpl.java   View source code 6 votes vote down vote up
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;
}
 
Example 16
Project: jahhan   File: JedisSentinelPoolExt.java   View source code 6 votes vote down vote up
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();
}
 
Example 17
Project: jahhan   File: JedisSentinelPoolExt.java   View source code 6 votes vote down vote up
/**
 * 读池增加新节点
 * 
 * @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);
	}
}
 
Example 18
Project: Jedis   File: ProtocolBenchmark.java   View source code 6 votes vote down vote up
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;
}
 
Example 19
Project: Jedis   File: ShardedJedisTest.java   View source code 6 votes vote down vote up
@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));
  }

}
 
Example 20
Project: Jedis   File: ShardedJedisTest.java   View source code 6 votes vote down vote up
@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());
  }
}
 
Example 21
Project: Jedis   File: ProtocolTest.java   View source code 6 votes vote down vote up
@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());
}
 
Example 22
Project: commons-jkit   File: JedisFacade.java   View source code 6 votes vote down vote up
@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);
	}
}
 
Example 23
Project: commons-jkit   File: JedisClusterFacade.java   View source code 6 votes vote down vote up
@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);
	}
}
 
Example 24
Project: distributed-session-manager   File: RedisSession.java   View source code 6 votes vote down vote up
@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);
	}
}
 
Example 25
Project: quartz-redis-jobstore   File: BaseTest.java   View source code 6 votes vote down vote up
@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();
}
 
Example 26
Project: sharded-spring-data-redis   File: RedisShardingStrategyImpl.java   View source code 6 votes vote down vote up
@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());
}
 
Example 27
Project: redis-quartz   File: RedisJobStore.java   View source code 6 votes vote down vote up
@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);

}
 
Example 28
Project: solr-redis   File: RedisQParserPlugin.java   View source code 6 votes vote down vote up
@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);
}
 
Example 29
Project: pippo   File: JedisFactory.java   View source code 6 votes vote down vote up
/**
 * Create a Jedis poll with pippo settings. URL format:
 * 'redis://[:[email protected]]host[:port][/db-number][?option=value]'
 *
 * @param settings pippo settings
 * @return Jedis pool
 */
public static JedisPool create(final PippoSettings settings) {
    String host = settings.getString(HOST, Protocol.DEFAULT_HOST).trim();
    int minIdle = settings.getInteger(MIN_IDLE, GenericObjectPoolConfig.DEFAULT_MIN_IDLE);
    int maxIdle = settings.getInteger(MAX_IDLE, GenericObjectPoolConfig.DEFAULT_MAX_IDLE);
    int maxTotal = settings.getInteger(MAX_TOTAL, GenericObjectPoolConfig.DEFAULT_MAX_TOTAL);
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMinIdle(minIdle);
    config.setMaxIdle(maxIdle);
    config.setMaxTotal(maxTotal);

    try {
        return new JedisPool(config, new URI(host));
    } catch (URISyntaxException e) {
        throw new PippoRuntimeException("Malformed redis URI", e);
    }
}
 
Example 30
Project: jframe   File: JedisServiceImpl.java   View source code 6 votes vote down vote up
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");
}
 
Example 31
Project: ddth-cache-adapter   File: RedisCacheFactory.java   View source code 6 votes vote down vote up
/**
 * 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;
}
 
Example 32
Project: ddth-id   File: RedisIdGenerator.java   View source code 6 votes vote down vote up
/**
 * 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;
}
 
Example 33
Project: hat-vldb2014-code   File: RedisClient.java   View source code 6 votes vote down vote up
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);
    }
}
 
Example 34
Project: rdbi   File: StrictRateLimiterTest.java   View source code 6 votes vote down vote up
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);
}
 
Example 35
Project: rdbi   File: TokenBucketRateLimiterTest.java   View source code 6 votes vote down vote up
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;
}
 
Example 36
Project: bolton-sigmod2013-code   File: RedisClient.java   View source code 6 votes vote down vote up
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);
    }
}
 
Example 37
Project: logstash-gelf   File: RedisGelfSenderProvider.java   View source code 6 votes vote down vote up
@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());
}
 
Example 38
Project: ddth-commons   File: JedisUtils.java   View source code 6 votes vote down vote up
/**
 * 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;
}
 
Example 39
Project: argo   File: RedisPool.java   View source code 6 votes vote down vote up
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());
    }
}
 
Example 40
Project: flume-redis   File: TestRedisSourceCreation.java   View source code 6 votes vote down vote up
/**
 * 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));
}