Search in sources :

Example 6 with RTopic

use of org.redisson.api.RTopic in project redisson by redisson.

the class RedissonSessionManager method startInternal.

@Override
protected void startInternal() throws LifecycleException {
    super.startInternal();
    redisson = buildClient();
    final ClassLoader applicationClassLoader;
    if (getContext().getLoader().getClassLoader() != null) {
        applicationClassLoader = getContext().getLoader().getClassLoader();
    } else if (Thread.currentThread().getContextClassLoader() != null) {
        applicationClassLoader = Thread.currentThread().getContextClassLoader();
    } else {
        applicationClassLoader = getClass().getClassLoader();
    }
    Codec codec = redisson.getConfig().getCodec();
    try {
        codecToUse = codec.getClass().getConstructor(ClassLoader.class, codec.getClass()).newInstance(applicationClassLoader, codec);
    } catch (Exception e) {
        throw new LifecycleException(e);
    }
    Pipeline pipeline = getContext().getPipeline();
    synchronized (pipeline) {
        if (readMode == ReadMode.REDIS) {
            Optional<Valve> res = Arrays.stream(pipeline.getValves()).filter(v -> v.getClass() == UsageValve.class).findAny();
            if (res.isPresent()) {
                ((UsageValve) res.get()).incUsage();
            } else {
                pipeline.addValve(new UsageValve());
            }
        }
        if (updateMode == UpdateMode.AFTER_REQUEST) {
            Optional<Valve> res = Arrays.stream(pipeline.getValves()).filter(v -> v.getClass() == UpdateValve.class).findAny();
            if (res.isPresent()) {
                ((UpdateValve) res.get()).incUsage();
            } else {
                pipeline.addValve(new UpdateValve());
            }
        }
    }
    if (readMode == ReadMode.MEMORY && this.broadcastSessionUpdates || broadcastSessionEvents) {
        RTopic updatesTopic = getTopic();
        messageListener = new MessageListener<AttributeMessage>() {

            @Override
            public void onMessage(CharSequence channel, AttributeMessage msg) {
                try {
                    if (msg.getNodeId().equals(nodeId)) {
                        return;
                    }
                    RedissonSession session = (RedissonSession) RedissonSessionManager.super.findSession(msg.getSessionId());
                    if (session != null) {
                        if (msg instanceof SessionDestroyedMessage) {
                            session.expire();
                        }
                        if (msg instanceof AttributeRemoveMessage) {
                            for (String name : ((AttributeRemoveMessage) msg).getNames()) {
                                session.superRemoveAttributeInternal(name, true);
                            }
                        }
                        if (msg instanceof AttributesClearMessage) {
                            RedissonSessionManager.super.remove(session, false);
                        }
                        if (msg instanceof AttributesPutAllMessage) {
                            AttributesPutAllMessage m = (AttributesPutAllMessage) msg;
                            Map<String, Object> attrs = m.getAttrs(codecToUse.getMapValueDecoder());
                            session.load(attrs);
                        }
                        if (msg instanceof AttributeUpdateMessage) {
                            AttributeUpdateMessage m = (AttributeUpdateMessage) msg;
                            session.superSetAttribute(m.getName(), m.getValue(codecToUse.getMapValueDecoder()), true);
                        }
                    } else {
                        if (msg instanceof SessionCreatedMessage) {
                            Session s = findSession(msg.getSessionId());
                            if (s == null) {
                                throw new IllegalStateException("Unable to find session: " + msg.getSessionId());
                            }
                        }
                        if (msg instanceof SessionDestroyedMessage) {
                            Session s = findSession(msg.getSessionId(), false);
                            if (s == null) {
                                throw new IllegalStateException("Unable to find session: " + msg.getSessionId());
                            }
                            s.expire();
                            RSet<String> set = getNotifiedNodes(msg.getSessionId());
                            set.add(nodeId);
                        }
                    }
                } catch (Exception e) {
                    log.error("Unable to handle topic message", e);
                }
            }
        };
        updatesTopic.addListener(AttributeMessage.class, messageListener);
    }
    setState(LifecycleState.STARTING);
}
Also used : HttpSession(javax.servlet.http.HttpSession) java.util(java.util) Codec(org.redisson.client.codec.Codec) StringCodec(org.redisson.client.codec.StringCodec) ManagerBase(org.apache.catalina.session.ManagerBase) Config(org.redisson.config.Config) RTopic(org.redisson.api.RTopic) RSet(org.redisson.api.RSet) IOException(java.io.IOException) Log(org.apache.juli.logging.Log) LogFactory(org.apache.juli.logging.LogFactory) File(java.io.File) Redisson(org.redisson.Redisson) RMap(org.redisson.api.RMap) CompositeCodec(org.redisson.codec.CompositeCodec) org.apache.catalina(org.apache.catalina) RedissonClient(org.redisson.api.RedissonClient) MessageListener(org.redisson.api.listener.MessageListener) Codec(org.redisson.client.codec.Codec) StringCodec(org.redisson.client.codec.StringCodec) CompositeCodec(org.redisson.codec.CompositeCodec) RSet(org.redisson.api.RSet) IOException(java.io.IOException) RTopic(org.redisson.api.RTopic) RMap(org.redisson.api.RMap) HttpSession(javax.servlet.http.HttpSession)

