Exemple #1
0
  @Test
  public void testNoContext() throws Exception {

    final CountDownLatch latch = new CountDownLatch(1);

    Vertx vertx = VertxFactory.newVertx();

    final NetServer server = vertx.createNetServer();
    server.connectHandler(
        new Handler<NetSocket>() {
          public void handle(NetSocket socket) {
            Pump p = Pump.createPump(socket, socket);
            p.start();
          }
        });

    final CountDownLatch listenLatch = new CountDownLatch(1);
    server.listen(
        1234,
        new AsyncResultHandler<NetServer>() {
          @Override
          public void handle(AsyncResult<NetServer> ar) {
            if (ar.succeeded()) {
              listenLatch.countDown();
            } else {
              ar.cause().printStackTrace();
            }
          }
        });

    assertTrue(listenLatch.await(5, TimeUnit.SECONDS));

    final NetClient client = vertx.createNetClient();
    client.connect(
        1234,
        new AsyncResultHandler<NetSocket>() {
          public void handle(AsyncResult<NetSocket> res) {
            res.result()
                .dataHandler(
                    new Handler<Buffer>() {
                      public void handle(Buffer data) {
                        server.close(
                            new AsyncResultHandler<Void>() {
                              public void handle(AsyncResult<Void> res) {
                                client.close();
                                latch.countDown();
                              }
                            });
                      }
                    });
            res.result().write("foo");
          }
        });

    assertTrue(latch.await(5, TimeUnit.SECONDS));
  }
 @Override
 public JDBCClient getConnection(Handler<AsyncResult<SQLConnection>> handler) {
   Context ctx = vertx.getOrCreateContext();
   exec.execute(
       () -> {
         Future<SQLConnection> res = Future.future();
         try {
           /*
           This can block until a connection is free.
           We don't want to do that while running on a worker as we can enter a deadlock situation as the worker
           might have obtained a connection, and won't release it until it is run again
           There is a general principle here:
           *User code* should be executed on a worker and can potentially block, it's up to the *user* to deal with
           deadlocks that might occur there.
           If the *service code* internally blocks waiting for a resource that might be obtained by *user code*, then
           this can cause deadlock, so the service should ensure it never does this, by executing such code
           (e.g. getConnection) on a different thread to the worker pool.
           We don't want to use the vert.x internal pool for this as the threads might end up all blocked preventing
           other important operations from occurring (e.g. async file access)
           */
           Connection conn = ds.getConnection();
           SQLConnection sconn = new JDBCConnectionImpl(vertx, conn);
           res.complete(sconn);
         } catch (SQLException e) {
           res.fail(e);
         }
         ctx.runOnContext(v -> res.setHandler(handler));
       });
   return this;
 }
 private DataSourceHolder lookupHolder(String datasourceName, JsonObject config) {
   synchronized (vertx) {
     LocalMap<String, DataSourceHolder> map = vertx.sharedData().getLocalMap(DS_LOCAL_MAP_NAME);
     DataSourceHolder theHolder = map.get(datasourceName);
     if (theHolder == null) {
       theHolder = new DataSourceHolder(config, () -> removeFromMap(map, datasourceName));
       map.put(datasourceName, theHolder);
     } else {
       theHolder.incRefCount();
     }
     return theHolder;
   }
 }
 private void checkAddAccceptedReplyAddress(final String replyAddress) {
   if (replyAddress != null) {
     // This message has a reply address
     // When the reply comes through we want to accept it irrespective of its address
     // Since all replies are implicitly accepted if the original message was accepted
     // So we cache the reply address, so we can check against it
     acceptedReplyAddresses.add(replyAddress);
     // And we remove after timeout in case the reply never comes
     vertx.setTimer(
         DEFAULT_REPLY_TIMEOUT,
         new Handler<Long>() {
           public void handle(Long id) {
             acceptedReplyAddresses.remove(replyAddress);
           }
         });
   }
 }
 public EventBusBridge(
     Vertx vertx,
     JsonArray inboundPermitted,
     JsonArray outboundPermitted,
     long authTimeout,
     String authAddress) {
   this.vertx = vertx;
   this.eb = vertx.eventBus();
   this.inboundPermitted = convertArray(inboundPermitted);
   this.outboundPermitted = convertArray(outboundPermitted);
   if (authTimeout < 0) {
     throw new IllegalArgumentException("authTimeout < 0");
   }
   this.authTimeout = authTimeout;
   if (authAddress == null) {
     authAddress = DEFAULT_AUTH_ADDRESS;
   }
   this.authAddress = authAddress;
 }
  public static HttpClient client(Vertx vertx) {
    HttpClient client =
        vertx
            .createHttpClient()
            .setHost(remoteHost())
            .setPort(remotePort())
            // .setKeepAlive(false)
            .setConnectTimeout(10000);

    if (remotePort() == 443) {
      client.setSSL(true).setTrustAll(true).setVerifyHost(false);
    }

    client.exceptionHandler(
        new Handler<Exception>() {
          public void handle(Exception e) {
            e.printStackTrace();
          }
        });

    return client;
  }