io.vertx.core.shareddata.SharedData Java Examples

The following examples show how to use io.vertx.core.shareddata.SharedData. 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: WSLocalHandler.java    From vert.x-microservice with Apache License 2.0 6 votes vote down vote up
@Override
public void findRouteSocketInRegistryAndRemove(ServerWebSocket serverSocket) {
    final SharedData sharedData = this.vertx.sharedData();
    final String binaryHandlerID = serverSocket.binaryHandlerID();
    final String textHandlerID = serverSocket.textHandlerID();
    final LocalMap<String, byte[]> wsRegistry = sharedData.getLocalMap(WS_REGISTRY);
    final WSEndpointHolder holder = getWSEndpointHolderFromSharedData(wsRegistry);
    if (holder != null) {
        final List<WSEndpoint> all = holder.getAll();
        final Optional<WSEndpoint> first = all.parallelStream().filter(e -> e.getBinaryHandlerId().equals(binaryHandlerID) && e.getTextHandlerId().equals(textHandlerID)).findFirst();
        first.ifPresent(endpoint -> {
            holder.remove(endpoint);
            wsRegistry.replace(WS_ENDPOINT_HOLDER, serialize(holder));
            log("OK REMOVE: " + serverSocket.binaryHandlerID());
        });
    }
}
 
Example #2
Source File: WSLocalHandler.java    From vert.x-microservice with Apache License 2.0 6 votes vote down vote up
@Override
public void replyToAllWS(Message<byte[]> message) {
    try {
        log("Reply to all: " + this);
        final WSMessageWrapper wrapper = (WSMessageWrapper) Serializer.deserialize(message.body());
        final String stringResult = TypeTool.trySerializeToString(wrapper.getBody());
        final byte[] payload = stringResult != null ? stringResult.getBytes() : Serializer.serialize(wrapper.getBody());

        final SharedData sharedData = this.vertx.sharedData();
        final LocalMap<String, byte[]> wsRegistry = sharedData.getLocalMap(WS_REGISTRY);
        final byte[] holderPayload = wsRegistry.get(WS_ENDPOINT_HOLDER);
        if (holderPayload != null) {
            final WSEndpointHolder holder = (WSEndpointHolder) deserialize(holderPayload);
            final List<WSEndpoint> all = holder.getAll();
            all.parallelStream().
                    filter(endP -> endP.getUrl().equals(wrapper.getEndpoint().getUrl())).
                    forEach(
                            endpoint -> replyToEndpoint(stringResult, payload, endpoint)
                    );
        }


    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }
}
 
Example #3
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 #4
Source File: LocalMapValueParamInjector.java    From nubes with Apache License 2.0 5 votes vote down vote up
@Override
public Object resolve(RoutingContext context, LocalMapValue annotation, String paramName, Class<?> resultClass) {
  SharedData sd = context.vertx().sharedData();
  String mapName = annotation.mapName();
  String key = annotation.key();
  if ("".equals(key)) {
    key = paramName;
  }
  io.vertx.core.shareddata.LocalMap<Object, Object> map = sd.getLocalMap(mapName);
  return map.get(key);
}
 
Example #5
Source File: LocalMapParamInjector.java    From nubes with Apache License 2.0 5 votes vote down vote up
@Override
public Object resolve(RoutingContext context, VertxLocalMap annotation, String paramName, Class<?> resultClass) {
  SharedData sd = context.vertx().sharedData();
  String mapName = annotation.value();
  if ("".equals(mapName)) {
    mapName = paramName;
  }
  return sd.getLocalMap(mapName);
}
 
Example #6
Source File: WSClusterHandler.java    From vert.x-microservice with Apache License 2.0 5 votes vote down vote up
@Override
public void findRouteToWSServiceAndRegister(ServerWebSocket serverSocket) {
    final SharedData sharedData = this.vertx.sharedData();
    sharedData.<String, ServiceInfoHolder>getClusterWideMap(REGISTRY, onSuccess(resultMap ->
                    resultMap.get(GlobalKeyHolder.SERVICE_HOLDER, onSuccess(resultHolder -> findServiceEntryAndRegisterWS(serverSocket, resultHolder, sharedData)))
    ));
}
 
Example #7
Source File: WSClusterHandler.java    From vert.x-microservice with Apache License 2.0 5 votes vote down vote up
private void findServiceEntryAndRegisterWS(final ServerWebSocket serverSocket, final ServiceInfoHolder resultHolder, final SharedData sharedData) {
    if (resultHolder != null) {
        final String path = serverSocket.path();
        log("find entry : " + path);
        final Optional<Operation> operationResult = findServiceInfoEntry(resultHolder, path);
        operationResult.ifPresent(op ->
                        createEndpointDefinitionAndRegister(serverSocket, sharedData)
        );
    }
}
 
