use of io.vertx.core.Context in project vert.x by eclipse.
the class Http2ConnectionBase method updateSettings.
protected synchronized void updateSettings(Http2Settings settingsUpdate, Handler<AsyncResult<Void>> completionHandler) {
Context completionContext = completionHandler != null ? context.owner().getOrCreateContext() : null;
Http2Settings current = handler.decoder().localSettings();
for (Map.Entry<Character, Long> entry : current.entrySet()) {
Character key = entry.getKey();
if (Objects.equals(settingsUpdate.get(key), entry.getValue())) {
settingsUpdate.remove(key);
}
}
handler.writeSettings(settingsUpdate).addListener(fut -> {
if (fut.isSuccess()) {
synchronized (Http2ConnectionBase.this) {
updateSettingsHandlers.add(() -> {
serverSettings.putAll(settingsUpdate);
if (completionHandler != null) {
completionContext.runOnContext(v -> {
completionHandler.handle(Future.succeededFuture());
});
}
});
}
} else {
if (completionHandler != null) {
completionContext.runOnContext(v -> {
completionHandler.handle(Future.failedFuture(fut.cause()));
});
}
}
});
}
use of io.vertx.core.Context in project vert.x by eclipse.
the class Http2ServerResponseImpl method sendFile.
@Override
public HttpServerResponse sendFile(String filename, long offset, long length, Handler<AsyncResult<Void>> resultHandler) {
synchronized (conn) {
checkEnded();
Context resultCtx = resultHandler != null ? stream.vertx.getOrCreateContext() : null;
File file = stream.vertx.resolveFile(filename);
if (!file.exists()) {
if (resultHandler != null) {
resultCtx.runOnContext((v) -> resultHandler.handle(Future.failedFuture(new FileNotFoundException())));
} else {
log.error("File not found: " + filename);
}
return this;
}
RandomAccessFile raf;
try {
raf = new RandomAccessFile(file, "r");
} catch (IOException e) {
if (resultHandler != null) {
resultCtx.runOnContext((v) -> resultHandler.handle(Future.failedFuture(e)));
} else {
log.error("Failed to send file", e);
}
return this;
}
long contentLength = Math.min(length, file.length() - offset);
if (headers.get(HttpHeaderNames.CONTENT_LENGTH) == null) {
putHeader(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(contentLength));
}
if (headers.get(HttpHeaderNames.CONTENT_TYPE) == null) {
String contentType = MimeMapping.getMimeTypeForFilename(filename);
if (contentType != null) {
putHeader(HttpHeaderNames.CONTENT_TYPE, contentType);
}
}
checkSendHeaders(false);
FileStreamChannel fileChannel = new FileStreamChannel(ar -> {
if (ar.succeeded()) {
bytesWritten += ar.result();
end();
}
if (resultHandler != null) {
resultCtx.runOnContext(v -> {
resultHandler.handle(Future.succeededFuture());
});
}
}, stream, offset, contentLength);
drainHandler(fileChannel.drainHandler);
ctx.channel().eventLoop().register(fileChannel);
fileChannel.pipeline().fireUserEventTriggered(raf);
}
return this;
}
use of io.vertx.core.Context in project vert.x by eclipse.
the class EventBusTestBase method testNoHandlersCallbackContext.
@Test
public void testNoHandlersCallbackContext() {
startNodes(2);
waitFor(4);
// On an "external" thread
vertices[0].eventBus().send("blah", "blah", ar -> {
assertTrue(ar.failed());
if (ar.cause() instanceof ReplyException) {
ReplyException cause = (ReplyException) ar.cause();
assertSame(ReplyFailure.NO_HANDLERS, cause.failureType());
} else {
fail(ar.cause());
}
assertTrue("Not an EL thread", Context.isOnEventLoopThread());
complete();
});
// On a EL context
vertices[0].runOnContext(v -> {
Context ctx = vertices[0].getOrCreateContext();
vertices[0].eventBus().send("blah", "blah", ar -> {
assertTrue(ar.failed());
if (ar.cause() instanceof ReplyException) {
ReplyException cause = (ReplyException) ar.cause();
assertSame(ReplyFailure.NO_HANDLERS, cause.failureType());
} else {
fail(ar.cause());
}
assertSame(ctx, vertices[0].getOrCreateContext());
complete();
});
});
// On a Worker context
vertices[0].deployVerticle(new AbstractVerticle() {
@Override
public void start() throws Exception {
Context ctx = getVertx().getOrCreateContext();
vertices[0].eventBus().send("blah", "blah", ar -> {
assertTrue(ar.failed());
if (ar.cause() instanceof ReplyException) {
ReplyException cause = (ReplyException) ar.cause();
assertSame(ReplyFailure.NO_HANDLERS, cause.failureType());
} else {
fail(ar.cause());
}
assertSame(ctx, getVertx().getOrCreateContext());
complete();
});
}
}, new DeploymentOptions().setWorker(true));
// Inside executeBlocking
vertices[0].executeBlocking(fut -> {
vertices[0].eventBus().send("blah", "blah", ar -> {
assertTrue(ar.failed());
if (ar.cause() instanceof ReplyException) {
ReplyException cause = (ReplyException) ar.cause();
assertSame(ReplyFailure.NO_HANDLERS, cause.failureType());
} else {
fail(ar.cause());
}
assertTrue("Not an EL thread", Context.isOnEventLoopThread());
complete();
});
fut.complete();
}, false, null);
await();
}
use of io.vertx.core.Context in project vert.x by eclipse.
the class ExecuteBlockingTest method testExecuteBlockingParallel.
@Test
public void testExecuteBlockingParallel() throws Exception {
long start = System.currentTimeMillis();
int numExecBlocking = 10;
long pause = 1000;
CountDownLatch latch = new CountDownLatch(numExecBlocking);
vertx.runOnContext(v -> {
Context ctx = vertx.getOrCreateContext();
assertTrue(ctx.isEventLoopContext());
for (int i = 0; i < numExecBlocking; i++) {
vertx.executeBlocking(future -> {
assertSame(ctx, vertx.getOrCreateContext());
assertTrue(Thread.currentThread().getName().startsWith("vert.x-worker-thread"));
assertTrue(Context.isOnWorkerThread());
assertFalse(Context.isOnEventLoopThread());
try {
Thread.sleep(pause);
} catch (Exception ignore) {
}
future.complete("done!");
}, false, onSuccess(res -> {
assertSame(ctx, vertx.getOrCreateContext());
assertTrue(Thread.currentThread().getName().startsWith("vert.x-eventloop-thread"));
assertFalse(Context.isOnWorkerThread());
assertTrue(Context.isOnEventLoopThread());
assertEquals("done!", res);
latch.countDown();
}));
}
});
awaitLatch(latch);
long now = System.currentTimeMillis();
long leeway = 1000;
assertTrue(now - start < pause + leeway);
}
use of io.vertx.core.Context in project vert.x by eclipse.
the class ExecuteBlockingTest method testExecuteBlockingContext.
@Test
public void testExecuteBlockingContext() {
vertx.runOnContext(v -> {
Context ctx = vertx.getOrCreateContext();
assertTrue(ctx.isEventLoopContext());
vertx.executeBlocking(future -> {
assertSame(ctx, vertx.getOrCreateContext());
assertTrue(Thread.currentThread().getName().startsWith("vert.x-worker-thread"));
assertTrue(Context.isOnWorkerThread());
assertFalse(Context.isOnEventLoopThread());
try {
Thread.sleep(1000);
} catch (Exception ignore) {
}
vertx.runOnContext(v2 -> {
assertSame(ctx, vertx.getOrCreateContext());
assertTrue(Thread.currentThread().getName().startsWith("vert.x-eventloop-thread"));
assertFalse(Context.isOnWorkerThread());
assertTrue(Context.isOnEventLoopThread());
future.complete("done!");
});
}, onSuccess(res -> {
assertSame(ctx, vertx.getOrCreateContext());
assertTrue(Thread.currentThread().getName().startsWith("vert.x-eventloop-thread"));
assertFalse(Context.isOnWorkerThread());
assertTrue(Context.isOnEventLoopThread());
assertEquals("done!", res);
testComplete();
}));
});
await();
}
Aggregations