io.vertx.core.shareddata.AsyncMap Java Examples

The following examples show how to use io.vertx.core.shareddata.AsyncMap. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultServiceDiscoveryBackend.java    From vertx-service-discovery with Apache License 2.0 6 votes vote down vote up
private synchronized void retrieveRegistry(Handler<AsyncResult<AsyncMap<String, String>>> handler) {
  if (registry != null) {
    handler.handle(Future.succeededFuture(registry));
  } else {
    vertx.sharedData().<String, String>getClusterWideMap("service.registry", ar -> {
      synchronized (DefaultServiceDiscoveryBackend.class) {
        if (ar.failed()) {
          handler.handle(ar);
        } else {
          registry = ar.result();
          handler.handle(Future.succeededFuture(registry));
        }
      }
    });
  }
}
 
Example #2
Source File: NearCacheSessionStoreIT.java    From vertx-vaadin with MIT License 5 votes vote down vote up
private void getRemoteMap(TestContext context, Handler<AsyncResult<AsyncMap<String, Session>>> resultHandler) {
    if (remoteMap == null) {
        rule.vertx().sharedData().<String, Session>getClusterWideMap(NearCacheSessionStore.DEFAULT_SESSION_MAP_NAME, res -> {
            if (res.succeeded()) {
                remoteMap = res.result();
                resultHandler.handle(Future.succeededFuture(res.result()));
            } else {
                resultHandler.handle(res);
            }
        });
    } else {
        resultHandler.handle(Future.succeededFuture(remoteMap));
    }
}
 
Example #3
Source File: ClusteredSessionStoreImpl.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
private void getMap(Handler<AsyncResult<AsyncMap<String, Session>>> resultHandler) {
  if (sessionMap == null) {
    vertx.sharedData().<String, Session>getClusterWideMap(sessionMapName, res -> {
      if (res.succeeded()) {
        sessionMap = res.result();
        resultHandler.handle(Future.succeededFuture(res.result()));
      } else {
        resultHandler.handle(res);
      }
    });
  } else {
    resultHandler.handle(Future.succeededFuture(sessionMap));
  }
}
 
Example #4
Source File: ServiceRegistry.java    From vert.x-microservice with Apache License 2.0 5 votes vote down vote up
private void removeAndUpdateServiceInfo(ServiceInfo info, AsyncMap<String, ServiceInfoHolder> resultMap, ServiceInfoHolder holder) {
    holder.remove(info);
    resultMap.replace(GlobalKeyHolder.SERVICE_HOLDER, holder, t -> {
        if (t.succeeded()) {
            resetServiceCounterAndPublish(info);

        }
    });
}
 
Example #5
Source File: ServiceRegistry.java    From vert.x-microservice with Apache License 2.0 5 votes vote down vote up
private void getServiceHolderAndPingServices(AsyncMap<String, ServiceInfoHolder> resultMap) {
    resultMap.get(GlobalKeyHolder.SERVICE_HOLDER, onSuccess(holder -> {
        logDebug("get Holder " + holder + " this:" + this);
        if (holder != null) {
            final List<ServiceInfo> serviceHolders = holder.getAll();
            serviceHolders.forEach(this::pingService);

        }

    }));
}
 
Example #6
Source File: ServiceRegistry.java    From vert.x-microservice with Apache License 2.0 5 votes vote down vote up
private void createNewEntry(final AsyncMap resultMap, final ServiceInfo info, final ServiceInfoHolder holder, final Message<byte[]> message) {
    holder.add(info);
    logDebug("add result holder");
    resultMap.put(GlobalKeyHolder.SERVICE_HOLDER, holder, onSuccess(s -> {
        publishToEntryPoint(info);
        message.reply(true);
        logDebug("Register ADD: " + info);
    }));
}
 
Example #7
Source File: ServiceRegistry.java    From vert.x-microservice with Apache License 2.0 5 votes vote down vote up
private void addServiceEntry(final AsyncMap resultMap, final ServiceInfo info, final ServiceInfoHolder holder, final Message<byte[]> message) {
    holder.add(info);
    logDebug("update result holder");
    resultMap.replace(GlobalKeyHolder.SERVICE_HOLDER, holder, onSuccess(s -> {
        publishToEntryPoint(info);
        message.reply(true);
        logDebug("Register REPLACE: " + info);
    }));
}
 
