Search in sources :

Example 1 with SharedData

use of io.vertx.core.shareddata.SharedData in project vert.x by eclipse.

the class AsynchronousLockTest method testAcquireOnExecuteBlocking.

@Test
public void testAcquireOnExecuteBlocking() {
    Vertx vertx = getVertx();
    SharedData sharedData = vertx.sharedData();
    AtomicReference<Long> start = new AtomicReference<>();
    vertx.<Lock>executeBlocking(future -> {
        CountDownLatch acquireLatch = new CountDownLatch(1);
        AtomicReference<AsyncResult<Lock>> lockReference = new AtomicReference<>();
        sharedData.getLock("foo", ar -> {
            lockReference.set(ar);
            acquireLatch.countDown();
        });
        try {
            awaitLatch(acquireLatch);
            AsyncResult<Lock> ar = lockReference.get();
            if (ar.succeeded()) {
                future.complete(ar.result());
            } else {
                future.fail(ar.cause());
            }
        } catch (InterruptedException e) {
            future.fail(e);
        }
    }, ar -> {
        if (ar.succeeded()) {
            start.set(System.currentTimeMillis());
            vertx.setTimer(1000, tid -> {
                ar.result().release();
            });
            vertx.executeBlocking(future -> {
                CountDownLatch acquireLatch = new CountDownLatch(1);
                AtomicReference<AsyncResult<Lock>> lockReference = new AtomicReference<>();
                sharedData.getLock("foo", ar2 -> {
                    lockReference.set(ar2);
                    acquireLatch.countDown();
                });
                try {
                    awaitLatch(acquireLatch);
                    AsyncResult<Lock> ar3 = lockReference.get();
                    if (ar3.succeeded()) {
                        future.complete(ar3.result());
                    } else {
                        future.fail(ar3.cause());
                    }
                } catch (InterruptedException e) {
                    future.fail(e);
                }
            }, ar4 -> {
                if (ar4.succeeded()) {
                    assertTrue(System.currentTimeMillis() - start.get() >= 1000);
                    testComplete();
                } else {
                    fail(ar4.cause());
                }
            });
        } else {
            fail(ar.cause());
        }
    });
    await();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Vertx(io.vertx.core.Vertx) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncResult(io.vertx.core.AsyncResult) SharedData(io.vertx.core.shareddata.SharedData) Lock(io.vertx.core.shareddata.Lock) Test(org.junit.Test)

Example 2 with SharedData

use of io.vertx.core.shareddata.SharedData in project vert.x by eclipse.

the class AsynchronousLockTest method testAcquireOnSameEventLoop.

@Test
public void testAcquireOnSameEventLoop() {
    Vertx vertx = getVertx();
    Context context = vertx.getOrCreateContext();
    SharedData sharedData = vertx.sharedData();
    AtomicReference<Long> start = new AtomicReference<>();
    context.runOnContext(v -> {
        sharedData.getLock("foo", ar -> {
            assertTrue(ar.succeeded());
            start.set(System.currentTimeMillis());
            Lock lock = ar.result();
            vertx.setTimer(1000, tid -> {
                lock.release();
            });
            context.runOnContext(v2 -> {
                sharedData.getLock("foo", ar2 -> {
                    assertTrue(ar2.succeeded());
                    assertTrue(System.currentTimeMillis() - start.get() >= 1000);
                    testComplete();
                });
            });
        });
    });
    await();
}
Also used : Context(io.vertx.core.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) Vertx(io.vertx.core.Vertx) SharedData(io.vertx.core.shareddata.SharedData) Lock(io.vertx.core.shareddata.Lock) Test(org.junit.Test)

Example 3 with SharedData

use of io.vertx.core.shareddata.SharedData in project vertx-zero by silentbalanceyh.

the class SharedClientImpl method createSync.

private SharedClient createSync(final String name) {
    final SharedData sd = this.vertx.sharedData();
    // Sync map created
    this.syncMap = sd.getLocalMap(name);
    LOGGER.info(Info.INFO_SYNC, String.valueOf(this.syncMap.hashCode()));
    this.isAsync = false;
    return this;
}
Also used : SharedData(io.vertx.core.shareddata.SharedData)

Example 4 with SharedData

use of io.vertx.core.shareddata.SharedData in project okapi by folio-org.

the class AsyncMapFactory method create.

/**
 * Create a AsyncMap
 *
 * @param <K> Key type
 * @param <V> Value type
 * @param vertx
 * @param mapName name of the map. If null, will always create a local map
 * @param fut
 */
public static <K, V> void create(Vertx vertx, String mapName, Handler<ExtendedAsyncResult<AsyncMap<K, V>>> fut) {
    if (vertx.isClustered() && mapName != null) {
        SharedData shared = vertx.sharedData();
        shared.<K, V>getClusterWideMap(mapName, res -> {
            if (res.succeeded()) {
                fut.handle(new Success<>(res.result()));
            } else {
                fut.handle(new Failure<>(INTERNAL, res.cause()));
            }
        });
    } else {
        // Dirty trickery to make sure we can run two verticles in our tests,
        // without them sharing the 'shared' memory. Only when running in non-
        // clustered mode, of course.
        // Also used in deploy-only nodes, where we want local-only tenant and
        // module lists with only the hard-coded supertenant and internalModule.
        Random r = new Random();
        String newid = String.format("%09d", r.nextInt(1000000000));
        if (mapName != null) {
            newid = mapName + newid;
        }
        AsyncLocalmap<K, V> l = new AsyncLocalmap<>(vertx, newid);
        fut.handle(new Success<>(l));
    }
}
Also used : Random(java.util.Random) SharedData(io.vertx.core.shareddata.SharedData)

Example 5 with SharedData

use of io.vertx.core.shareddata.SharedData in project vertx-zero by silentbalanceyh.

the class SharedClientImpl method createAsync.

private SharedClient createAsync(final String name, final Handler<AsyncResult<SharedClient>> handler) {
    final SharedData sd = this.vertx.sharedData();
    // Async map created
    LOGGER.info(Info.INFO_ASYNC_START);
    sd.<K, V>getAsyncMap(name, res -> {
        if (res.succeeded()) {
            this.asyncMap = res.result();
            LOGGER.info(Info.INFO_ASYNC_END, String.valueOf(this.asyncMap.hashCode()));
            this.isAsync = true;
            handler.handle(Future.succeededFuture(this));
        }
    });
    return this;
}
Also used : SharedData(io.vertx.core.shareddata.SharedData)

Aggregations

SharedData (io.vertx.core.shareddata.SharedData)5 Vertx (io.vertx.core.Vertx)2 Lock (io.vertx.core.shareddata.Lock)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Test (org.junit.Test)2 AsyncResult (io.vertx.core.AsyncResult)1 Context (io.vertx.core.Context)1 Random (java.util.Random)1 CountDownLatch (java.util.concurrent.CountDownLatch)1