Search in sources :

Example 1 with MessageListener

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);
        }
    });
}
Also used : MessageListener(org.redisson.api.listener.MessageListener) BaseStatusListener(org.redisson.api.listener.BaseStatusListener) RTopic(org.redisson.api.RTopic)

Example 2 with MessageListener

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);
        });
    });
}
Also used : CompositeCodec(org.redisson.codec.CompositeCodec) MessageListener(org.redisson.api.listener.MessageListener) StreamMessageId(org.redisson.api.StreamMessageId)

Example 3 with MessageListener

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);
                    }
                });
            });
        }
    });
}
Also used : RFuture(org.redisson.api.RFuture) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flowable(io.reactivex.rxjava3.core.Flowable) RTopic(org.redisson.api.RTopic) MessageListener(org.redisson.api.listener.MessageListener) LongConsumer(io.reactivex.rxjava3.functions.LongConsumer) Action(io.reactivex.rxjava3.functions.Action) ReplayProcessor(io.reactivex.rxjava3.processors.ReplayProcessor) LongConsumer(io.reactivex.rxjava3.functions.LongConsumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Action(io.reactivex.rxjava3.functions.Action) MessageListener(org.redisson.api.listener.MessageListener) RFuture(org.redisson.api.RFuture)

Example 4 with MessageListener

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"));
}
Also used : MessageListener(org.redisson.api.listener.MessageListener) RTopicReactive(org.redisson.api.RTopicReactive) Test(org.junit.jupiter.api.Test)

Example 5 with MessageListener

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"));
}
Also used : RTopicRx(org.redisson.api.RTopicRx) MessageListener(org.redisson.api.listener.MessageListener) Test(org.junit.jupiter.api.Test)

Aggregations

MessageListener (org.redisson.api.listener.MessageListener)12 Test (org.junit.jupiter.api.Test)4 RTopic (org.redisson.api.RTopic)4 IOException (java.io.IOException)3 RedissonClient (org.redisson.api.RedissonClient)3 CompositeCodec (org.redisson.codec.CompositeCodec)3 File (java.io.File)2 java.util (java.util)2 HttpSession (javax.servlet.http.HttpSession)2 org.apache.catalina (org.apache.catalina)2 ManagerBase (org.apache.catalina.session.ManagerBase)2 Log (org.apache.juli.logging.Log)2 LogFactory (org.apache.juli.logging.LogFactory)2 Redisson (org.redisson.Redisson)2 RMap (org.redisson.api.RMap)2 RSet (org.redisson.api.RSet)2 RTopicReactive (org.redisson.api.RTopicReactive)2 RTopicRx (org.redisson.api.RTopicRx)2 BaseStatusListener (org.redisson.api.listener.BaseStatusListener)2 Codec (org.redisson.client.codec.Codec)2