Example #8
Source File: ServiceRegistry.java    From vert.x-microservice with Apache License 2.0 5 votes vote down vote up
private void getServiceHolderAndRegister(Message<byte[]> message, ServiceInfo info, AsyncMap<String, ServiceInfoHolder> resultMap) {
    logDebug("got map");
    resultMap.get(GlobalKeyHolder.SERVICE_HOLDER, onSuccess(resultHolder -> {
        logDebug("got result holder");
        if (resultHolder != null) {
            addServiceEntry(resultMap, info, resultHolder, message);
        } else {
            createNewEntry(resultMap, info, new ServiceInfoHolder(), message);
        }
    }));
}
 
Example #9
Source File: ServiceRegistry.java    From vert.x-microservice with Apache License 2.0 5 votes vote down vote up
private void getServiceHolderAndReplyToServiceInfoRequest(Message<byte[]> message, AsyncMap<String, ServiceInfoHolder> resultMap) {
    resultMap.get(GlobalKeyHolder.SERVICE_HOLDER, onSuccess(resultHolder -> {
        if (resultHolder != null) {
            message.reply(getServiceIfoHolderBinary(buildServiceInfoForEntryPoint(resultHolder)));
        } else {
            message.reply(getServiceIfoHolderBinary(new ServiceInfoHolder()));
        }
    }));
}
 
Example #10
Source File: WSClusterHandler.java    From vert.x-microservice with Apache License 2.0 5 votes vote down vote up
private void findEndpointAndRemove(ServerWebSocket serverSocket, String binaryHandlerID, String textHandlerID, AsyncMap<String, WSEndpointHolder> registryMap, WSEndpointHolder wsEndpointHolder) {
    final List<WSEndpoint> all = wsEndpointHolder.getAll();
    final Optional<WSEndpoint> first = all.stream().filter(e -> e.getBinaryHandlerId().equals(binaryHandlerID) && e.getTextHandlerId().equals(textHandlerID)).findFirst();
    if (first.isPresent()) {
        first.ifPresent(endpoint -> {
            wsEndpointHolder.remove(endpoint);
            registryMap.replace(WS_ENDPOINT_HOLDER, wsEndpointHolder, replaceHolder -> log("OK REMOVE: " + serverSocket.binaryHandlerID() + "  succeed:" + replaceHolder.succeeded()));
        });
    }
}
 
Example #11
Source File: WSClusterHandler.java    From vert.x-microservice with Apache License 2.0 5 votes vote down vote up
private void retrieveEndpointHolderAndRemove(ServerWebSocket serverSocket, String binaryHandlerID, String textHandlerID, AsyncMap<String, WSEndpointHolder> registryMap, AsyncResult<WSEndpointHolder> wsEndpointHolder) {
    if (wsEndpointHolder.succeeded()) {
        final WSEndpointHolder result = wsEndpointHolder.result();
        if (result != null) {
            findEndpointAndRemove(serverSocket, binaryHandlerID, textHandlerID, registryMap,result);

        }
    }
}
 
Example #12
Source File: WSClusterHandler.java    From vert.x-microservice with Apache License 2.0 5 votes vote down vote up
private void addDefinitionToRegistry(ServerWebSocket serverSocket, EventBus eventBus, String path, WSEndpoint endpoint, AsyncMap<String, WSEndpointHolder> registryMap, WSEndpointHolder wsEndpointHolder) {
    wsEndpointHolder.add(endpoint);
    registryMap.replace(WS_ENDPOINT_HOLDER, wsEndpointHolder, s -> {
                if (s.succeeded()) {
                    log("OK REPLACE: " + serverSocket.binaryHandlerID() + "  Thread" + Thread.currentThread());
                    sendToWSService(serverSocket, eventBus, path, endpoint);
                }
            }
    );
}
 
