本文整理汇总了Java中io.vertx.core.net.NetClient类的典型用法代码示例。如果您正苦于以下问题:Java NetClient类的具体用法?Java NetClient怎么用?Java NetClient使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NetClient类属于io.vertx.core.net包,在下文中一共展示了NetClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: start
import io.vertx.core.net.NetClient; //导入依赖的package包/类
@Override
public void start() throws Exception {
NetServer netServer = vertx.createNetServer();//创建代理服务器
NetClient netClient = vertx.createNetClient();//创建连接mysql客户端
netServer.connectHandler(socket -> netClient.connect(port, mysqlHost, result -> {
//响应来自客户端的连接请求,成功之后,在建立一个与目标mysql服务器的连接
if (result.succeeded()) {
//与目标mysql服务器成功连接连接之后,创造一个MysqlProxyConnection对象,并执行代理方法
new MysqlProxyConnection(socket, result.result()).proxy();
} else {
logger.error(result.cause().getMessage(), result.cause());
socket.close();
}
})).listen(port, listenResult -> {//代理服务器的监听端口
if (listenResult.succeeded()) {
//成功启动代理服务器
logger.info("Mysql proxy server start up.");
} else {
//启动代理服务器失败
logger.error("Mysql proxy exit. because: " + listenResult.cause().getMessage(), listenResult.cause());
System.exit(1);
}
});
}
示例2: registerTimerOut
import io.vertx.core.net.NetClient; //导入依赖的package包/类
private void registerTimerOut(final Session session, final NetClient client) {
vertx.setPeriodic(config.getJsonObject("application").getInteger("tcp-session-timeout", 1200) * 1000, new Handler<Long>() {
private int readCount = 0;
private int writeCount = 0;
@Override
public void handle(Long timerID) {
if ((session.getRead_count() <= readCount) && (session.getWrite_count() <= writeCount)) {
session.setActive(false);
if (client != null) {
client.close();
}
vertx.cancelTimer(timerID);
}
readCount = session.getRead_count();
writeCount = session.getWrite_count();
}
});
}
示例3: start
import io.vertx.core.net.NetClient; //导入依赖的package包/类
@Override
public void start(Handler<AsyncResult<Void>> startFuture) {
if (port > 0) {
// fail if port is already in use
NetClientOptions options = new NetClientOptions().setConnectTimeout(200);
NetClient c = vertx.createNetClient(options);
c.connect(port, "localhost", res -> {
if (res.succeeded()) {
NetSocket socket = res.result();
socket.close();
startFuture.handle(Future.failedFuture("port " + port + " already in use"));
} else {
start2(startFuture);
}
});
} else {
start2(startFuture);
}
}
示例4: waitPortToClose
import io.vertx.core.net.NetClient; //导入依赖的package包/类
private void waitPortToClose(Handler<AsyncResult<Void>> stopFuture, int iter) {
if (port > 0) {
// fail if port is already in use
NetClientOptions options = new NetClientOptions().setConnectTimeout(50);
NetClient c = vertx.createNetClient(options);
c.connect(port, "localhost", res -> {
if (res.succeeded()) {
NetSocket socket = res.result();
socket.close();
if (iter > 0) {
vertx.setTimer(100, x -> waitPortToClose(stopFuture, iter - 1));
} else {
stopFuture.handle(Future.failedFuture("port " + port + " not shut down"));
}
} else {
stopFuture.handle(Future.succeededFuture());
}
});
} else {
stopFuture.handle(Future.succeededFuture());
}
}
示例5: noConnectSent
import io.vertx.core.net.NetClient; //导入依赖的package包/类
@Test
public void noConnectSent(TestContext context) {
NetClient client = this.vertx.createNetClient();
Async async = context.async();
client.connect(MQTT_SERVER_PORT, MQTT_SERVER_HOST, done -> {
if (done.succeeded()) {
done.result().closeHandler(v -> {
log.info("No CONNECT sent in " + MQTT_TIMEOUT_ON_CONNECT + " secs. Closing connection.");
async.complete();
});
} else {
context.fail();
}
});
// check that the async is completed (so connection was closed by server) in
// the specified timeout (+500 ms just for being sure)
async.await(500 + MQTT_TIMEOUT_ON_CONNECT * 1000);
if (!async.isCompleted())
context.fail();
}
示例6: start
import io.vertx.core.net.NetClient; //导入依赖的package包/类
@Override
public void start(Future<Void> startFuture) {
log.info("start", "initializationStarted");
MemcacheClusterConfig memcacheClusterConfig;
try {
memcacheClusterConfig = new MemcacheClusterConfig(config().getJsonObject(MEMCACHE_CLUSTER_KEY));
} catch (MemcacheException me) {
log.error("start", "exception", me.getMessage());
startFuture.fail(new Exception(me.getMessage()));
return;
}
NetClient netClient = vertx.createNetClient();
establishSockets(memcacheClusterConfig, netClient);
log.info("start", "initializationCompleted");
startFuture.complete();
}
示例7: testSendMessageWithReplyBacktrack
import io.vertx.core.net.NetClient; //导入依赖的package包/类
@Test
public void testSendMessageWithReplyBacktrack(TestContext context) {
// Send a request and get a response
NetClient client = vertx.createNetClient();
final Async async = context.async();
client.connect(7000, "localhost", conn -> {
context.assertFalse(conn.failed());
NetSocket socket = conn.result();
final FrameParser parser = new FrameParser(parse -> {
context.assertTrue(parse.succeeded());
JsonObject frame = parse.result();
context.assertNotEquals("err", frame.getString("type"));
context.assertEquals("Hello vert.x", frame.getJsonObject("body").getString("value"));
client.close();
async.complete();
});
socket.handler(parser);
FrameHelper.sendFrame("send", "hello", "#backtrack", new JsonObject().put("value", "vert.x"), socket);
});
}
示例8: testSendMessageWithDuplicateReplyID
import io.vertx.core.net.NetClient; //导入依赖的package包/类
@Test
public void testSendMessageWithDuplicateReplyID(TestContext context) {
// replies must always return to the same origin
NetClient client = vertx.createNetClient();
final Async async = context.async();
client.connect(7000, "localhost", conn -> {
context.assertFalse(conn.failed());
NetSocket socket = conn.result();
vertx.eventBus().consumer("third-party-receiver", msg -> context.fail());
final FrameParser parser = new FrameParser(parse -> {
context.assertTrue(parse.succeeded());
client.close();
async.complete();
});
socket.handler(parser);
FrameHelper.sendFrame("send", "hello", "third-party-receiver", new JsonObject().put("value", "vert.x"), socket);
});
}
示例9: testSendVoidMessage
import io.vertx.core.net.NetClient; //导入依赖的package包/类
@Test
public void testSendVoidMessage(TestContext context) {
// Send a request and get a response
NetClient client = vertx.createNetClient();
final Async async = context.async();
vertx.eventBus().consumer("test", (Message<JsonObject> msg) -> {
client.close();
async.complete();
});
client.connect(7000, "localhost", conn -> {
context.assertFalse(conn.failed());
NetSocket socket = conn.result();
FrameHelper.sendFrame("send", "test", new JsonObject().put("value", "vert.x"), socket);
});
}
示例10: testSendsFromOtherSideOfBridge
import io.vertx.core.net.NetClient; //导入依赖的package包/类
@Test
public void testSendsFromOtherSideOfBridge(TestContext context) {
NetClient client = vertx.createNetClient();
final Async async = context.async();
client.connect(7000, "localhost", conn -> {
context.assertFalse(conn.failed());
NetSocket socket = conn.result();
final FrameParser parser = new FrameParser(parse -> {
context.assertTrue(parse.succeeded());
JsonObject frame = parse.result();
context.assertNotEquals("err", frame.getString("type"));
context.assertEquals(true, frame.getBoolean("send"));
context.assertEquals("hi", frame.getJsonObject("body").getString("value"));
client.close();
async.complete();
});
socket.handler(parser);
FrameHelper.sendFrame("register", "ping", null, socket);
});
}
示例11: testSendMessageWithReplyBacktrack
import io.vertx.core.net.NetClient; //导入依赖的package包/类
@Test
public void testSendMessageWithReplyBacktrack(TestContext context) {
// Send a request and get a response
NetClient client = vertx.createNetClient();
final Async async = context.async();
client.connect(7000, "localhost", conn -> {
context.assertFalse(conn.failed());
NetSocket socket = conn.result();
final FrameParser parser = new FrameParser(parse -> {
context.assertTrue(parse.succeeded());
JsonObject frame = parse.result();
context.assertNotEquals("err", frame.getString("type"));
context.assertEquals(true, frame.getBoolean("send"));
context.assertEquals("Hello vert.x", frame.getJsonObject("body").getString("value"));
client.close();
async.complete();
});
socket.handler(parser);
FrameHelper.sendFrame("send", "hello", "#backtrack", new JsonObject().put("value", "vert.x"), socket);
});
}
示例12: testSendPing
import io.vertx.core.net.NetClient; //导入依赖的package包/类
@Test
public void testSendPing(TestContext context) {
NetClient client = vertx.createNetClient();
final Async async = context.async();
eventHandler = event -> {
if (event.type() == BridgeEventType.SOCKET_PING) {
async.complete();
}
};
client.connect(7000, "localhost", context.asyncAssertSuccess(socket -> {
socket.handler(buff -> {
});
FrameHelper.sendFrame("register", "echo", null, socket);
FrameHelper.sendFrame("ping", socket);
}));
}
示例13: check
import io.vertx.core.net.NetClient; //导入依赖的package包/类
@Override
public Future<Result> check() {
Future<Result> future = Future.future();
NetClientOptions options = new NetClientOptions().setConnectTimeout(500);
NetClient client = vertx.createNetClient(options);
client.connect(port, host, res -> {
if (res.succeeded()) {
future.complete(Result.healthy());
} else {
future.complete(Result.unhealthy(res.cause()));
}
client.close();
});
return future;
}
示例14: check
import io.vertx.core.net.NetClient; //导入依赖的package包/类
@Override
public CompletableFuture<Result> check() {
VertxCompletableFuture<Result> result = new VertxCompletableFuture<>(vertx);
NetClientOptions options = new NetClientOptions().setConnectTimeout(500);
NetClient client = vertx.createNetClient(options);
client.connect(port, host, res -> {
if (res.succeeded()) {
result.complete(Result.healthy());
} else {
result.complete(Result.unhealthy(res.cause()));
}
client.close();
});
return result;
}
示例15: ProtonTransport
import io.vertx.core.net.NetClient; //导入依赖的package包/类
ProtonTransport(Connection connection, Vertx vertx, NetClient netClient, NetSocket socket,
ProtonSaslAuthenticator authenticator, ProtonTransportOptions options) {
this.connection = connection;
this.vertx = vertx;
this.netClient = netClient;
this.socket = socket;
transport.setMaxFrameSize(options.getMaxFrameSize() == 0 ? DEFAULT_MAX_FRAME_SIZE : options.getMaxFrameSize());
transport.setEmitFlowEventOnSend(false); // TODO: make configurable
transport.setIdleTimeout(2 * options.getHeartbeat());
if (authenticator != null) {
authenticator.init(this.socket, (ProtonConnection) this.connection.getContext(), transport);
}
this.authenticator = authenticator;
transport.bind(connection);
connection.collect(collector);
socket.endHandler(this::handleSocketEnd);
socket.handler(this::handleSocketBuffer);
}