Example usage for io.netty.channel ChannelFuture await

List of usage examples for io.netty.channel ChannelFuture await

Introduction

In this page you can find the example usage for io.netty.channel ChannelFuture await.

Prototype

@Override
    ChannelFuture await() throws InterruptedException;

Source Link

Usage

From source file:org.wso2.siddhi.tcp.transport.TcpNettyClient.java

License:Open Source License

/**
 * @param args/*from w w  w .  jav  a 2 s. c o m*/
 */
public static void main(String[] args) {

    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                p.addLast(new EventEncoder());
            }
        });

        // Start the connection attempt.
        Channel ch = b.connect("localhost", 8080).sync().channel();
        ChannelFuture cf;
        for (int i = 0; i < 1000000; i++) {
            ArrayList<Event> arrayList = new ArrayList<Event>(100);
            for (int j = 0; j < 50; j++) {
                arrayList.add(new Event(System.currentTimeMillis(), new Object[] { "WSO2", i, 10 }));
                arrayList.add(new Event(System.currentTimeMillis(), new Object[] { "IBM", i, 10 }));
            }
            EventComposite EventComposite = new EventComposite("test", "StockStream",
                    arrayList.toArray(new Event[10]));
            cf = ch.write(EventComposite);
            ch.flush();
            cf.await();

            //                if (i * 10 % 10000 == 0) {
            //                    Thread.sleep(10000);
            log.info("Done Sending " + i * 10 + " events..");
            //                }
        }

        ch.close().sync();
    } catch (InterruptedException e) {
        log.error("Error sending messages " + e.getMessage(), e);
    } finally {
        group.shutdownGracefully();
    }

}