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

The following examples show how to use io.netty.util.Attribute#compareAndSet() . 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: NettyChannel.java    From joyrpc with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T getAttribute(final String key, final Function<String, T> function) {
    if (key == null) {
        return null;
    }
    Attribute<T> attribute = channel.attr(AttributeKey.valueOf(key));
    T result = attribute.get();
    if (result == null && function != null) {
        T target = function.apply(key);
        if (target != null) {
            if (attribute.compareAndSet(null, target)) {
                return target;
            } else {
                return attribute.get();
            }
        }
    }
    return result;
}
 
Example 2
Source File: TracingHttpServerHandler.java    From brave with Apache License 2.0 6 votes vote down vote up
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise prm) {
  Attribute<Span> spanAttr = ctx.channel().attr(NettyHttpTracing.SPAN_ATTRIBUTE);
  Span span = spanAttr.get();
  spanAttr.compareAndSet(span, null);
  if (span == null || !(msg instanceof HttpResponse)) {
    ctx.write(msg, prm);
    return;
  }

  HttpResponse response = (HttpResponse) msg;

  Scope scope = currentTraceContext.maybeScope(span.context());
  Throwable error = null;
  try {
    ctx.write(msg, prm);
  } catch (Throwable t) {
    error = t;
    throw t;
  } finally {
    HttpServerRequest request = ctx.channel().attr(NettyHttpTracing.REQUEST_ATTRIBUTE).get();
    handler.handleSend(new HttpResponseWrapper(request, response, error), span);
    scope.close();
  }
}
 
Example 3
Source File: VideoSessionRegistry.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
public void put(UUID recId, ChannelHandlerContext ctx) {
   if(ctx == null) {
      return;
   }
   Attribute<UUID> recIdAttr = ctx.attr(REC_KEY);
   if(!recIdAttr.compareAndSet(null, recId)) {
      throw new IllegalStateException("attempt to set existing non-null ctx recId [" + recIdAttr.get() + "] to [" + recId + "]");
   }
   sessions.put(recIdAttr.get(), ctx);
}
 
Example 4
Source File: TransportHelper.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
public static ChannelTransport getOrNewTransport(Channel channel, RequestBarrier requestBarrier) {
    Attribute<ChannelTransport> attr = channel.attr(TRANSPORT_CACHE_ATTR);
    ChannelTransport transport = attr.get();

    if (transport == null) {
        transport = newTransport(channel, requestBarrier);
        if (!attr.compareAndSet(null, transport)) {
            transport = attr.get();
        }
    }

    return transport;
}
 
Example 5
Source File: ChannelUtils.java    From jforgame with Apache License 2.0 2 votes vote down vote up
/**
 * 添加新的会话
 * @param channel
 * @param session
 * @return
 */
public static boolean addChannelSession(Channel channel, IdSession session) {
	Attribute<IdSession> sessionAttr = channel.attr(SESSION_KEY);
	return sessionAttr.compareAndSet(null, session);
}