Example 7 with RTopic

use of org.redisson.api.RTopic in project netty-socketio by mrniko.

the class RedissonPubSubStore method subscribe.

@Override
public <T extends PubSubMessage> void subscribe(PubSubType type, final PubSubListener<T> listener, Class<T> clazz) {
    String name = type.toString();
    RTopic topic = redissonSub.getTopic(name);
    int regId = topic.addListener(PubSubMessage.class, new MessageListener<PubSubMessage>() {

        @Override
        public void onMessage(CharSequence channel, PubSubMessage msg) {
            if (!nodeId.equals(msg.getNodeId())) {
                listener.onMessage((T) msg);
            }
        }
    });
    Queue<Integer> list = map.get(name);
    if (list == null) {
        list = new ConcurrentLinkedQueue<Integer>();
        Queue<Integer> oldList = map.putIfAbsent(name, list);
        if (oldList != null) {
            list = oldList;
        }
    }
    list.add(regId);
}
Also used : PubSubMessage(com.corundumstudio.socketio.store.pubsub.PubSubMessage) RTopic(org.redisson.api.RTopic)

Example 8 with RTopic

use of org.redisson.api.RTopic in project redisson by redisson.

the class RedissonTopicPatternTest method testLazyUnsubscribe.

@Test
public void testLazyUnsubscribe() throws InterruptedException {
    final CountDownLatch messageRecieved = new CountDownLatch(1);
    RedissonClient redisson1 = BaseTest.createInstance();
    RPatternTopic topic1 = redisson1.getPatternTopic("topic.*");
    int listenerId = topic1.addListener(Message.class, (pattern, channel, msg) -> {
        Assertions.fail();
    });
    Thread.sleep(1000);
    topic1.removeListener(listenerId);
    Thread.sleep(1000);
    RedissonClient redisson2 = BaseTest.createInstance();
    RPatternTopic topic2 = redisson2.getPatternTopic("topic.*");
    topic2.addListener(Message.class, (pattern, channel, msg) -> {
        Assertions.assertTrue(pattern.equals("topic.*"));
        Assertions.assertTrue(channel.equals("topic.t1"));
        Assertions.assertEquals(new Message("123"), msg);
        messageRecieved.countDown();
    });
    RTopic topic3 = redisson2.getTopic("topic.t1");
    topic3.publish(new Message("123"));
    Assertions.assertTrue(messageRecieved.await(5, TimeUnit.SECONDS));
    redisson1.shutdown();
    redisson2.shutdown();
}
Also used : RPatternTopic(org.redisson.api.RPatternTopic) RedissonClient(org.redisson.api.RedissonClient) CountDownLatch(java.util.concurrent.CountDownLatch) RTopic(org.redisson.api.RTopic) Test(org.junit.jupiter.api.Test)