Example #8
Source File: ServiceRegistry.java    From vert.x-microservice with Apache License 2.0 5 votes vote down vote up
private void getSharedRegistryAndPing() {
    final SharedData sharedData = this.vertx.sharedData();
    sharedData.<String, ServiceInfoHolder>getClusterWideMap(GlobalKeyHolder.REGISTRY_MAP_KEY, onSuccess(resultMap -> {
                logDebug("resultMap " + resultMap);
                getServiceHolderAndPingServices(resultMap);
            }
    ));
}
 
Example #9
Source File: MockServiceSessionSetup.java    From vertx-vaadin with MIT License 4 votes vote down vote up
public MockServiceSessionSetup(boolean sessionAvailable)
    throws Exception {
    MockitoAnnotations.initMocks(this);
    SharedData sharedData = Mockito.mock(SharedData.class);
    Mockito.when(vertx.sharedData()).thenReturn(sharedData);
    Mockito.when(sharedData.getLocalMap(Matchers.anyString())).thenReturn(Mockito.mock(LocalMap.class));
    Mockito.when(vertx.eventBus()).thenReturn(Mockito.mock(EventBus.class));
    Mockito.when(vertx.fileSystem()).thenReturn(fileSystem);
    Mockito.when(vertx.getOrCreateContext()).thenReturn(context);

    JsonObject config = new JsonObject();
    Mockito.when(context.config()).thenReturn(config);

    vertxVaadin = new TestVertxVaadin(vertx);

    deploymentConfiguration.setXsrfProtectionEnabled(false);
    //Mockito.when(servletConfig.getServletContext())
    //        .thenReturn(servletContext);


    if (sessionAvailable) {
        Mockito.when(session.getConfiguration())
            .thenReturn(deploymentConfiguration);

        Mockito.when(session.getBrowser()).thenReturn(browser);
        Mockito.when(session.getPushId()).thenReturn("fake push id");
        Mockito.when(session.getLocale()).thenReturn(Locale.ENGLISH);

        //Mockito.when(wrappedSession.getHttpSession())
        //        .thenReturn(httpSession);

        Mockito.when(session.getService()).thenAnswer(i -> service);
        Mockito.when(session.hasLock()).thenReturn(true);
        Mockito.when(session.getPendingAccessQueue())
            .thenReturn(new LinkedBlockingDeque<>());
        //Mockito.when(request.getWrappedSession())
        //        .thenReturn(wrappedSession);
    } else {
        session = null;
    }
    //servlet.init(servletConfig);

    CurrentInstance.set(VaadinRequest.class, request);
    CurrentInstance.set(VaadinService.class, service);
    if (sessionAvailable) {
        CurrentInstance.set(VaadinSession.class, session);
    }

    //Mockito.when(request.getServletPath()).thenReturn("");
    Mockito.when(browser.isEs6Supported()).thenReturn(true);

}
 
Example #10
Source File: SfsVertxImpl.java    From sfs with Apache License 2.0 4 votes vote down vote up
@Override
public SharedData sharedData() {
    return vertx.sharedData();
}
 
Example #11
Source File: WSClusterHandler.java    From vert.x-microservice with Apache License 2.0 4 votes vote down vote up
private void createEndpointDefinitionAndRegister(ServerWebSocket serverSocket, final SharedData sharedData) {
    sharedData.<String, WSEndpointHolder>getClusterWideMap(WS_REGISTRY, onSuccess(registryMap ->
                    getEndpointHolderAndAdd(serverSocket, registryMap)
    ));
}
 
Example #12
Source File: WSLocalHandler.java    From vert.x-microservice with Apache License 2.0 3 votes vote down vote up
private void createEndpointDefinitionAndRegister(ServerWebSocket serverSocket) {
    final SharedData sharedData = this.vertx.sharedData();
    final LocalMap<String, byte[]> wsRegistry = sharedData.getLocalMap(WS_REGISTRY);
    final WSEndpointHolder holder = getWSEndpointHolderFromSharedData(wsRegistry);
    final String path = serverSocket.path();
    final WSEndpoint endpoint = new WSEndpoint(serverSocket.binaryHandlerID(), serverSocket.textHandlerID(), path);

    replaceOrAddEndpoint(wsRegistry, holder, endpoint);

    sendToWSService(serverSocket, path, endpoint);


}