当前位置: 首页>>代码示例>>Java>>正文


Java ChannelFuture.sync方法代码示例

本文整理汇总了Java中io.netty.channel.ChannelFuture.sync方法的典型用法代码示例。如果您正苦于以下问题:Java ChannelFuture.sync方法的具体用法?Java ChannelFuture.sync怎么用?Java ChannelFuture.sync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在io.netty.channel.ChannelFuture的用法示例。


在下文中一共展示了ChannelFuture.sync方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: sendRequests

import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
private synchronized Collection<FullHttpResponse> sendRequests(
    final SocketAddress remoteAddress,
    final Collection<HttpRequest> requests) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(requests.size());
    final Collection<FullHttpResponse> content = Collections.synchronizedList(new ArrayList<>(requests.size()));

    clientBootstrap.handler(new CountDownLatchHandler(latch, content));

    ChannelFuture channelFuture = null;
    try {
        channelFuture = clientBootstrap.connect(remoteAddress);
        channelFuture.sync();

        for (HttpRequest request : requests) {
            channelFuture.channel().writeAndFlush(request);
        }
        latch.await(10, TimeUnit.SECONDS);

    } finally {
        if (channelFuture != null) {
            channelFuture.channel().close().sync();
        }
    }

    return content;
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:27,代码来源:Netty4HttpClient.java

示例2: connect

import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
/**
 *  Connects to the node and returns only upon connection close
 */
public void connect(String host, int port, String remoteId, boolean discoveryMode) {
    try {
        ChannelFuture f = connectAsync(host, port, remoteId, discoveryMode);

        f.sync();

        // Wait until the connection is closed.
        f.channel().closeFuture().sync();

        logger.debug("Connection is closed");

    } catch (Exception e) {
        if (discoveryMode) {
            logger.trace("Exception:", e);
        } else {
            if (e instanceof IOException) {
                logger.info("PeerClient: Can't connect to " + host + ":" + port + " (" + e.getMessage() + ")");
                logger.debug("PeerClient.connect(" + host + ":" + port + ") exception:", e);
            } else {
                logger.error("Exception:", e);
            }
        }
    }
}
 
开发者ID:talentchain,项目名称:talchain,代码行数:28,代码来源:PeerClient.java

示例3: connect

import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
/**
 *  Connects to the node and returns only upon connection close
 */
public void connect(String host, int port, String remoteId, boolean discoveryMode) {
    try {
        ChannelFuture f = connectAsync(host, port, remoteId, discoveryMode);

        f.sync();

        // Wait until the connection is closed.
        f.channel().closeFuture().sync();

        logger.debug("Connection is closed");

    } catch (Exception e) {
        if (discoveryMode) {
            logger.debug("Exception:", e);
        } else {
            if (e instanceof IOException) {
                logger.info("PeerClient: Can't connect to " + host + ":" + port + " (" + e.getMessage() + ")");
                logger.debug("PeerClient.connect(" + host + ":" + port + ") exception:", e);
            } else {
                logger.error("Exception:", e);
            }
        }
    }
}
 
开发者ID:rsksmart,项目名称:rskj,代码行数:28,代码来源:PeerClient.java

示例4: start

