Example usage for io.netty.util Attribute getAndSet

List of usage examples for io.netty.util Attribute getAndSet

Introduction

In this page you can find the example usage for io.netty.util Attribute getAndSet.

Prototype

T getAndSet(T value);

Source Link

Document

Atomically sets to the given value and returns the old value which may be null if non was set before.

Usage

From source file:com.linecorp.armeria.internal.DefaultAttributeMapTest.java

License:Apache License

@Test
public void testGetAndSetWithNull() {
    AttributeKey<Integer> key = AttributeKey.valueOf("key");

    Attribute<Integer> attr = map.attr(key);
    attr.set(1);/*from  w w  w.  j av a 2 s .  c o  m*/
    assertSame(1, attr.getAndSet(null));

    Attribute<Integer> attr2 = map.attr(key);
    attr2.set(2);
    assertSame(2, attr2.get());
    assertSame(attr, attr2);
}

From source file:com.qq.servlet.demo.netty.telnet.client.TelnetClient.java

License:Apache License

private void test(Bootstrap b, int i) throws InterruptedException {
    // Start the connection attempt.
    Channel ch = b.connect(host, port).sync().channel();

    String body = "[" + i + "]aaaaaaa" + Thread.currentThread().getName() + "aaaaa";

    AttributeKey<String> key = AttributeKey.valueOf("ASYNC_CONTEXT");
    Attribute<String> attr = ch.attr(key);
    String andSet = attr.getAndSet(Thread.currentThread().getName());
    System.out.println(andSet);//  w  w w.j  a v  a 2s  .  com

    ChannelFuture lastWriteFuture = ch.writeAndFlush(body + "\r\n");
    // Wait until all messages are flushed before closing the channel.
    if (lastWriteFuture != null) {
        lastWriteFuture.sync();
    }
}

From source file:com.qq.servlet.demo.thrift.netty.client.NettyThriftClient.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup group = new NioEventLoopGroup(2);
    try {/*  ww  w.j  a  va2 s  .com*/
        final Bootstrap b = new Bootstrap();
        b.group(group);
        b.channel(NioSocketChannel.class);
        b.handler(new NettyThriftClientInitializer());

        // Start the connection attempt.
        ChannelFuture future = b.connect(host, port);
        ChannelFuture sync = future.sync();
        Channel ch = sync.channel();

        AttributeKey<String> key = AttributeKey.valueOf("ASYNC_CONTEXT");
        Attribute<String> attr = ch.attr(key);
        String andSet = attr.getAndSet(Thread.currentThread().getName());

        TMemoryBuffer transportr = new TMemoryBuffer(1024);
        TProtocol protocol = new TBinaryProtocol(transportr);
        TMultiplexedProtocol multiProtocol3 = new TMultiplexedProtocol(protocol,
                IdGenService.class.getSimpleName());
        IdGenService.Client aoClient = new IdGenService.Client(multiProtocol3);
        IdGenReq req = new IdGenReq();
        req.setTableName("t_lottery_append_task-----------");
        req.setSource(andSet);
        aoClient.send_idGen(req);
        int length = transportr.length();
        byte[] buf = new byte[length];
        transportr.read(buf, 0, length);
        System.out.println(Arrays.toString(buf));

        ChannelFuture lastWriteFuture = ch.writeAndFlush(buf);
        // Wait until all messages are flushed before closing the channel.
        if (lastWriteFuture != null) {
            lastWriteFuture.sync();
        }

        System.in.read();
    } finally {
        group.shutdownGracefully();
    }
}