Example 9 with RTopic

use of org.redisson.api.RTopic in project redisson by redisson.

the class RedissonTopicPatternTest method test.

@Test
public void test() throws InterruptedException {
    final CountDownLatch messageRecieved = new CountDownLatch(5);
    final CountDownLatch statusRecieved = new CountDownLatch(1);
    RedissonClient redisson1 = BaseTest.createInstance();
    RPatternTopic topic1 = redisson1.getPatternTopic("topic.*");
    topic1.addListener(new BasePatternStatusListener() {

        @Override
        public void onPSubscribe(String pattern) {
            Assertions.assertEquals("topic.*", pattern);
            statusRecieved.countDown();
        }
    });
    topic1.addListener(Message.class, (pattern, channel, msg) -> {
        Assertions.assertEquals(new Message("123"), msg);
        messageRecieved.countDown();
    });
    RedissonClient redisson2 = BaseTest.createInstance();
    RTopic topic2 = redisson2.getTopic("topic.t1");
    topic2.addListener(Message.class, (channel, msg) -> {
        Assertions.assertEquals(new Message("123"), msg);
        messageRecieved.countDown();
    });
    topic2.publish(new Message("123"));
    topic2.publish(new Message("123"));
    RTopic topicz = redisson2.getTopic("topicz.t1");
    // this message doesn't get
    topicz.publish(new Message("789"));
    // delivered, and would fail the
    // assertion
    RTopic topict2 = redisson2.getTopic("topic.t2");
    topict2.publish(new Message("123"));
    statusRecieved.await();
    Assertions.assertTrue(messageRecieved.await(5, TimeUnit.SECONDS));
    redisson1.shutdown();
    redisson2.shutdown();
}
Also used : RPatternTopic(org.redisson.api.RPatternTopic) RedissonClient(org.redisson.api.RedissonClient) BasePatternStatusListener(org.redisson.api.listener.BasePatternStatusListener) CountDownLatch(java.util.concurrent.CountDownLatch) RTopic(org.redisson.api.RTopic) Test(org.junit.jupiter.api.Test)

Example 10 with RTopic

use of org.redisson.api.RTopic in project redisson by redisson.

the class RedissonTopicPatternTest method testListenerRemove.

@Test
public void testListenerRemove() throws InterruptedException {
    RedissonClient redisson1 = BaseTest.createInstance();
    RPatternTopic topic1 = redisson1.getPatternTopic("topic.*");
    final CountDownLatch l = new CountDownLatch(1);
    topic1.addListener(new BasePatternStatusListener() {

        @Override
        public void onPUnsubscribe(String pattern) {
            Assertions.assertEquals("topic.*", pattern);
            l.countDown();
        }
    });
    int id = topic1.addListener(Message.class, (pattern, channel, msg) -> {
        Assertions.fail();
    });
    RedissonClient redisson2 = BaseTest.createInstance();
    RTopic topic2 = redisson2.getTopic("topic.t1");
    topic1.removeListener(id);
    topic2.publish(new Message("123"));
    redisson1.shutdown();
    redisson2.shutdown();
}
Also used : RPatternTopic(org.redisson.api.RPatternTopic) RedissonClient(org.redisson.api.RedissonClient) BasePatternStatusListener(org.redisson.api.listener.BasePatternStatusListener) CountDownLatch(java.util.concurrent.CountDownLatch) RTopic(org.redisson.api.RTopic) Test(org.junit.jupiter.api.Test)

Aggregations

RTopic (org.redisson.api.RTopic)10 RedissonClient (org.redisson.api.RedissonClient)5 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.jupiter.api.Test)3 RPatternTopic (org.redisson.api.RPatternTopic)3 MessageListener (org.redisson.api.listener.MessageListener)3 File (java.io.File)2 IOException (java.io.IOException)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 BasePatternStatusListener (org.redisson.api.listener.BasePatternStatusListener)2 Codec (org.redisson.client.codec.Codec)2 StringCodec (org.redisson.client.codec.StringCodec)2