import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
public void start(){
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(workerGroup,bossGroup)
            .channel(NioServerSocketChannel.class)
            .childHandler(new TCPHandlerInitializer(this))
            .option(ChannelOption.SO_BACKLOG, 512)
            .childOption(ChannelOption.SO_KEEPALIVE, true);
    Channel serverChannel = bootstrap.bind(new InetSocketAddress(port)).channel();
    ChannelFuture future = serverChannel.closeFuture();
    try {
        System.out.println("MQTT服务器已启动...");
        future.sync();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
 
开发者ID:Dovakin-IO,项目名称:DovakinMQ,代码行数:17,代码来源:DovakinMQServer.java

示例5: main

import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {

        // Configure SSL.

        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
             .channel(NioSocketChannel.class)
             .handler(new SecureChatClientInitializer(null));

            // Start the connection attempt.
            Channel ch = b.connect(HOST, PORT).sync().channel();

            // Read commands from the stdin.
            ChannelFuture lastWriteFuture = null;
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            for (;;) {
                String line = in.readLine();
                if (line == null) {
                    break;
                }

                // Sends the received line to the server.
                lastWriteFuture = ch.writeAndFlush(line + "\r\n");

                // If user typed the 'bye' command, wait until the server closes
                // the connection.
                if ("bye".equals(line.toLowerCase())) {
                    ch.closeFuture().sync();
                    break;
                }
            }

            // Wait until all messages are flushed before closing the channel.
            if (lastWriteFuture != null) {
                lastWriteFuture.sync();
            }
        } finally {
            // The connection is closed automatically on shutdown.
            group.shutdownGracefully();
        }
    }
 
开发者ID:veritasware,项目名称:neto,代码行数:44,代码来源:SecureChatClient.java

示例6: run

import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
@Override
public void run()
{
	this.console.println("Starting Client...");
	EventLoopGroup boss = new NioEventLoopGroup();
	try
	{
		Bootstrap b = new Bootstrap();
		b.group(boss)
				.channel(NioSocketChannel.class)
				.handler(new ClientChannelInitializer());

		this.console.println("Connecting to Server...");
		Channel ch = b.connect(ChubbyCat.HOST, ChubbyCat.PORT).sync().channel();
		this.console.println("Connection Established!");

		ChannelFuture prevWrite;
		for(;;)
		{
			Message msg = this.outgoing.poll();
			if (msg == null)
			{
				Thread.sleep(50);
				continue;
			}

			prevWrite = ch.writeAndFlush(msg);

			if ("bye".equals(msg.getData().toLowerCase()))
			{
				ch.closeFuture().sync();
				break;
			}
		}

		if (prevWrite != null)
		{
			prevWrite.sync();
		}
	}
	catch (Exception e)
	{
		e.printStackTrace();
	}
	finally
	{
		this.console.println("Stopping Client...");
		boss.shutdownGracefully();
	}

	this.console.println("Connection Ended!");
}
 
开发者ID:andykuo1,项目名称:candlelight,代码行数:53,代码来源:ChatClient.java

示例7: main

import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group)
                .channel(NioSocketChannel.class)
                .handler(new ClientInitializer());

        // Start the connection attempt.
        Channel ch = b.connect(HOST, 12345).sync().channel();

        // Read commands from the stdin.
        ChannelFuture lastWriteFuture = null;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        for (; ; ) {
            String line = in.readLine();
            if (line == null) {
                break;
            }
            BaseByteArrayPacket head = null;
            if (line.startsWith("1")) {
                CS_USER_CONNECT_TO_SERVER.Builder builder = CS_USER_CONNECT_TO_SERVER.newBuilder();
                builder.setName("Test");
                builder.setParams("Param");
                CS_USER_CONNECT_TO_SERVER build = builder.build();
                head = new BaseByteArrayPacket(Message.PB.SystemKey.CS_USER_CONNECT_TO_SERVER_VALUE, build.toByteArray());
            } else if (line.startsWith("2")) {
                CS_REGISTER register = CS_REGISTER.newBuilder().setName("1").setPassword("1").build();
                head = new BaseByteArrayPacket(Message.PB.MessageKey.CS_REGISTER_VALUE, register.toByteArray());
            }
            // Sends the received line to the server.
            lastWriteFuture = ch.writeAndFlush(head);

        }

        // Wait until all messages are flushed before closing the channel.
        if (lastWriteFuture != null) {
            lastWriteFuture.sync();
        }
    } finally {
        group.shutdownGracefully();
    }
}
 
开发者ID:zerosoft,项目名称:CodeBroker,代码行数:44,代码来源:ClientMain.java


注:本文中的io.netty.channel.ChannelFuture.sync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。