Example usage for io.vertx.core.impl ContextImpl executeBlocking

List of usage examples for io.vertx.core.impl ContextImpl executeBlocking

Introduction

In this page you can find the example usage for io.vertx.core.impl ContextImpl executeBlocking.

Prototype

@Override
    public <T> Future<T> executeBlocking(Handler<Promise<T>> blockingCodeHandler, TaskQueue queue) 

Source Link

Usage

From source file:com.usebilbo.vertx.cluster.manager.IgniteClusterManager.java

License:Open Source License

@Override
public void getLockWithTimeout(String name, long timeout, Handler<AsyncResult<Lock>> handler) {
    ContextImpl context = (ContextImpl) vertx.getOrCreateContext();
    // Ordered on the internal blocking executor
    context.executeBlocking(() -> {
        boolean locked = false;

        try {//from   w  ww.j av  a 2  s  .  c o  m
            @SuppressWarnings("resource")
            IgniteQueue<String> queue = getQueue(name, true);

            pendingLocks.offer(name);

            locked = queue.offer(getNodeID(), timeout, TimeUnit.MILLISECONDS);

            if (!locked) {
                // EVT_NODE_LEFT/EVT_NODE_FAILED event might be already handled, so trying get lock again if
                // node left topology.
                // Use IgniteSempahore when it will be fixed.
                String ownerId = queue.peek();
                ClusterNode ownerNode = ignite().cluster().forNodeId(UUID.fromString(ownerId)).node();
                if (ownerNode == null) {
                    queue.remove(ownerId);
                    locked = queue.offer(getNodeID(), timeout, TimeUnit.MILLISECONDS);
                }
            }
        } catch (Exception e) {
            throw new VertxException("Error during getting lock " + name, e);
        } finally {
            pendingLocks.remove(name);
        }

        if (locked) {
            return new LockImpl(name);
        } else {
            throw new VertxException("Timed out waiting to get lock " + name);
        }
    }, handler);
}