Example #13
Source File: WSClusterHandler.java    From vert.x-microservice with Apache License 2.0 5 votes vote down vote up
private void createEntryAndAddDefinition(ServerWebSocket serverSocket, EventBus eventBus, String path, WSEndpoint endpoint, AsyncMap<String, WSEndpointHolder> registryMap) {
    final WSEndpointHolder holder = new WSEndpointHolder();
    holder.add(endpoint);
    registryMap.put(WS_ENDPOINT_HOLDER, holder, s -> {
                if (s.succeeded()) {
                    log("OK ADD: " + serverSocket.binaryHandlerID() + "  Thread" + Thread.currentThread());
                    sendToWSService(serverSocket, eventBus, path, endpoint);
                }
            }

    );
}
 
Example #14
Source File: WSClusterHandler.java    From vert.x-microservice with Apache License 2.0 5 votes vote down vote up
private void updateWSEndpointHolder(ServerWebSocket serverSocket, AsyncMap<String, WSEndpointHolder> registryMap, AsyncResult<WSEndpointHolder> wsEndpointHolder) {
    log("add entry: " + Thread.currentThread());
    final String binaryHandlerId = serverSocket.binaryHandlerID();
    final String textHandlerId = serverSocket.textHandlerID();
    final String path = serverSocket.path();
    final EventBus eventBus = vertx.eventBus();
    final WSEndpoint endpoint = new WSEndpoint(binaryHandlerId, textHandlerId, path);
    final WSEndpointHolder result = wsEndpointHolder.result();
    if (result != null) {
        addDefinitionToRegistry(serverSocket, eventBus, path, endpoint, registryMap, result);
    } else {
        createEntryAndAddDefinition(serverSocket, eventBus, path, endpoint, registryMap);
    }
}
 
Example #15
Source File: WSClusterHandler.java    From vert.x-microservice with Apache License 2.0 5 votes vote down vote up
private void getEndpointHolderAndAdd(ServerWebSocket serverSocket, AsyncMap<String, WSEndpointHolder> registryMap) {
    registryMap.get(WS_ENDPOINT_HOLDER, wsEndpointHolder -> {
        if (wsEndpointHolder.succeeded()) {
            updateWSEndpointHolder(serverSocket, registryMap, wsEndpointHolder);
        }
    });

}
 
Example #16
Source File: AtomixClusterManager.java    From atomix-vertx with Apache License 2.0 5 votes vote down vote up
@Override
public <K, V> void getAsyncMap(String name, Handler<AsyncResult<AsyncMap<K, V>>> handler) {
  atomix.<K, V>atomicMapBuilder(name)
      .withSerializer(createSerializer())
      .buildAsync()
      .whenComplete(VertxFutures.convertHandler(
          handler, map -> new AtomixAsyncMap<>(vertx, map.async()), vertx.getOrCreateContext()));
}
 
Example #17
Source File: InfinispanAsyncMap.java    From vertx-infinispan with Apache License 2.0 5 votes vote down vote up
/**
 * Unwraps a generic {@link AsyncMap} to an {@link InfinispanAsyncMap}.
 *
 * @throws IllegalArgumentException if underlying implementation is not Infinispan
 */
@SuppressWarnings("unchecked")
static <K, V> InfinispanAsyncMap<K, V> unwrap(AsyncMap asyncMap) {
  if (asyncMap instanceof WrappedAsyncMap) {
    WrappedAsyncMap wrappedAsyncMap = (WrappedAsyncMap) asyncMap;
    AsyncMap delegate = wrappedAsyncMap.getDelegate();
    if (delegate instanceof InfinispanAsyncMap) {
      return (InfinispanAsyncMap<K, V>) delegate;
    }
  }
  throw new IllegalArgumentException(String.valueOf(asyncMap != null ? asyncMap.getClass() : null));
}
 
Example #18
Source File: InfinispanClusterManager.java    From vertx-infinispan with Apache License 2.0 5 votes vote down vote up
@Override
public <K, V> void getAsyncMap(String name, Promise<AsyncMap<K, V>> promise) {
  vertx.executeBlocking(prom -> {
    EmbeddedCacheManagerAdmin administration = cacheManager.administration().withFlags(CacheContainerAdmin.AdminFlag.VOLATILE);
    Cache<byte[], byte[]> cache = administration.getOrCreateCache(name, "__vertx.distributed.cache.configuration");
    prom.complete(new InfinispanAsyncMapImpl<>(vertx, cache));
  }, false, promise);
}
 
