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

The following examples show how to use org.redisson.config.Config#fromJSON() . 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: RedissonRegionFactory.java    From redisson with Apache License 2.0 6 votes vote down vote up
private Config loadConfig(ClassLoader classLoader, String fileName) {
    InputStream is = classLoader.getResourceAsStream(fileName);
    if (is != null) {
        try {
            return Config.fromJSON(is);
        } catch (IOException e) {
            try {
                is = classLoader.getResourceAsStream(fileName);
                return Config.fromYAML(is);
            } catch (IOException e1) {
                throw new CacheException("Can't parse yaml config", e1);
            }
        }
    }
    return null;
}
 
Example 2
Source File: RedissonRegionFactory.java    From redisson with Apache License 2.0 6 votes vote down vote up
private Config loadConfig(ClassLoader classLoader, String fileName) {
    InputStream is = classLoader.getResourceAsStream(fileName);
    if (is != null) {
        try {
            return Config.fromJSON(is);
        } catch (IOException e) {
            try {
                is = classLoader.getResourceAsStream(fileName);
                return Config.fromYAML(is);
            } catch (IOException e1) {
                throw new CacheException("Can't parse yaml config", e1);
            }
        }
    }
    return null;
}
 
Example 3
Source File: JCacheTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsync() throws Exception {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .port(6311)
            .run();
    
    URL configUrl = getClass().getResource("redisson-jcache.json");
    Config cfg = Config.fromJSON(configUrl);
    
    Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
    Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
            .createCache("test", config);

    CacheAsync<String, String> async = cache.unwrap(CacheAsync.class);
    async.putAsync("1", "2").get();
    assertThat(async.getAsync("1").get()).isEqualTo("2");
    
    cache.close();
    runner.stop();
}
 
Example 4
Source File: JCacheTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testReactive() throws Exception {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .port(6311)
            .run();
    
    URL configUrl = getClass().getResource("redisson-jcache.json");
    Config cfg = Config.fromJSON(configUrl);
    
    Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
    Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
            .createCache("test", config);

    CacheReactive<String, String> reactive = cache.unwrap(CacheReactive.class);
    reactive.put("1", "2").block();
    assertThat(reactive.get("1").block()).isEqualTo("2");
    
    cache.close();
    runner.stop();
}
 
Example 5
Source File: JCacheTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testRx() throws Exception {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .port(6311)
            .run();
    
    URL configUrl = getClass().getResource("redisson-jcache.json");
    Config cfg = Config.fromJSON(configUrl);
    
    Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
    Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
            .createCache("test", config);

    CacheRx<String, String> rx = cache.unwrap(CacheRx.class);
    rx.put("1", "2").blockingAwait();
    assertThat(rx.get("1").blockingGet()).isEqualTo("2");
    
    cache.close();
    runner.stop();
}
 
Example 6
Source File: JCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAllHighVolume() throws Exception {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .port(6311)
            .run();
    
    URL configUrl = getClass().getResource("redisson-jcache.json");
    Config cfg = Config.fromJSON(configUrl);
    
    Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
    Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
            .createCache("test", config);

    Map<String, String> m = new HashMap<>();
    for (int i = 0; i < 10000; i++) {
        m.put("" + i, "" + i);
    }
    cache.putAll(m);
    
    Map<String, String> entries = cache.getAll(m.keySet());
    assertThat(entries).isEqualTo(m);
    
    cache.close();
    runner.stop();
}
 
Example 7
Source File: RedissonRegionFactory.java    From redisson with Apache License 2.0 5 votes vote down vote up
private Config loadConfig(String configPath) {
    try {
        return Config.fromJSON(new File(configPath));
    } catch (IOException e) {
        // trying next format
        try {
            return Config.fromYAML(new File(configPath));
        } catch (IOException e1) {
            throw new CacheException("Can't parse default yaml config", e1);
        }
    }
}
 
