Java Code Examples for org.redisson.config.Config#setCodec()

The following examples show how to use org.redisson.config.Config#setCodec() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testStringCodec() {
    Config config = createConfig();
    config.setCodec(StringCodec.INSTANCE);
    RedissonClient redisson = Redisson.create(config);

    RMap<String, String> rmap = redisson.getMap("TestRMap01");
    rmap.put("A", "1");
    rmap.put("B", "2");

    Iterator<Map.Entry<String, String>> iterator = rmap.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<String, String> next = iterator.next();
        assertThat(next).isIn(new AbstractMap.SimpleEntry("A", "1"), new AbstractMap.SimpleEntry("B", "2"));
    }

    destroy(rmap);
    redisson.shutdown();
}
 
Example 2
Source File: RedissonManager.java    From game-server with MIT License 6 votes vote down vote up
/**
 * 连接服务器
 * 
 * @author JiangZhiYong
 * @QQ 359135103 2017年9月15日 下午3:36:06
 * @param configPath
 */
public static void connectRedis(String configPath) {
	if (redisson != null) {
		LOGGER.warn("Redisson客户端已经连接");
	}
	redissonClusterConfig = FileUtil.getConfigXML(configPath, "redissonClusterConfig.xml",
			RedissonClusterConfig.class);
	if (redissonClusterConfig == null) {
		LOGGER.warn("{}/redissonClusterConfig.xml文件不存在", configPath);
		System.exit(0);
	}
	Config config = new Config();
	config.setCodec(new FastJsonCodec());
	ClusterServersConfig clusterServersConfig = config.useClusterServers();
	clusterServersConfig.setScanInterval(redissonClusterConfig.getScanInterval()); // 集群状态扫描间隔时间,单位是毫秒
	// 可以用"rediss://"来启用SSL连接
	redissonClusterConfig.getNodes().forEach(url -> clusterServersConfig.addNodeAddress(url));
	clusterServersConfig.setReadMode(redissonClusterConfig.getReadMode());
	clusterServersConfig.setSubscriptionMode(redissonClusterConfig.getSubscriptionMode());
	redisson = Redisson.create(config);
}
 
Example 3
Source File: RedisClientManager.java    From apiman with Apache License 2.0 6 votes vote down vote up
/**
 * @param storeName a name to associate with the instance
 * @return a new or existing Redis client for the given store name
 */
public RedisBackingStore getRedis(String storeName) {
    if (isNull(client)) {
        synchronized (mutex) {
            if (isNull(client)) { // double-guard
                final Config config;
                if (nonNull(overrideConfig)) {
                    config = overrideConfig;
                } else if (nonNull(configFile)) {
                    config = loadConfigFromFile();
                } else {
                    throw new IllegalStateException("No Redisson configuration provided");
                }
                config.setCodec(new StringCodec());
                client = Redisson.create(config);
            }
        }
    }
    return new RedisBackingStore(client, storeName);
}
 
Example 4
Source File: JCacheTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testJson() throws InterruptedException, IllegalArgumentException, URISyntaxException, IOException {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .port(6311)
            .run();
    
    URL configUrl = getClass().getResource("redisson-jcache.json");
    Config cfg = Config.fromJSON(configUrl);
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JavaTimeModule());
    cfg.setCodec(new TypedJsonJacksonCodec(String.class, LocalDateTime.class, objectMapper));
    
    Configuration<String, LocalDateTime> config = RedissonConfiguration.fromConfig(cfg);
    Cache<String, LocalDateTime> cache = Caching.getCachingProvider().getCacheManager()
            .createCache("test", config);
    
    LocalDateTime t = LocalDateTime.now();
    cache.put("1", t);
    Assert.assertEquals(t, cache.get("1"));
    
    cache.close();
    runner.stop();
}
 
Example 5
Source File: CommandHandlersTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test(expected = RuntimeException.class)
public void testEncoder() throws InterruptedException {
    Config config = createConfig();
    config.setCodec(new ErrorsCodec());
    
    RedissonClient redisson = Redisson.create(config);
    
    redisson.getBucket("1234").set("1234");
}
 
Example 6
Source File: RedissonTwoLockedThread.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Before
public void before() throws IOException, InterruptedException {
    if (RedissonRuntimeEnvironment.isTravis) {
        RedisRunner.startDefaultRedisServerInstance();
        redisson = createInstance();
    }
    Config config = BaseTest.createConfig();
    config.setCodec(codec);
    redisson = Redisson.create(config);
}
 
