Example usage for java.util.concurrent CompletableFuture obtrudeException

List of usage examples for java.util.concurrent CompletableFuture obtrudeException

Introduction

In this page you can find the example usage for java.util.concurrent CompletableFuture obtrudeException.

Prototype

public void obtrudeException(Throwable ex) 

Source Link

Document

Forcibly causes subsequent invocations of method #get() and related methods to throw the given exception, whether or not already completed.

Usage

From source file:com.canoo.dolphin.client.ClientContextFactory.java

/**
 * Create a {@link ClientContext} based on the given configuration. This method doesn't block and returns a
 * {@link CompletableFuture} to receive its result. If the {@link ClientContext} can't be created the
 * {@link CompletableFuture#get()} will throw a {@link ClientInitializationException}.
 *
 * @param clientConfiguration the configuration
 * @return the future/*  w w  w  .  j  a  va  2 s  .co  m*/
 */
public static CompletableFuture<ClientContext> connect(final ClientConfiguration clientConfiguration) {
    Assert.requireNonNull(clientConfiguration, "clientConfiguration");
    final CompletableFuture<ClientContext> result = new CompletableFuture<>();

    Level openDolphinLogLevel = clientConfiguration.getDolphinLogLevel();
    Logger openDolphinLogger = Logger.getLogger("org.opendolphin");
    openDolphinLogger.setLevel(openDolphinLogLevel);

    Executors.newSingleThreadExecutor().execute(() -> {
        try {
            final ForwardableCallback<DolphinRemotingException> remotingErrorHandler = new ForwardableCallback<>();
            final ClientDolphin clientDolphin = new ClientDolphin();
            clientDolphin.setClientModelStore(new ClientModelStore(clientDolphin));
            final HttpClient httpClient = new DefaultHttpClient(new PoolingClientConnectionManager());
            final ClientConnector clientConnector = new DolphinPlatformHttpClientConnector(clientDolphin,
                    new OptimizedJsonCodec(), httpClient, clientConfiguration.getServerEndpoint(),
                    remotingErrorHandler, clientConfiguration.getUiThreadHandler());
            clientDolphin.setClientConnector(clientConnector);
            final DolphinCommandHandler dolphinCommandHandler = new DolphinCommandHandler(clientDolphin);
            final EventDispatcher dispatcher = new ClientEventDispatcher(clientDolphin);
            final BeanRepository beanRepository = new BeanRepositoryImpl(clientDolphin, dispatcher);
            final Converters converters = new Converters(beanRepository);
            final PresentationModelBuilderFactory builderFactory = new ClientPresentationModelBuilderFactory(
                    clientDolphin);
            final ClassRepository classRepository = new ClassRepositoryImpl(clientDolphin, converters,
                    builderFactory);
            final ListMapper listMapper = new ListMapperImpl(clientDolphin, classRepository, beanRepository,
                    builderFactory, dispatcher);
            final BeanBuilder beanBuilder = new ClientBeanBuilderImpl(classRepository, beanRepository,
                    listMapper, builderFactory, dispatcher);
            final ClientPlatformBeanRepository platformBeanRepository = new ClientPlatformBeanRepository(
                    clientDolphin, beanRepository, dispatcher, converters);
            final ClientBeanManagerImpl clientBeanManager = new ClientBeanManagerImpl(beanRepository,
                    beanBuilder, clientDolphin);
            final ControllerProxyFactory controllerProxyFactory = new ControllerProxyFactoryImpl(
                    platformBeanRepository, dolphinCommandHandler, clientDolphin);
            final ClientContext clientContext = new ClientContextImpl(clientConfiguration, clientDolphin,
                    controllerProxyFactory, dolphinCommandHandler, platformBeanRepository, clientBeanManager,
                    remotingErrorHandler);
            clientDolphin.startPushListening(PlatformConstants.POLL_EVENT_BUS_COMMAND_NAME,
                    PlatformConstants.RELEASE_EVENT_BUS_COMMAND_NAME);
            clientConfiguration.getUiThreadHandler()
                    .executeInsideUiThread(() -> result.complete(clientContext));
        } catch (Exception e) {
            result.obtrudeException(new ClientInitializationException("Can not connect to server!", e));
            throw new ClientInitializationException(e);
        }
    });
    return result;
}