Example 8
Source File: RedissonRegionFactory.java    From redisson with Apache License 2.0 5 votes vote down vote up
private Config loadConfig(String configPath) {
    try {
        return Config.fromJSON(new File(configPath));
    } catch (IOException e) {
        // trying next format
        try {
            return Config.fromYAML(new File(configPath));
        } catch (IOException e1) {
            throw new CacheException("Can't parse default yaml config", e1);
        }
    }
}
 
Example 9
Source File: RedissonRegionFactory.java    From redisson with Apache License 2.0 5 votes vote down vote up
private Config loadConfig(String configPath) {
    try {
        return Config.fromJSON(new File(configPath));
    } catch (IOException e) {
        // trying next format
        try {
            return Config.fromYAML(new File(configPath));
        } catch (IOException e1) {
            throw new CacheException("Can't parse default yaml config", e1);
        }
    }
}
 
Example 10
Source File: RedisClientManager.java    From apiman with Apache License 2.0 5 votes vote down vote up
private Config loadConfigFromFile() {
    if (configFile.exists()) {
        try {
            return Config.fromJSON(configFile);
        } catch (IOException e) {
            throw new RuntimeException(String.format("Error reading Redisson configuration file: %s", configFile), e);
        }
    } else {
        throw new RuntimeException(String.format("Redisson configuration file: '%s' does not exist", configFile));
    }
}
 
Example 11
Source File: RedissonRegionFactory.java    From redisson with Apache License 2.0 5 votes vote down vote up
private Config loadConfig(String configPath) {
    try {
        return Config.fromJSON(new File(configPath));
    } catch (IOException e) {
        // trying next format
        try {
            return Config.fromYAML(new File(configPath));
        } catch (IOException e1) {
            throw new CacheException("Can't parse default yaml config", e1);
        }
    }
}
 
Example 12
Source File: JCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testRedissonConfig() 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);
    
    Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
    Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
            .createCache("test", config);
    
    cache.put("1", "2");
    Assert.assertEquals("2", cache.get("1"));
    
    cache.put("key", "value");
    String result = cache.getAndRemove("key");

    Assert.assertEquals("value", result);
    Assert.assertNull(cache.get("key"));

    cache.put("key", "value");
    cache.remove("key");
    Assert.assertNull(cache.get("key"));
    
    cache.close();
    runner.stop();
}
 
Example 13
Source File: RedissonConfigurationIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenJSONFileConfig_thenRedissonConnectToRedis() throws IOException {
    
    File configFile = new File(getClass().getClassLoader().getResource("singleNodeConfig.json").getFile());
    String configContent = Files.toString(configFile, Charset.defaultCharset()).replace("6379", String.valueOf(port));
    
    Config config = Config.fromJSON(configContent);
    client = Redisson.create(config);

    assert(client != null && client.getKeys().count() >= 0);
}
 
Example 14
Source File: JCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAll() throws Exception {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .port(6311)
            .run();
    
    URL configUrl = getClass().getResource("redisson-jcache.json");
    Config cfg = Config.fromJSON(configUrl);
    
    Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
    Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
            .createCache("test", config);
    
    cache.put("1", "2");
    cache.put("3", "4");
    
    Map<String, String> entries = cache.getAll(new HashSet<String>(Arrays.asList("1", "3", "7")));
    Map<String, String> expected = new HashMap<String, String>();
    expected.put("1", "2");
    expected.put("3", "4");
    assertThat(entries).isEqualTo(expected);
    
    cache.close();
    runner.stop();
}
 
Example 15
Source File: RedisInitializer.java    From tio-starters with MIT License 5 votes vote down vote up
private Config getConfigByFile() {
    try {
        if (redisConfig.isJSONConfig()) {
            return Config.fromJSON(getFileUri(redisConfig.getConfigPath()));
        } else if (redisConfig.isYAMLConfig()) {
            return Config.fromYAML(getFileUri(redisConfig.getConfigPath()));
        }
    } catch (IOException e) {
        logger.error("init with file config error", e);
    }
    return null;
}
 