Example 7
Source File: RedisInitializer.java    From t-io with Apache License 2.0 5 votes vote down vote up
private Config getSingleServerConfig() {
    Config config = new Config();
    String address = redisConfig.toString();
    SingleServerConfig singleServerConfig = config.useSingleServer()
            .setAddress(address)
            .setConnectionPoolSize(redisConfig.getPoolSize())
            .setConnectionMinimumIdleSize(redisConfig.getMinimumIdleSize());
    if (redisConfig.hasPassword()) {
        singleServerConfig.setPassword(redisConfig.getPassword());
    }
    config.setCodec(new FstCodec());
    //默认情况下,看门狗的检查锁的超时时间是30秒钟
    config.setLockWatchdogTimeout(1000 * 30);
    return config;
}
 
Example 8
Source File: RedisEventChannelTestCase.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Before
public void start() {
    // 注意此处的编解码器
    Codec codec = new ByteArrayCodec();
    Config configuration = new Config();
    configuration.setCodec(codec);
    configuration.useSingleServer().setAddress("redis://127.0.0.1:6379");

    redisson = (Redisson) Redisson.create(configuration);
    keys = redisson.getKeys();
    keys.flushdb();
}
 
Example 9
Source File: RedisIdentityFactoryTestCase.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Before
public void testBefore() {
    // 注意此处的编解码器
    Codec codec = new JsonJacksonCodec();
    Config configuration = new Config();
    configuration.setCodec(codec);
    configuration.useSingleServer().setAddress("redis://127.0.0.1:6379");
    redisson = (Redisson) Redisson.create(configuration);
    keys = redisson.getKeys();
    keys.flushdb();
}
 
Example 10
Source File: RedissonCodecTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testListOfStrings() {
    Config config = createConfig();
    config.setCodec(new JsonJacksonCodec());
    RedissonClient redisson = Redisson.create(config);

    RMap<String, List<String>> map = redisson.getMap("list of strings", jsonListOfStringCodec);
    map.put("foo", new ArrayList<String>(Arrays.asList("bar")));

    RMap<String, List<String>> map2 = redisson.getMap("list of strings", jsonListOfStringCodec);

    assertThat(map2).isEqualTo(map);
    
    redisson.shutdown();
}
 
Example 11
Source File: RedissonCodecTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testJdk() {
    Config config = createConfig();
    config.setCodec(codec);
    RedissonClient redisson = Redisson.create(config);

    test(redisson);
}
 
Example 12
Source File: RedissonConfig.java    From kkbinlog with Apache License 2.0 5 votes vote down vote up
@Bean(destroyMethod = "shutdown")
RedissonClient redisson() throws Exception {
    Config config = new Config();
    config.setKeepPubSubOrder(keepPubSubOrder).useSingleServer().setAddress(address)
            .setConnectionMinimumIdleSize(connectionMinimumIdleSize)
            .setConnectionPoolSize(connectionPoolSize)
            .setDatabase(database)
            .setDnsMonitoring(dnsMonitoring)
            .setDnsMonitoringInterval(dnsMonitoringInterval)
            .setSubscriptionConnectionMinimumIdleSize(subscriptionConnectionMinimumIdleSize)
            .setSubscriptionConnectionPoolSize(subscriptionConnectionPoolSize)
            .setSubscriptionsPerConnection(subscriptionsPerConnection)
            .setClientName(clientName)
            .setFailedAttempts(failedAttempts)
            .setRetryAttempts(retryAttempts)
            .setRetryInterval(retryInterval)
            .setReconnectionTimeout(reconnectionTimeout)
            .setTimeout(timeout)
            .setConnectTimeout(connectTimeout)
            .setIdleConnectionTimeout(idleConnectionTimeout)
            .setPingTimeout(pingTimeout)
            .setPassword(password);
    Codec codec = (Codec) ClassUtils.forName(getCodec(), ClassUtils.getDefaultClassLoader()).newInstance();
    config.setCodec(codec);
    config.setThreads(thread);
    config.setEventLoopGroup(new NioEventLoopGroup());
    config.setUseLinuxNativeEpoll(false);
    return Redisson.create(config);
}
 
Example 13
Source File: RedissonCodecTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testLZ4() {
    Config config = createConfig();
    config.setCodec(lz4Codec);
    RedissonClient redisson = Redisson.create(config);

    test(redisson);
}
 
Example 14
Source File: RedissonCodecTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testCbor() {
    Config config = createConfig();
    config.setCodec(cborCodec);
    RedissonClient redisson = Redisson.create(config);

    test(redisson);

}
 
