Java源码示例:org.redisson.config.RedissonNodeConfig
示例1
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();
}
示例2
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();
}
示例3
@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();
}
示例4
@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);
}
示例5
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();
}
示例6
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();
}
});
}
示例7
@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();
}
示例8
@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();
}
示例9
@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;
}
示例10
private RedissonNode(RedissonNodeConfig config, RedissonClient redisson) {
this.config = new RedissonNodeConfig(config);
this.id = generateId();
this.redisson = redisson;
hasRedissonInstance = redisson == null;
}
示例11
@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();
}
示例12
@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);
}
示例13
/**
* Create Redisson node instance with provided config
*
* @param config of RedissonNode
* @return RedissonNode instance
*/
public static RedissonNode create(RedissonNodeConfig config) {
return create(config, null);
}
示例14
/**
* 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);
}