org.redisson.config.RedissonNodeConfig Java Examples

The following examples show how to use org.redisson.config.RedissonNodeConfig. 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: ExecutorServiceExamples.java    From redisson-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    Config config = new Config();
    config.useClusterServers()
        .addNodeAddress("127.0.0.1:7001", "127.0.0.1:7002", "127.0.0.1:7003");
    
    RedissonClient redisson = Redisson.create(config);

    RedissonNodeConfig nodeConfig = new RedissonNodeConfig(config);
    nodeConfig.setExecutorServiceWorkers(Collections.singletonMap("myExecutor", 1));
    RedissonNode node = RedissonNode.create(nodeConfig);
    node.start();

    RExecutorService e = redisson.getExecutorService("myExecutor");
    e.execute(new RunnableTask());
    e.submit(new CallableTask());
    
    e.shutdown();
    node.shutdown();
}
 
Example #2
Source File: SchedulerServiceExamples.java    From redisson-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    Config config = new Config();
    config.useClusterServers()
        .addNodeAddress("127.0.0.1:7001", "127.0.0.1:7002", "127.0.0.1:7003");
    
    RedissonClient redisson = Redisson.create(config);

    RedissonNodeConfig nodeConfig = new RedissonNodeConfig(config);
    nodeConfig.setExecutorServiceWorkers(Collections.singletonMap("myExecutor", 5));
    RedissonNode node = RedissonNode.create(nodeConfig);
    node.start();

    RScheduledExecutorService e = redisson.getExecutorService("myExecutor");
    e.schedule(new RunnableTask(), 10, TimeUnit.SECONDS);
    e.schedule(new CallableTask(), 4, TimeUnit.MINUTES);

    e.schedule(new RunnableTask(), CronSchedule.of("10 0/5 * * * ?"));
    e.schedule(new RunnableTask(), CronSchedule.dailyAtHourAndMinute(10, 5));
    e.schedule(new RunnableTask(), CronSchedule.weeklyOnDayAndHourAndMinute(12, 4, Calendar.MONDAY, Calendar.FRIDAY));
    
    e.shutdown();
    node.shutdown();
}
 
Example #3
Source File: RedissonScheduledExecutorServiceTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleWorker() throws InterruptedException {
    Config config = createConfig();
    RedissonNodeConfig nodeConfig = new RedissonNodeConfig(config);
    nodeConfig.getExecutorServiceWorkers().put("JobA", 1);
    RedissonNode node = RedissonNode.create(nodeConfig);
    node.start();
    
    RedissonClient client = Redisson.create(config);
    RScheduledExecutorService executorService = client.getExecutorService("JobA");
    executorService.schedule(new TestTask() , CronSchedule.of("0/1 * * * * ?"));
    
    TimeUnit.MILLISECONDS.sleep(4900);
    
    assertThat(client.getAtomicLong("counter").get()).isEqualTo(4);
    
    client.shutdown();
    node.shutdown();
}
 
Example #4
Source File: RedissonScheduledExecutorServiceTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 7000)
public void testTaskResume() throws InterruptedException, ExecutionException {
    RScheduledExecutorService executor = redisson.getExecutorService("test");
    ScheduledFuture<Long> future1 = executor.schedule(new ScheduledCallableTask(), 5, TimeUnit.SECONDS);
    ScheduledFuture<Long> future2 = executor.schedule(new ScheduledCallableTask(), 5, TimeUnit.SECONDS);
    ScheduledFuture<Long> future3 = executor.schedule(new ScheduledCallableTask(), 5, TimeUnit.SECONDS);
    
    node.shutdown();
    
    RedissonNodeConfig nodeConfig = new RedissonNodeConfig(redisson.getConfig());
    nodeConfig.setExecutorServiceWorkers(Collections.singletonMap("test", 1));
    node = RedissonNode.create(nodeConfig);
    node.start();

    assertThat(future1.get()).isEqualTo(100);
    assertThat(future2.get()).isEqualTo(100);
    assertThat(future3.get()).isEqualTo(100);
}
 
