use of io.vertx.core.AsyncResult in project vert.x by eclipse.
the class DatagramTest method testMulticastJoinLeave.
private void testMulticastJoinLeave(String bindAddress, DatagramSocketOptions options1, DatagramSocketOptions options2, BiConsumer<String, Handler<AsyncResult<Void>>> join, BiConsumer<String, Handler<AsyncResult<Void>>> leave) {
if (USE_NATIVE_TRANSPORT) {
return;
}
Buffer buffer = Buffer.buffer("HELLO");
String groupAddress = "230.0.0.1";
AtomicBoolean received = new AtomicBoolean();
peer1 = vertx.createDatagramSocket(options1);
peer2 = vertx.createDatagramSocket(options2);
peer1.handler(packet -> {
assertEquals(buffer, packet.data());
received.set(true);
});
peer1.listen(1234, bindAddress, onSuccess(v1 -> {
join.accept(groupAddress, onSuccess(v2 -> {
peer2.send(buffer, 1234, groupAddress, onSuccess(ar3 -> {
// leave group in 1 second so give it enough time to really receive the packet first
vertx.setTimer(1000, id -> {
leave.accept(groupAddress, onSuccess(ar4 -> {
AtomicBoolean receivedAfter = new AtomicBoolean();
peer1.handler(packet -> {
// Should not receive any more event as it left the group
receivedAfter.set(true);
});
peer2.send(buffer, 1234, groupAddress, onSuccess(v5 -> {
// schedule a timer which will check in 1 second if we received a message after the group
// was left before
vertx.setTimer(1000, id2 -> {
assertFalse(receivedAfter.get());
assertTrue(received.get());
testComplete();
});
}));
}));
});
}));
}));
}));
await();
}
use of io.vertx.core.AsyncResult in project vert.x by eclipse.
the class Http2ServerResponse method sendFile.
@Override
public HttpServerResponse sendFile(String filename, long offset, long length, Handler<AsyncResult<Void>> resultHandler) {
synchronized (conn) {
checkValid();
}
Handler<AsyncResult<Void>> h;
if (resultHandler != null) {
Context resultCtx = stream.vertx.getOrCreateContext();
h = ar -> {
resultCtx.runOnContext((v) -> {
resultHandler.handle(ar);
});
};
} else {
h = ar -> {
};
}
HttpUtils.resolveFile(stream.vertx, filename, offset, length, ar -> {
if (ar.succeeded()) {
AsyncFile file = ar.result();
long contentLength = Math.min(length, file.getReadLength());
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);
file.pipeTo(this, ar1 -> file.close(ar2 -> {
Throwable failure = ar1.failed() ? ar1.cause() : ar2.failed() ? ar2.cause() : null;
if (failure == null)
h.handle(ar1);
else
h.handle(Future.failedFuture(failure));
}));
} else {
h.handle(ar.mapEmpty());
}
});
return this;
}
use of io.vertx.core.AsyncResult in project vert.x by eclipse.
the class ConnectionManagerTest method testCloseManager.
@Test
public void testCloseManager() throws Exception {
EventLoopContext ctx = (EventLoopContext) vertx.getOrCreateContext();
Connection expected = new Connection();
boolean[] disposed = new boolean[1];
ConnectionManager<Object, Connection> mgr = new ConnectionManager<>(new EndpointProvider<Object, Connection>() {
@Override
public Endpoint<Connection> create(Object key, ContextInternal ctx, Runnable dispose) {
return new Endpoint<Connection>(dispose) {
@Override
public void requestConnection(ContextInternal ctx, long timeout, Handler<AsyncResult<Connection>> handler) {
incRefCount();
handler.handle(Future.succeededFuture(expected));
}
@Override
protected void dispose() {
disposed[0] = true;
}
@Override
protected void close() {
super.close();
decRefCount();
}
};
}
});
CountDownLatch latch = new CountDownLatch(1);
mgr.getConnection(ctx, TEST_KEY, onSuccess(conn -> {
assertEquals(expected, conn);
latch.countDown();
}));
awaitLatch(latch);
assertFalse(disposed[0]);
mgr.close();
assertTrue(disposed[0]);
}
use of io.vertx.core.AsyncResult in project vert.x by eclipse.
the class ConnectionManagerTest method testConcurrentDispose.
@Test
public void testConcurrentDispose() throws Exception {
EventLoopContext ctx = (EventLoopContext) vertx.getOrCreateContext();
ConcurrentLinkedQueue<AtomicBoolean> disposals = new ConcurrentLinkedQueue<>();
ConnectionManager<Object, Connection> mgr = new ConnectionManager<>(new EndpointProvider<Object, Connection>() {
@Override
public Endpoint<Connection> create(Object key, ContextInternal ctx, Runnable dispose) {
AtomicBoolean disposed = new AtomicBoolean();
disposals.add(disposed);
return new Endpoint<Connection>(dispose) {
@Override
public void requestConnection(ContextInternal ctx, long timeout, Handler<AsyncResult<Connection>> handler) {
if (disposed.get()) {
// Check we don't have reentrant demands once disposed
fail();
} else {
Connection conn = new Connection();
incRefCount();
handler.handle(Future.succeededFuture(conn));
decRefCount();
}
}
@Override
protected void dispose() {
disposed.set(true);
}
};
}
});
int num = 100000;
int concurrency = 4;
CountDownLatch[] latches = new CountDownLatch[concurrency];
for (int i = 0; i < concurrency; i++) {
CountDownLatch cc = new CountDownLatch(num);
latches[i] = cc;
new Thread(() -> {
for (int j = 0; j < num; j++) {
mgr.getConnection(ctx, TEST_KEY, onSuccess(conn -> {
cc.countDown();
}));
}
}).start();
}
for (int i = 0; i < concurrency; i++) {
awaitLatch(latches[i]);
}
disposals.forEach(disposed -> {
waitUntil(disposed::get);
});
}
use of io.vertx.core.AsyncResult in project nem2-sdk-java by nemtech.
the class MetadataRepositoryVertxImpl method search.
@Override
public Observable<Page<Metadata>> search(MetadataSearchCriteria criteria) {
String sourceAddress = toDto(criteria.getSourceAddress());
String targetAddress = toDto(criteria.getTargetAddress());
String scopedMetadataKey = toDto(criteria.getScopedMetadataKey());
String targetId = criteria.getTargetId();
MetadataTypeEnum metadataType = criteria.getMetadataType() == null ? null : MetadataTypeEnum.fromValue(criteria.getMetadataType().getValue());
String offset = criteria.getOffset();
Integer pageSize = criteria.getPageSize();
Integer pageNumber = criteria.getPageNumber();
Order order = toDto(criteria.getOrder());
Consumer<Handler<AsyncResult<MetadataPage>>> callback = handler -> getClient().searchMetadataEntries(sourceAddress, targetAddress, scopedMetadataKey, targetId, metadataType, pageSize, pageNumber, offset, order, handler);
return exceptionHandling(call(callback).map(page -> this.toPage(page.getPagination(), page.getData().stream().map(this::toMetadata).collect(Collectors.toList()))));
}
Aggregations