001
014
015 package com.liferay.portal.fabric.netty.handlers;
016
017 import com.liferay.portal.fabric.netty.rpc.RPCUtil;
018 import com.liferay.portal.kernel.concurrent.AsyncBroker;
019
020 import io.netty.channel.Channel;
021 import io.netty.util.Attribute;
022 import io.netty.util.AttributeKey;
023
024 import java.io.Serializable;
025
026 import java.util.concurrent.atomic.AtomicLong;
027
028
031 public class NettyChannelAttributes {
032
033 public static <T extends Serializable> AsyncBroker<Long, T> getAsyncBroker(
034 Channel channel) {
035
036 Attribute<AsyncBroker<Long, Serializable>> attribute = channel.attr(
037 _asyncBrokerKey);
038
039 AsyncBroker<Long, Serializable> asyncBroker = attribute.get();
040
041 if (asyncBroker == null) {
042 asyncBroker = new AsyncBroker<Long, Serializable>();
043
044 AsyncBroker<Long, Serializable> previousAsyncBroker =
045 attribute.setIfAbsent(asyncBroker);
046
047 if (previousAsyncBroker != null) {
048 asyncBroker = previousAsyncBroker;
049 }
050 }
051
052 return (AsyncBroker<Long, T>)asyncBroker;
053 }
054
055 public static long nextId(Channel channel) {
056 Attribute<AtomicLong> attribute = channel.attr(_idGeneratorKey);
057
058 AtomicLong attachmentIdGenerator = attribute.get();
059
060 if (attachmentIdGenerator == null) {
061 attachmentIdGenerator = new AtomicLong();
062
063 AtomicLong previousAttachmentIdGenerator = attribute.setIfAbsent(
064 attachmentIdGenerator);
065
066 if (previousAttachmentIdGenerator != null) {
067 attachmentIdGenerator = previousAttachmentIdGenerator;
068 }
069 }
070
071 return attachmentIdGenerator.getAndIncrement();
072 }
073
074 private static final AttributeKey<AsyncBroker<Long, Serializable>>
075 _asyncBrokerKey = AttributeKey.valueOf(
076 RPCUtil.class.getName() + "-AsyncBroker");
077 private static final AttributeKey<AtomicLong> _idGeneratorKey =
078 AttributeKey.valueOf(RPCUtil.class.getName() + "-IdGenerator");
079
080 }