Example #5
Source File: RedissonExecutorServiceTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
public void testTaskFinishing() throws Exception {
    AtomicInteger counter = new AtomicInteger();
    new MockUp<TasksRunnerService>() {
        @Mock
        private void finish(Invocation invocation, String requestId) {
            if (counter.incrementAndGet() > 1) {
                invocation.proceed();
            }
        }
    };
    
    Config config = createConfig();
    RedissonNodeConfig nodeConfig = new RedissonNodeConfig(config);
    nodeConfig.setExecutorServiceWorkers(Collections.singletonMap("test2", 1));
    node.shutdown();
    node = RedissonNode.create(nodeConfig);
    node.start();
    
    RExecutorService executor = redisson.getExecutorService("test2");
    RExecutorFuture<?> f = executor.submit(new FailoverTask("finished"));
    Thread.sleep(2000);
    node.shutdown();

    f.get();
    assertThat(redisson.<Boolean>getBucket("finished").get()).isTrue();
}
 
Example #6
Source File: RedissonNode.java    From redisson with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    if (args.length == 0) {
        throw new IllegalArgumentException("Config file not defined");
    }
    
    String configPath = args[0];
    RedissonNodeConfig config = null;
    try {
        config = RedissonNodeConfig.fromJSON(new File(configPath));
    } catch (IOException e) {
        // trying next format
        try {
            config = RedissonNodeConfig.fromYAML(new File(configPath));
        } catch (IOException e1) {
            log.error("Can't parse json config " + configPath, e);
            throw new IllegalArgumentException("Can't parse yaml config " + configPath, e1);
        }
    }
    
    final RedissonNode node = RedissonNode.create(config);
    node.start();
    
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            node.shutdown();
        }
    });
}
 
Example #7
Source File: RedissonScheduledExecutorServiceTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Before
@Override
public void before() throws IOException, InterruptedException {
    super.before();
    Config config = createConfig();
    RedissonNodeConfig nodeConfig = new RedissonNodeConfig(config);
    nodeConfig.setExecutorServiceWorkers(Collections.singletonMap("test", 1));
    node = RedissonNode.create(nodeConfig);
    node.start();
}
 
Example #8
Source File: RedissonExecutorServiceTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Before
@Override
public void before() throws IOException, InterruptedException {
    super.before();
    Config config = createConfig();
    RedissonNodeConfig nodeConfig = new RedissonNodeConfig(config);
    nodeConfig.setExecutorServiceWorkers(Collections.singletonMap("test", 1));
    node = RedissonNode.create(nodeConfig);
    node.start();
}
 
Example #9
Source File: RedissonExecutorServiceSpringTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Bean(destroyMethod = "shutdown")
RedissonNode redissonNode(BeanFactory beanFactory) {
    Config config = BaseTest.createConfig();
    RedissonNodeConfig nodeConfig = new RedissonNodeConfig(config);
    nodeConfig.setExecutorServiceWorkers(Collections.singletonMap(EXECUTOR_NAME, 1));
    nodeConfig.setBeanFactory(beanFactory);
    RedissonNode node = RedissonNode.create(nodeConfig);
    node.start();
    return node;
}
 
Example #10
Source File: RedissonNode.java    From redisson with Apache License 2.0 4 votes vote down vote up
private RedissonNode(RedissonNodeConfig config, RedissonClient redisson) {
    this.config = new RedissonNodeConfig(config);
    this.id = generateId();
    this.redisson = redisson;
    hasRedissonInstance = redisson == null;
}
 