Example 16
Source File: JCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoveAll() throws Exception {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .port(6311)
            .run();
    
    URL configUrl = getClass().getResource("redisson-jcache.json");
    Config cfg = Config.fromJSON(configUrl);
    
    Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
    Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
            .createCache("test", config);
    
    cache.put("1", "2");
    cache.put("3", "4");
    cache.put("4", "4");
    cache.put("5", "5");
    
    Set<? extends String> keys = new HashSet<String>(Arrays.asList("1", "3", "4", "5"));
    cache.removeAll(keys);
    assertThat(cache.containsKey("1")).isFalse();
    assertThat(cache.containsKey("3")).isFalse();
    assertThat(cache.containsKey("4")).isFalse();
    assertThat(cache.containsKey("5")).isFalse();
    
    cache.close();
    runner.stop();
}
 
Example 17
Source File: JCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutAll() throws Exception {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .port(6311)
            .run();
    
    URL configUrl = getClass().getResource("redisson-jcache.json");
    Config cfg = Config.fromJSON(configUrl);
    
    Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
    Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
            .createCache("test", config);
    
    Map<String, String> map = new HashMap<>();
    for (int i = 0; i < 10000; i++) {
        map.put("" + i, "" + i);
    }
    
    long start = System.currentTimeMillis();
    cache.putAll(map);
    System.out.println(System.currentTimeMillis() - start);
    
    for (int i = 0; i < 10000; i++) {
        assertThat(cache.containsKey("" + i)).isTrue();
    }
    
    cache.close();
    runner.stop();
}
 
Example 18
Source File: JCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreatedExpiryPolicy() throws Exception {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .port(6311)
            .run();

    URL configUrl = getClass().getResource("redisson-jcache.json");
    Config cfg = Config.fromJSON(configUrl);

    MutableConfiguration c = new MutableConfiguration();
    c.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(MILLISECONDS, 500)));
    Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg, c);
    Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
            .createCache("test", config);

    cache.put("1", "2");
    Thread.sleep(500);
    assertThat(cache.get("1")).isNull();
    cache.put("1", "3");
    assertThat(cache.get("1")).isEqualTo("3");
    Thread.sleep(500);
    assertThat(cache.get("1")).isNull();

    cache.put("1", "4");
    assertThat(cache.get("1")).isEqualTo("4");
    Thread.sleep(100);
    cache.put("1", "5");
    assertThat(cache.get("1")).isEqualTo("5");

    cache.close();
    runner.stop();
}
 
Example 19
Source File: JCachingProvider.java    From redisson with Apache License 2.0 5 votes vote down vote up
private Config loadConfig(URI uri) {
    Config config = null;
    try {
        URL jsonUrl = null;
        if (DEFAULT_URI_PATH.equals(uri.getPath())) {
            jsonUrl = JCachingProvider.class.getResource("/redisson-jcache.json");
        } else {
            jsonUrl = uri.toURL();
        }
        if (jsonUrl == null) {
            throw new IOException();
        }
        config = Config.fromJSON(jsonUrl);
    } catch (IOException e) {
        try {
            URL yamlUrl = null;
            if (DEFAULT_URI_PATH.equals(uri.getPath())) {
                yamlUrl = JCachingProvider.class.getResource("/redisson-jcache.yaml");
            } else {
                yamlUrl = uri.toURL();
            }
            if (yamlUrl != null) {
                config = Config.fromYAML(yamlUrl);
            }
        } catch (IOException e2) {
            // skip
        }
    }
    return config;
}
 
Example 20
Source File: RedisInitializer.java    From t-io with Apache License 2.0 5 votes vote down vote up
private Config getConfigByFile() {
    try {
        if (redisConfig.isJSONConfig()) {
            return Config.fromJSON(getFileUri(redisConfig.getConfigPath()));
        } else if (redisConfig.isYAMLConfig()) {
            return Config.fromYAML(getFileUri(redisConfig.getConfigPath()));
        }
    } catch (IOException e) {
        logger.error("init with file config error", e);
    }
    return null;
}