public void start() { RTopic schedulerTopic = getTopic(); statusListenerId = schedulerTopic.addListener(new BaseStatusListener() { @Override public void onSubscribe(String channel) { pushTask(); } }); messageListenerId = schedulerTopic.addListener(Long.class, new MessageListener<Long>() { @Override public void onMessage(CharSequence channel, Long startTime) { scheduleTask(startTime); } }); }
@Override public void setCreationTime(long time) { super.setCreationTime(time); if (map != null) { Map<String, Object> newMap = new HashMap<String, Object>(3); newMap.put(CREATION_TIME_ATTR, creationTime); newMap.put(LAST_ACCESSED_TIME_ATTR, lastAccessedTime); newMap.put(THIS_ACCESSED_TIME_ATTR, thisAccessedTime); map.putAll(newMap); if (readMode == ReadMode.MEMORY) { topic.publish(createPutAllMessage(newMap)); } } }
@Override public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { if (isTerminated()) { return true; } final CountDownLatch latch = new CountDownLatch(1); MessageListener<Long> listener = new MessageListener<Long>() { @Override public void onMessage(CharSequence channel, Long msg) { if (msg == TERMINATED_STATE) { latch.countDown(); } } }; int listenerId = terminationTopic.addListener(Long.class, listener); if (isTerminated()) { terminationTopic.removeListener(listenerId); return true; } boolean res = latch.await(timeout, unit); terminationTopic.removeListener(listenerId); return res; }
@Override public void shutdown() { queueTransferService.remove(getName()); remoteService.deregister(RemoteExecutorService.class); workersTopic.removeListener(workersGroupListenerId); commandExecutor.evalWrite(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_VOID, "if redis.call('exists', KEYS[2]) == 0 then " + "if redis.call('get', KEYS[1]) == '0' or redis.call('exists', KEYS[1]) == 0 then " + "redis.call('set', KEYS[2], ARGV[2]);" + "redis.call('publish', KEYS[3], ARGV[2]);" + "else " + "redis.call('set', KEYS[2], ARGV[1]);" + "end;" + "end;", Arrays.<Object>asList(tasksCounterName, statusName, terminationTopic.getChannelNames().get(0), tasksRetryIntervalName), SHUTDOWN_STATE, TERMINATED_STATE); }
public static void main(String[] args) throws InterruptedException { // connects to 127.0.0.1:6379 by default RedissonClient redisson = Redisson.create(); CountDownLatch latch = new CountDownLatch(1); RTopic topic = redisson.getTopic("topic2"); topic.addListener(String.class, new MessageListener<String>() { @Override public void onMessage(CharSequence channel, String msg) { latch.countDown(); } }); topic.publish("msg"); latch.await(); redisson.shutdown(); }
service.setTasksCounterName(tasksCounterName); service.setTasksName(tasksName); service.setTerminationTopicName(terminationTopic.getChannelNames().get(0)); service.setSchedulerChannelName(schedulerChannelName); service.setSchedulerQueueName(schedulerQueueName); workersGroupListenerId = workersTopic.addListener(String.class, new MessageListener<String>() { @Override public void onMessage(CharSequence channel, String id) {
commandExecutor, RedissonObject.suffixName(entry.getKey().getName(), requestId + RedissonLocalCachedMap.DISABLED_ACK_SUFFIX)); topics.add(topic); topic.addListener(Object.class, new MessageListener<Object>() { @Override public void onMessage(CharSequence channel, Object msg) { topic.removeAllListeners();
public void destroy() { topic.removeListener(listenerId); }
public RFuture<T> sumAsync() { final RPromise<T> result = new RedissonPromise<T>(); RFuture<Long> future = topic.publishAsync(SUM_MSG); future.addListener(new SumListener(result)); return result; }
public <M> Flux<M> getMessages(Class<M> type) { return Flux.<M>create(emitter -> { emitter.onRequest(n -> { AtomicLong counter = new AtomicLong(n); RFuture<Integer> t = topic.addListenerAsync(type, new MessageListener<M>() { @Override public void onMessage(CharSequence channel, M msg) { emitter.next(msg); if (counter.decrementAndGet() == 0) { topic.removeListenerAsync(this); emitter.complete(); } } }); t.whenComplete((id, e) -> { if (e != null) { emitter.error(e); return; } emitter.onDispose(() -> { topic.removeListenerAsync(id); }); }); }); }); }
@Override public void accept(long n) throws Exception { final AtomicLong counter = new AtomicLong(n); RFuture<Integer> t = topic.addListenerAsync(type, new MessageListener<M>() { @Override public void onMessage(CharSequence channel, M msg) {
service.setTasksCounterName(tasksCounterName); service.setTasksName(tasksName); service.setTerminationTopicName(terminationTopic.getChannelNames().get(0)); service.setSchedulerChannelName(schedulerChannelName); service.setSchedulerQueueName(schedulerQueueName); workersGroupListenerId = workersTopic.addListener(String.class, new MessageListener<String>() { @Override public void onMessage(CharSequence channel, String id) {
commandExecutor, RedissonObject.suffixName(entry.getKey().getName(), requestId + RedissonLocalCachedMap.DISABLED_ACK_SUFFIX)); topics.add(topic); topic.addListener(Object.class, new MessageListener<Object>() { @Override public void onMessage(CharSequence channel, Object msg) { topic.removeAllListeners();
public void destroy() { topic.removeListener(listenerId); }
public RFuture<Void> resetAsync(final long timeout, final TimeUnit timeUnit) { final RPromise<Void> result = new RedissonPromise<Void>(); RFuture<Long> future = topic.publishAsync(CLEAR_MSG); future.addListener(new ResetListener(result) { @Override protected RFuture<Void> acquireAsync(int value) { return tryAcquire(timeout, timeUnit, value); } }); return result; }
@Override public void shutdown() { queueTransferService.remove(getName()); remoteService.deregister(RemoteExecutorService.class); workersTopic.removeListener(workersGroupListenerId); commandExecutor.evalWrite(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_VOID, "if redis.call('exists', KEYS[2]) == 0 then " + "if redis.call('get', KEYS[1]) == '0' or redis.call('exists', KEYS[1]) == 0 then " + "redis.call('set', KEYS[2], ARGV[2]);" + "redis.call('publish', KEYS[3], ARGV[2]);" + "else " + "redis.call('set', KEYS[2], ARGV[1]);" + "end;" + "end;", Arrays.<Object>asList(tasksCounterName, statusName, terminationTopic.getChannelNames().get(0), tasksRetryIntervalName), SHUTDOWN_STATE, TERMINATED_STATE); }
@Override public void accept(long n) throws Exception { final AtomicLong counter = new AtomicLong(n); RFuture<Integer> t = topic.addListenerAsync(type, new MessageListener<M>() { @Override public void onMessage(CharSequence channel, M msg) {
@Override public void setCreationTime(long time) { super.setCreationTime(time); if (map != null) { Map<String, Object> newMap = new HashMap<String, Object>(3); newMap.put(CREATION_TIME_ATTR, creationTime); newMap.put(LAST_ACCESSED_TIME_ATTR, lastAccessedTime); newMap.put(THIS_ACCESSED_TIME_ATTR, thisAccessedTime); map.putAll(newMap); if (readMode == ReadMode.MEMORY) { topic.publish(createPutAllMessage(newMap)); } } }
public void start() { RTopic schedulerTopic = getTopic(); statusListenerId = schedulerTopic.addListener(new BaseStatusListener() { @Override public void onSubscribe(String channel) { pushTask(); } }); messageListenerId = schedulerTopic.addListener(Long.class, new MessageListener<Long>() { @Override public void onMessage(CharSequence channel, Long startTime) { scheduleTask(startTime); } }); }
@Override public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { if (isTerminated()) { return true; } final CountDownLatch latch = new CountDownLatch(1); MessageListener<Long> listener = new MessageListener<Long>() { @Override public void onMessage(CharSequence channel, Long msg) { if (msg == TERMINATED_STATE) { latch.countDown(); } } }; int listenerId = terminationTopic.addListener(Long.class, listener); if (isTerminated()) { terminationTopic.removeListener(listenerId); return true; } boolean res = latch.await(timeout, unit); terminationTopic.removeListener(listenerId); return res; }