Java Code Examples for io.netty.util.Attribute#setIfAbsent()

The following examples show how to use io.netty.util.Attribute#setIfAbsent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompatibleObjectEncoder.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
    Attribute<ObjectOutputStream> oosAttr = ctx.attr(OOS);
    ObjectOutputStream oos = oosAttr.get();
    if (oos == null) {
        oos = newObjectOutputStream(new ByteBufOutputStream(out));
        ObjectOutputStream newOos = oosAttr.setIfAbsent(oos);
        if (newOos != null) {
            oos = newOos;
        }
    }

    synchronized (oos) {
        if (resetInterval != 0) {
            // Resetting will prevent OOM on the receiving side.
            writtenObjects ++;
            if (writtenObjects % resetInterval == 0) {
                oos.reset();
            }
        }

        oos.writeObject(msg);
        oos.flush();
    }
}
 
Example 2
Source File: HttpKeyEvaluationContext.java    From karyon with Apache License 2.0 6 votes vote down vote up
private QueryStringDecoder getOrCreateQueryStringDecoder(HttpServerRequest<?> request) {
    if (null == request) {
        throw new NullPointerException("Request can not be null.");
    }
    String uri = request.getUri();
    if (null == uri) {
        return null;
    }

    Attribute<QueryStringDecoder> queryDecoderAttr = channel.attr(queryDecoderKey);

    QueryStringDecoder _queryStringDecoder = queryDecoderAttr.get();

    if (null == _queryStringDecoder) {
        _queryStringDecoder = new QueryStringDecoder(uri);
        queryDecoderAttr.setIfAbsent(_queryStringDecoder);
    }
    return _queryStringDecoder;
}
 
Example 3
Source File: HttpKeyEvaluationContext.java    From karyon with Apache License 2.0 6 votes vote down vote up
public static QueryStringDecoder getOrCreateQueryStringDecoder(HttpServerRequest<?> request,
                                                               ChannelHandlerContext channelHandlerContext) {
    if (null == request) {
        throw new NullPointerException("Request can not be null.");
    }

    String uri = request.getUri();
    if (null == uri) {
        return null;
    }

    Attribute<QueryStringDecoder> queryDecoderAttr = channelHandlerContext.attr(queryDecoderKey);

    QueryStringDecoder _queryStringDecoder = queryDecoderAttr.get();

    if (null == _queryStringDecoder) {
        _queryStringDecoder = new QueryStringDecoder(uri);
        queryDecoderAttr.setIfAbsent(_queryStringDecoder);
    }
    return _queryStringDecoder;
}
 
Example 4
Source File: NettyHelper.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
public static AbstractChannel getChannel(ChannelHandlerContext ctx) {
    Attribute<AbstractChannel> attr = ctx.channel().attr(CHANNEL_KEY);
    AbstractChannel sofaChannel = attr.get();
    if (sofaChannel == null) {
        sofaChannel = new NettyChannel(ctx);
        AbstractChannel old = attr.setIfAbsent(sofaChannel);
        if (old != null) {
            sofaChannel = old;
        }
    }
    return sofaChannel;
}
 
Example 5
Source File: HighPerformanceChannelWriter.java    From sailfish with Apache License 2.0 5 votes vote down vote up
private static HighPerformanceChannelWriter getWriter(Channel channel) {
	Attribute<HighPerformanceChannelWriter> attr = channel.attr(ChannelAttrKeys.highPerformanceWriter);
	HighPerformanceChannelWriter writer = attr.get();
	if (null == writer) {
		HighPerformanceChannelWriter old = attr.setIfAbsent(writer = new HighPerformanceChannelWriter(channel));
		if (null != old) {
			writer = old;
		}
	}
	return writer;
}
 
Example 6
Source File: DefaultRegistry.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
private static boolean attachPublishEventOnChannel(RegisterMeta meta, Channel channel) {
    Attribute<ConcurrentSet<RegisterMeta>> attr = channel.attr(C_PUBLISH_KEY);
    ConcurrentSet<RegisterMeta> registerMetaSet = attr.get();
    if (registerMetaSet == null) {
        ConcurrentSet<RegisterMeta> newRegisterMetaSet = new ConcurrentSet<>();
        registerMetaSet = attr.setIfAbsent(newRegisterMetaSet);
        if (registerMetaSet == null) {
            registerMetaSet = newRegisterMetaSet;
        }
    }

    return registerMetaSet.add(meta);
}
 
