Search in sources :

Example 21 with Config

use of org.redisson.config.Config in project redisson by redisson.

the class WeightedRoundRobinBalancerTest method testUseMasterForReadsIfNoConnectionsToSlaves.

@Test
public void testUseMasterForReadsIfNoConnectionsToSlaves() throws IOException, InterruptedException {
    RedisProcess master = null;
    RedisProcess slave = null;
    RedissonClient client = null;
    try {
        master = redisTestInstance();
        slave = redisTestInstance();
        Map<String, Integer> weights = new HashMap<>();
        weights.put(master.getRedisServerAddressAndPort(), 1);
        weights.put(slave.getRedisServerAddressAndPort(), 2);
        Config config = new Config();
        config.useMasterSlaveServers().setReadMode(ReadMode.SLAVE).setMasterAddress(master.getRedisServerAddressAndPort()).addSlaveAddress(slave.getRedisServerAddressAndPort()).setLoadBalancer(new WeightedRoundRobinBalancer(weights, 1));
        client = Redisson.create(config);
        // To simulate network connection issues to slave, stop the slave
        // after creating the client. Cannot create the client without the
        // slave running. See https://github.com/mrniko/redisson/issues/580
        slave.stop();
        RedissonClient clientCopy = client;
        assertThat(clientCopy.getBucket("key").get()).isNull();
    } finally {
        if (master != null) {
            master.stop();
        }
        if (slave != null) {
            slave.stop();
        }
        if (client != null) {
            client.shutdown();
        }
    }
}
Also used : RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) HashMap(java.util.HashMap) Config(org.redisson.config.Config) Test(org.junit.Test)

Example 22 with Config

use of org.redisson.config.Config in project redisson by redisson.

the class RedissonExecutorServiceTest method beforeClass.

@BeforeClass
public static void beforeClass() throws IOException, InterruptedException {
    BaseTest.beforeClass();
    if (!RedissonRuntimeEnvironment.isTravis) {
        Config config = createConfig();
        RedissonNodeConfig nodeConfig = new RedissonNodeConfig(config);
        nodeConfig.setExecutorServiceWorkers(Collections.singletonMap("test", 1));
        node = RedissonNode.create(nodeConfig);
        node.start();
    }
}
Also used : Config(org.redisson.config.Config) BaseTest.createConfig(org.redisson.BaseTest.createConfig) RedissonNodeConfig(org.redisson.config.RedissonNodeConfig) RedissonNodeConfig(org.redisson.config.RedissonNodeConfig) BeforeClass(org.junit.BeforeClass)

Example 23 with Config

use of org.redisson.config.Config in project redisson by redisson.

the class RedissonTopicTest method testReattach.

@Test
public void testReattach() throws InterruptedException, IOException, ExecutionException, TimeoutException {
    RedisProcess runner = new RedisRunner().nosave().randomDir().randomPort().run();
    Config config = new Config();
    config.useSingleServer().setAddress(runner.getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    final AtomicBoolean executed = new AtomicBoolean();
    RTopic<Integer> topic = redisson.getTopic("topic");
    topic.addListener(new MessageListener<Integer>() {

        @Override
        public void onMessage(String channel, Integer msg) {
            if (msg == 1) {
                executed.set(true);
            }
        }
    });
    runner.stop();
    runner = new RedisRunner().port(runner.getRedisServerPort()).nosave().randomDir().run();
    Thread.sleep(1000);
    redisson.getTopic("topic").publish(1);
    await().atMost(5, TimeUnit.SECONDS).untilTrue(executed);
    redisson.shutdown();
    runner.stop();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) Test(org.junit.Test)

Example 24 with Config

use of org.redisson.config.Config in project redisson by redisson.

the class JCachingProvider method loadConfig.

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;
}
Also used : Config(org.redisson.config.Config) IOException(java.io.IOException) URL(java.net.URL)

Example 25 with Config

use of org.redisson.config.Config in project redisson by redisson.

the class JCachingProvider method getCacheManager.

@Override
public CacheManager getCacheManager(URI uri, ClassLoader classLoader, Properties properties) {
    if (uri == null) {
        uri = getDefaultURI();
    }
    if (uri == null) {
        throw new CacheException("Uri is not defined. Can't load default configuration");
    }
    if (classLoader == null) {
        classLoader = getDefaultClassLoader();
    }
    ConcurrentMap<URI, CacheManager> value = new ConcurrentHashMap<URI, CacheManager>();
    ConcurrentMap<URI, CacheManager> oldValue = managers.putIfAbsent(classLoader, value);
    if (oldValue != null) {
        value = oldValue;
    }
    CacheManager manager = value.get(uri);
    if (manager != null) {
        return manager;
    }
    Config config = loadConfig(uri);
    Redisson redisson = null;
    if (config != null) {
        redisson = (Redisson) Redisson.create(config);
    }
    manager = new JCacheManager(redisson, classLoader, this, properties, uri);
    CacheManager oldManager = value.putIfAbsent(uri, manager);
    if (oldManager != null) {
        if (redisson != null) {
            redisson.shutdown();
        }
        manager = oldManager;
    }
    return manager;
}
Also used : CacheException(javax.cache.CacheException) Config(org.redisson.config.Config) CacheManager(javax.cache.CacheManager) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URI(java.net.URI) Redisson(org.redisson.Redisson)

Aggregations

Config (org.redisson.config.Config)68 Test (org.junit.Test)51 RedissonClient (org.redisson.api.RedissonClient)44 RedisProcess (org.redisson.RedisRunner.RedisProcess)17 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 IOException (java.io.IOException)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 RedissonNodeConfig (org.redisson.config.RedissonNodeConfig)4 HashMap (java.util.HashMap)3 ExecutorService (java.util.concurrent.ExecutorService)3 Before (org.junit.Before)3 SerializationCodec (org.redisson.codec.SerializationCodec)3 File (java.io.File)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 LifecycleException (org.apache.catalina.LifecycleException)2 BeforeClass (org.junit.BeforeClass)2 BaseTest.createConfig (org.redisson.BaseTest.createConfig)2 RLock (org.redisson.api.RLock)2 JsonJacksonCodec (org.redisson.codec.JsonJacksonCodec)2