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);
}
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);
}
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();
}
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();
}
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();
}
Aggregations