本文整理汇总了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;
}
示例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);
}
}
}
}
示例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);
}
}
}
}
示例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();
}
}
示例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();
}
}
示例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!");
}
示例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();
}
}