Example #19
Source File: AsyncMapFactory.java    From okapi with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an AsyncMap.
 *
 * @param <K> Key type
 * @param <V> Value type
 * @param vertx Vert.x handle
 * @param mapName name of the map. If null, will always create a local map
 * @param fut future
 */
public static <K, V> void create(Vertx vertx, String mapName,
                                 Handler<ExtendedAsyncResult<AsyncMap<K, V>>> fut) {
  SharedData shared = vertx.sharedData();
  if (vertx.isClustered() && mapName != null) {
    shared.<K, V>getClusterWideMap(mapName, res -> {
      if (res.succeeded()) {
        fut.handle(new Success<>(res.result()));
      } else {
        fut.handle(new Failure<>(ErrorType.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.
    String id = vertx.getOrCreateContext().deploymentID();
    if (mapName != null) {
      id = mapName + id;
    }
    shared.<K, V>getLocalAsyncMap(id, res -> {
      if (res.succeeded()) {
        fut.handle(new Success<>(res.result()));
      } else {
        fut.handle(new Failure<>(ErrorType.INTERNAL, res.cause()));
      }
    });
  }
}
 
Example #20
Source File: NearCacheSessionStoreIT.java    From vertx-vaadin with MIT License 5 votes vote down vote up
private void getRemoteMap(TestContext context, Handler<AsyncResult<AsyncMap<String, Session>>> resultHandler) {
    if (remoteMap == null) {
        rule.vertx().sharedData().<String, Session>getClusterWideMap(NearCacheSessionStore.DEFAULT_SESSION_MAP_NAME, res -> {
            if (res.succeeded()) {
                remoteMap = res.result();
                resultHandler.handle(Future.succeededFuture(res.result()));
            } else {
                resultHandler.handle(res);
            }
        });
    } else {
        resultHandler.handle(Future.succeededFuture(remoteMap));
    }
}
 
Example #21
Source File: LealoneVertxClusterManager.java    From Lealone-Plugins with Apache License 2.0 4 votes vote down vote up
@Override
public <K, V> void getAsyncMap(String name, Handler<AsyncResult<AsyncMap<K, V>>> resultHandler) {
    VertxAsyncMap<K, V> map = new VertxAsyncMap<>();
    resultHandler.handle(Future.succeededFuture(map));
}
 
Example #22
Source File: Examples.java    From vertx-infinispan with Apache License 2.0 4 votes vote down vote up
public <K, V> void asyncMapStreams(AsyncMap<K, V> asyncMap) {
  InfinispanAsyncMap<K, V> infinispanAsyncMap = InfinispanAsyncMap.unwrap(asyncMap);
  ReadStream<K> keyStream = infinispanAsyncMap.keyStream();
  ReadStream<V> valueStream = infinispanAsyncMap.valueStream();
  ReadStream<Map.Entry<K, V>> entryReadStream = infinispanAsyncMap.entryStream();
}
 
Example #23
Source File: IgniteClusterManager.java    From vertx-ignite with Apache License 2.0 4 votes vote down vote up
@Override
public <K, V> void getAsyncMap(String name, Promise<AsyncMap<K, V>> promise) {
  vertx.executeBlocking(prom -> prom.complete(new AsyncMapImpl<>(getCache(name), vertx)), promise);
}
 
Example #24
Source File: AsyncMapTest.java    From vertx-service-discovery with Apache License 2.0 4 votes vote down vote up
protected Future<AsyncMap<String, String>> getAsyncMap() {
  return vertx.sharedData().getLocalAsyncMap("some-name");
}
 
Example #25
Source File: ClusteredAsyncMapTest.java    From vertx-service-discovery with Apache License 2.0 4 votes vote down vote up
@Override
protected Future<AsyncMap<String, String>> getAsyncMap() {
  return vertx.sharedData().getClusterWideMap("some-name");
}
 
Example #26
Source File: HazelcastClusterManager.java    From vertx-hazelcast with Apache License 2.0 4 votes vote down vote up
@Override
public <K, V> void getAsyncMap(String name, Promise<AsyncMap<K, V>> promise) {
  promise.complete(new HazelcastAsyncMap<>(vertx, hazelcast.getMap(name)));
}