use of org.redisson.api.listener.MessageListener in project redisson by redisson.
the class QueueTransferTask method start.
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);
}
});
}
use of org.redisson.api.listener.MessageListener in project redisson by redisson.
the class RedissonReliableTopic method poll.
private void poll(String id, StreamMessageId startId) {
readFuture = commandExecutor.readAsync(getRawName(), new CompositeCodec(StringCodec.INSTANCE, codec), RedisCommands.XREAD_BLOCKING_SINGLE, "BLOCK", 0, "STREAMS", getRawName(), startId);
readFuture.whenComplete((res, ex) -> {
if (readFuture.isCancelled()) {
return;
}
if (ex != null) {
if (ex instanceof RedissonShutdownException) {
return;
}
log.error(ex.getMessage(), ex);
commandExecutor.getConnectionManager().newTimeout(task -> {
poll(id, startId);
}, 1, TimeUnit.SECONDS);
return;
}
commandExecutor.getConnectionManager().getExecutor().execute(() -> {
res.values().forEach(entry -> {
Object m = entry.get("m");
listeners.values().forEach(e -> {
if (e.getType().isInstance(m)) {
((MessageListener<Object>) e.getListener()).onMessage(getRawName(), m);
}
});
});
});
if (listeners.isEmpty()) {
return;
}
StreamMessageId lastId = res.keySet().stream().skip(res.size() - 1).findFirst().get();
long time = System.currentTimeMillis();
RFuture<Boolean> updateFuture = commandExecutor.evalWriteAsync(getRawName(), StringCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, "local r = redis.call('zscore', KEYS[2], ARGV[2]); " + "if r ~= false then " + "local value = redis.call('incr', KEYS[4]); " + "redis.call('zadd', KEYS[2], value, ARGV[2]); " + "redis.call('hset', KEYS[3], ARGV[2], ARGV[1]); " + "end; " + "local t = redis.call('zrange', KEYS[5], 0, 0, 'WITHSCORES'); " + "if tonumber(t[2]) < tonumber(ARGV[3]) then " + "redis.call('hdel', KEYS[3], t[1]); " + "redis.call('zrem', KEYS[2], t[1]); " + "redis.call('zrem', KEYS[5], t[1]); " + "end; " + "local v = redis.call('zrange', KEYS[2], 0, 0); " + "local score = redis.call('hget', KEYS[3], v[1]); " + "local range = redis.call('xrange', KEYS[1], score, '+'); " + "if #range == 0 then " + "redis.call('del', KEYS[1]); " + "elseif #range == 1 and range[1][1] == score then " + "redis.call('del', KEYS[1]); " + "else " + "redis.call('xtrim', KEYS[1], 'maxlen', #range); " + "end;" + "return r ~= false; ", Arrays.asList(getRawName(), getSubscribersName(), getMapName(), getCounter(), getTimeout()), lastId, id, time);
updateFuture.whenComplete((re, exc) -> {
if (exc != null) {
if (exc instanceof RedissonShutdownException) {
return;
}
log.error("Unable to update subscriber status", exc);
return;
}
if (!re || listeners.isEmpty()) {
return;
}
poll(id, lastId);
});
});
}
use of org.redisson.api.listener.MessageListener in project redisson by redisson.
the class RedissonTopicRx method getMessages.
public <M> Flowable<M> getMessages(Class<M> type) {
ReplayProcessor<M> p = ReplayProcessor.create();
return p.doOnRequest(new LongConsumer() {
@Override
public void accept(long n) throws Exception {
AtomicLong counter = new AtomicLong(n);
RFuture<Integer> t = topic.addListenerAsync(type, new MessageListener<M>() {
@Override
public void onMessage(CharSequence channel, M msg) {
p.onNext(msg);
if (counter.decrementAndGet() == 0) {
topic.removeListenerAsync(this);
p.onComplete();
}
}
});
t.whenComplete((id, e) -> {
if (e != null) {
p.onError(e);
return;
}
p.doOnCancel(new Action() {
@Override
public void run() throws Exception {
topic.removeListenerAsync(id);
}
});
});
}
});
}
use of org.redisson.api.listener.MessageListener in project redisson by redisson.
the class RedissonTopicReactiveTest method testRemoveListenerById.
@Test
public void testRemoveListenerById() throws InterruptedException {
RTopicReactive topic1 = redisson.getTopic("topic1");
MessageListener listener = new MessageListener() {
@Override
public void onMessage(CharSequence channel, Object msg) {
Assertions.fail();
}
};
Mono<Integer> res = topic1.addListener(Message.class, listener);
Integer listenerId = res.block();
topic1 = redisson.getTopic("topic1");
topic1.removeListener(listenerId);
topic1.publish(new Message("123"));
}
use of org.redisson.api.listener.MessageListener in project redisson by redisson.
the class RedissonTopicRxTest method testRemoveListenerByInstance.
@Test
public void testRemoveListenerByInstance() throws InterruptedException {
RTopicRx topic1 = redisson.getTopic("topic1");
MessageListener listener = new MessageListener() {
@Override
public void onMessage(CharSequence channel, Object msg) {
Assertions.fail();
}
};
topic1.addListener(Message.class, listener);
topic1 = redisson.getTopic("topic1");
topic1.removeListener(listener);
topic1.publish(new Message("123"));
}
Aggregations