Example 15
Source File: RedissonConfig.java    From kkbinlog with Apache License 2.0 5 votes vote down vote up
@Bean(destroyMethod = "shutdown")
RedissonClient redisson() throws Exception {
    Config config = new Config();
    config.setKeepPubSubOrder(keepPubSubOrder).useSingleServer().setAddress(address)
            .setConnectionMinimumIdleSize(connectionMinimumIdleSize)
            .setConnectionPoolSize(connectionPoolSize)
            .setDatabase(database)
            .setDnsMonitoring(dnsMonitoring)
            .setDnsMonitoringInterval(dnsMonitoringInterval)
            .setSubscriptionConnectionMinimumIdleSize(subscriptionConnectionMinimumIdleSize)
            .setSubscriptionConnectionPoolSize(subscriptionConnectionPoolSize)
            .setSubscriptionsPerConnection(subscriptionsPerConnection)
            .setClientName(clientName)
            .setFailedAttempts(failedAttempts)
            .setRetryAttempts(retryAttempts)
            .setRetryInterval(retryInterval)
            .setReconnectionTimeout(reconnectionTimeout)
            .setTimeout(timeout)
            .setConnectTimeout(connectTimeout)
            .setIdleConnectionTimeout(idleConnectionTimeout)
            .setPingTimeout(pingTimeout)
            .setPassword(password);
    Codec codec = (Codec) ClassUtils.forName(getCodec(), ClassUtils.getDefaultClassLoader()).newInstance();
    config.setCodec(codec);
    config.setThreads(thread);
    config.setEventLoopGroup(new NioEventLoopGroup());
    config.setUseLinuxNativeEpoll(false);
    return Redisson.create(config);
}
 
Example 16
Source File: RedissonCodecTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testJson() {
    Config config = createConfig();
    config.setCodec(jsonCodec);
    RedissonClient redisson = Redisson.create(config);

    test(redisson);
}
 
Example 17
Source File: RedissonCacheRegionFactory.java    From mPass with Apache License 2.0 5 votes vote down vote up
/**
 * 创建redission客户端
 *
 * @param properties
 * @return
 */
@Override
protected RedissonClient createRedissonClient(Map properties) {
    Config customConfig = new Config(defaultConfig);
    customConfig.setCodec(new SnappyCodec());
    return Redisson.create(customConfig);
}
 
Example 18
Source File: RedissonCodecTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testKryo() {
    Config config = createConfig();
    config.setCodec(kryoCodec);
    RedissonClient redisson = Redisson.create(config);

    test(redisson);
}
 
Example 19
Source File: RedissonReferenceReactiveTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldUseDefaultCodec() throws Exception {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
    objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
    JsonJacksonCodec codec = new JsonJacksonCodec(objectMapper);

    Config config = new Config();
    config.setCodec(codec);
    config.useSingleServer()
            .setAddress(RedisRunner.getDefaultRedisServerBindAddressAndPort());

    RedissonReactiveClient reactive = Redisson.createReactive(config);
    RBucketReactive<Object> b1 = reactive.getBucket("b1");
    sync(b1.set(new MyObject()));
    RSetReactive<Object> s1 = reactive.getSet("s1");
    assertTrue(sync(s1.add(b1)));
    assertTrue(codec == b1.getCodec());

    Config config1 = new Config();
    config1.setCodec(codec);
    config1.useSingleServer()
            .setAddress(RedisRunner.getDefaultRedisServerBindAddressAndPort());
    RedissonReactiveClient reactive1 = Redisson.createReactive(config1);

    RSetReactive<RBucketReactive> s2 = reactive1.getSet("s1");
    RBucketReactive<MyObject> b2 = sync(s2.iterator(1));
    assertTrue(codec == b2.getCodec());
    assertTrue(sync(b2.get()) instanceof MyObject);
    reactive.shutdown();
    reactive1.shutdown();
}
 
Example 20
Source File: RedisInitializer.java    From tio-starters with MIT License 5 votes vote down vote up
private Config getSingleServerConfig() {
    Config config = new Config();
    String address = redisConfig.toString();
    SingleServerConfig singleServerConfig = config.useSingleServer()
            .setAddress(address)
            .setConnectionPoolSize(redisConfig.getPoolSize())
            .setConnectionMinimumIdleSize(redisConfig.getMinimumIdleSize());
    if (redisConfig.hasPassword()) {
        singleServerConfig.setPassword(redisConfig.getPassword());
    }
    config.setCodec(new FstCodec());
    //默认情况下,看门狗的检查锁的超时时间是30秒钟
    config.setLockWatchdogTimeout(1000 * 30);
    return config;
}