Example #11
Source File: RedissonExecutorServiceTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test
public void testFailoverInSentinel() throws Exception {
    RedisRunner.RedisProcess master = new RedisRunner()
            .nosave()
            .randomPort()
            .randomDir()
            .run();
    RedisRunner.RedisProcess slave1 = new RedisRunner()
            .port(6380)
            .nosave()
            .randomDir()
            .slaveof("127.0.0.1", master.getRedisServerPort())
            .run();
    RedisRunner.RedisProcess slave2 = new RedisRunner()
            .port(6381)
            .nosave()
            .randomDir()
            .slaveof("127.0.0.1", master.getRedisServerPort())
            .run();
    RedisRunner.RedisProcess sentinel1 = new RedisRunner()
            .nosave()
            .randomDir()
            .port(26379)
            .sentinel()
            .sentinelMonitor("myMaster", "127.0.0.1", master.getRedisServerPort(), 2)
            .run();
    RedisRunner.RedisProcess sentinel2 = new RedisRunner()
            .nosave()
            .randomDir()
            .port(26380)
            .sentinel()
            .sentinelMonitor("myMaster", "127.0.0.1", master.getRedisServerPort(), 2)
            .run();
    RedisRunner.RedisProcess sentinel3 = new RedisRunner()
            .nosave()
            .randomDir()
            .port(26381)
            .sentinel()
            .sentinelMonitor("myMaster", "127.0.0.1", master.getRedisServerPort(), 2)
            .run();
    
    Thread.sleep(5000); 
    
    Config config = new Config();
    config.useSentinelServers()
        .setLoadBalancer(new RandomLoadBalancer())
        .addSentinelAddress(sentinel3.getRedisServerAddressAndPort()).setMasterName("myMaster");
    
    RedissonClient redisson = Redisson.create(config);
    
    RedissonNodeConfig nodeConfig = new RedissonNodeConfig(config);
    nodeConfig.setExecutorServiceWorkers(Collections.singletonMap("test2", 1));
    node.shutdown();
    node = RedissonNode.create(nodeConfig);
    node.start();

    RExecutorService executor = redisson.getExecutorService("test2", ExecutorOptions.defaults().taskRetryInterval(10, TimeUnit.SECONDS));
    for (int i = 0; i < 10; i++) {
        executor.submit(new DelayedTask(2000, "counter"));
    }
    Thread.sleep(2500);
    assertThat(redisson.getAtomicLong("counter").get()).isEqualTo(1);

    master.stop();
    System.out.println("master " + master.getRedisServerAddressAndPort() + " stopped!");
    
    Thread.sleep(TimeUnit.SECONDS.toMillis(70));
    
    master = new RedisRunner()
            .port(master.getRedisServerPort())
            .nosave()
            .randomDir()
            .run();

    System.out.println("master " + master.getRedisServerAddressAndPort() + " started!");
    
    Thread.sleep(25000);
    
    assertThat(redisson.getAtomicLong("counter").get()).isEqualTo(10);
    
    redisson.shutdown();
    node.shutdown();
    sentinel1.stop();
    sentinel2.stop();
    sentinel3.stop();
    master.stop();
    slave1.stop();
    slave2.stop();
}
 
Example #12
Source File: RedissonExecutorServiceTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test
public void testNodeFailover() throws Exception {
    AtomicInteger counter = new AtomicInteger();
    new MockUp<TasksRunnerService>() {
        @Mock
        private void finish(Invocation invocation, String requestId, boolean removeTask) {
            if (counter.incrementAndGet() > 1) {
                invocation.proceed();
            } else {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    
    Config config = createConfig();
    RedissonNodeConfig nodeConfig = new RedissonNodeConfig(config);
    nodeConfig.setExecutorServiceWorkers(Collections.singletonMap("test2", 1));
    node.shutdown();
    node = RedissonNode.create(nodeConfig);
    node.start();
    
    
    RExecutorService executor = redisson.getExecutorService("test2", ExecutorOptions.defaults().taskRetryInterval(10, TimeUnit.SECONDS));
    RExecutorFuture<?> f = executor.submit(new IncrementRunnableTask("counter"));
    assertThat(executor.getTaskCount()).isEqualTo(1);
    Thread.sleep(1000);
    assertThat(redisson.getAtomicLong("counter").get()).isEqualTo(1);
    Thread.sleep(1000);
    System.out.println("shutdown");
    node.shutdown();

    assertThat(executor.getTaskCount()).isEqualTo(1);

    node = RedissonNode.create(nodeConfig);
    node.start();

    assertThat(executor.getTaskCount()).isEqualTo(1);

    Thread.sleep(8500);
    assertThat(executor.getTaskCount()).isEqualTo(0);
    assertThat(redisson.getAtomicLong("counter").get()).isEqualTo(2);

    Thread.sleep(16000);
    assertThat(executor.getTaskCount()).isEqualTo(0);
    assertThat(redisson.getAtomicLong("counter").get()).isEqualTo(2);

    redisson.getKeys().delete("counter");
    f.get();
    assertThat(redisson.getKeys().count()).isEqualTo(1);
}
 
Example #13
Source File: RedissonNode.java    From redisson with Apache License 2.0 2 votes vote down vote up
/**
 * Create Redisson node instance with provided config
 *
 * @param config of RedissonNode
 * @return RedissonNode instance
 */
public static RedissonNode create(RedissonNodeConfig config) {
    return create(config, null);
}
 
Example #14
Source File: RedissonNode.java    From redisson with Apache License 2.0 2 votes vote down vote up
/**
 * Create Redisson node instance with provided config and Redisson instance
 *
 * @param config of RedissonNode
 * @param redisson instance
 * @return RedissonNode instance
 */
public static RedissonNode create(RedissonNodeConfig config, RedissonClient redisson) {
    return new RedissonNode(config, redisson);
}