Example 7
Source File: DefaultRegistry.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
private static boolean attachSubscribeEventOnChannel(RegisterMeta.ServiceMeta serviceMeta, Channel channel) {
    Attribute<ConcurrentSet<RegisterMeta.ServiceMeta>> attr = channel.attr(C_SUBSCRIBE_KEY);
    ConcurrentSet<RegisterMeta.ServiceMeta> serviceMetaSet = attr.get();
    if (serviceMetaSet == null) {
        ConcurrentSet<RegisterMeta.ServiceMeta> newServiceMetaSet = new ConcurrentSet<>();
        serviceMetaSet = attr.setIfAbsent(newServiceMetaSet);
        if (serviceMetaSet == null) {
            serviceMetaSet = newServiceMetaSet;
        }
    }

    return serviceMetaSet.add(serviceMeta);
}
 
Example 8
Source File: DefaultRegistryServer.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
private static boolean attachPublishEventOnChannel(RegisterMeta meta, Channel channel) {
    Attribute<ConcurrentSet<RegisterMeta>> attr = channel.attr(S_PUBLISH_KEY);
    ConcurrentSet<RegisterMeta> registerMetaSet = attr.get();
    if (registerMetaSet == null) {
        ConcurrentSet<RegisterMeta> newRegisterMetaSet = new ConcurrentSet<>();
        registerMetaSet = attr.setIfAbsent(newRegisterMetaSet);
        if (registerMetaSet == null) {
            registerMetaSet = newRegisterMetaSet;
        }
    }

    return registerMetaSet.add(meta);
}
 
Example 9
Source File: DefaultRegistryServer.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
private static boolean attachPublishCancelEventOnChannel(RegisterMeta meta, Channel channel) {
    Attribute<ConcurrentSet<RegisterMeta>> attr = channel.attr(S_PUBLISH_KEY);
    ConcurrentSet<RegisterMeta> registerMetaSet = attr.get();
    if (registerMetaSet == null) {
        ConcurrentSet<RegisterMeta> newRegisterMetaSet = new ConcurrentSet<>();
        registerMetaSet = attr.setIfAbsent(newRegisterMetaSet);
        if (registerMetaSet == null) {
            registerMetaSet = newRegisterMetaSet;
        }
    }

    return registerMetaSet.remove(meta);
}
 
Example 10
Source File: DefaultRegistryServer.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
private static boolean attachSubscribeEventOnChannel(RegisterMeta.ServiceMeta serviceMeta, Channel channel) {
    Attribute<ConcurrentSet<RegisterMeta.ServiceMeta>> attr = channel.attr(S_SUBSCRIBE_KEY);
    ConcurrentSet<RegisterMeta.ServiceMeta> serviceMetaSet = attr.get();
    if (serviceMetaSet == null) {
        ConcurrentSet<RegisterMeta.ServiceMeta> newServiceMetaSet = new ConcurrentSet<>();
        serviceMetaSet = attr.setIfAbsent(newServiceMetaSet);
        if (serviceMetaSet == null) {
            serviceMetaSet = newServiceMetaSet;
        }
    }

    return serviceMetaSet.add(serviceMeta);
}
 
Example 11
Source File: NettyChannel.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@link NettyChannel} for given {@link Channel}, this method never return null.
 */
public static NettyChannel attachChannel(Channel channel) {
    Attribute<NettyChannel> attr = channel.attr(NETTY_CHANNEL_KEY);
    NettyChannel nChannel = attr.get();
    if (nChannel == null) {
        NettyChannel newNChannel = new NettyChannel(channel);
        nChannel = attr.setIfAbsent(newNChannel);
        if (nChannel == null) {
            nChannel = newNChannel;
        }
    }
    return nChannel;
}