Java Code Examples for io.reactivex.Completable#defer()

The following examples show how to use io.reactivex.Completable#defer() . 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: FCSPlugin.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Override
public Completable deployToResteasy(VertxResteasyDeployment deployment) {
	return Completable.defer(() -> {
		JsonArray packages = AppGlobals.get().getConfig().getJsonArray("scan");
		if(packages == null) {
			System.err.println("Not scanning any packages, please specify the 'scan' array of packages in configuration");
		}else {
			String[] packagesToScan = (String[]) packages.getList().toArray(new String[packages.size()]);
			new FastClasspathScanner(packagesToScan)
			.matchClassesWithAnnotation(Path.class, klass -> {
				if(!Modifier.isAbstract(klass.getModifiers()))
					deployment.getActualResourceClasses().add(klass);
			})
			.matchClassesWithAnnotation(Provider.class, klass -> {
				if(!Modifier.isAbstract(klass.getModifiers()))
					deployment.getActualProviderClasses().add(klass);
			})
			.scan();
		}
		return super.deployToResteasy(deployment);
	});
}
 
Example 2
Source File: Server.java    From redpipe with Apache License 2.0 6 votes vote down vote up
private Completable setupVertx(VertxResteasyDeployment deployment) {
   	return Completable.defer(() -> {
   		// Get a DB
   		SQLClient dbClient = createDbClient(AppGlobals.get().getConfig());

   		Class<?> mainClass = null;
   		for (Class<?> resourceClass : deployment.getActualResourceClasses()) {
   			if(resourceClass.getAnnotation(MainResource.class) != null){
   				mainClass = resourceClass;
   				break;
   			}
   		}

   		// Save our injected globals
   		AppGlobals globals = AppGlobals.get();
   		globals.setDbClient(dbClient);
   		globals.setMainClass(mainClass);
   		globals.setDeployment(deployment);

   		return doOnPlugins(plugin -> plugin.init())
   				.concatWith(startVertx(deployment));
   	});
}
 
Example 3
Source File: CdiPlugin.java    From redpipe with Apache License 2.0 5 votes vote down vote up
@Override
public Completable init() {
	return Completable.defer(() -> {
		// Setup the Vertx-CDI integration
		VertxExtension vertxExtension = CDI.current().select(VertxExtension.class).get();
		BeanManager beanManager = CDI.current().getBeanManager();
		// has to be done in a blocking thread
		Vertx vertx = AppGlobals.get().getVertx();
		return vertx.rxExecuteBlocking(future -> {
			vertxExtension.registerConsumers(vertx.getDelegate(), BeanManagerProxy.unwrap(beanManager).event());
			future.complete();
		}).ignoreElement();
	});
}
 
Example 4
Source File: CdiPlugin.java    From redpipe with Apache License 2.0 5 votes vote down vote up
@Override
public Completable shutdown() {
	return Completable.defer(() -> {
		weld.shutdown();
		return super.shutdown();
	});
}
 
Example 5
Source File: SQLUtil.java    From redpipe with Apache License 2.0 5 votes vote down vote up
public static Completable doInConnectionCompletable(Func1<? super SQLConnection, ? extends Completable> func){
	return Completable.defer(() -> {
		Single<SQLConnection> connection = getConnection();
		return connection.flatMapCompletable(conn -> {
			return func.call(conn).doAfterTerminate(() -> {
				conn.close();
			});
		});
	});
}
 
Example 6
Source File: Server.java    From redpipe with Apache License 2.0 5 votes vote down vote up
private Completable setupTemplateRenderers() {
	return Completable.defer(() -> {
		List<TemplateRenderer> renderers = new ArrayList<>();
		for(TemplateRenderer renderer : ServiceLoader.load(TemplateRenderer.class))
			renderers.add(renderer);
		AppGlobals.get().setTemplateRenderers(renderers);
		return Completable.complete();
	});
}
 
Example 7
Source File: Server.java    From redpipe with Apache License 2.0 5 votes vote down vote up
private Completable doOnPlugins(Function<Plugin, Completable> operation){
	return Completable.defer(() -> {
		Completable last = Completable.complete();
		for(Plugin plugin : plugins) {
			last = last.concatWith(operation.apply(plugin));
		}
		return last;
	});
}
 
Example 8
Source File: Server.java    From redpipe with Apache License 2.0 5 votes vote down vote up
private Completable startVertx(VertxResteasyDeployment deployment)
{
	return Completable.defer(() -> {
		Router router = Router.router(vertx);
		AppGlobals globals = AppGlobals.get();
		globals.setRouter(router);

		VertxPluginRequestHandler resteasyHandler = new VertxPluginRequestHandler(vertx, deployment, plugins);

		return doOnPlugins(plugin -> plugin.preRoute())
				.doOnComplete(() -> {
					setupRoutes(router);
					router.route().handler(routingContext -> {
						ResteasyProviderFactory.pushContext(RoutingContext.class, routingContext);
						ResteasyProviderFactory.pushContext(io.vertx.rxjava.ext.web.RoutingContext.class, 
								io.vertx.rxjava.ext.web.RoutingContext.newInstance(routingContext.getDelegate()));
						resteasyHandler.handle(routingContext.request());
					});
				}).concatWith(doOnPlugins(plugin -> plugin.postRoute()))
				.concatWith(Completable.defer(() -> {
					// Start the front end server using the Jax-RS controller
					int port = globals.getConfig().getInteger("http_port", 9000);
					String host = globals.getConfig().getString("http_host", NetServerOptions.DEFAULT_HOST);
					return vertx.createHttpServer()
							.requestHandler(router::accept)
							.rxListen(port, host)
							.doOnSuccess(server -> System.out.println("Server started on port " + server.actualPort()))
							.doOnError(t -> t.printStackTrace())
							.ignoreElement();
				}));
	});
}
 
Example 9
Source File: TrackedEntityAttributeReservedValueManager.java    From dhis2-android-sdk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Completable downloadValuesIfBelowThreshold(String attribute,
                                                   OrganisationUnit organisationUnit,
                                                   Integer minNumberOfValuesToHave,
                                                   BooleanWrapper systemInfoDownloaded,
                                                   boolean storeError) {
    return Completable.defer(() -> {
        // TODO use server date
        store.deleteExpired(new Date());

        Integer fillUpTo = getFillUpToValue(minNumberOfValuesToHave);

        Integer remainingValues = organisationUnit == null ?
                store.count(attribute) : store.count(attribute, organisationUnit.uid());

        // If number of values is explicitly specified, we use that value as threshold.
        Integer minNumberToTryFill = minNumberOfValuesToHave == null ?
                (int) (fillUpTo * FACTOR_TO_REFILL) : minNumberOfValuesToHave;

        if (remainingValues < minNumberToTryFill) {
            Integer numberToReserve = fillUpTo - remainingValues;

            return downloadValues(attribute, organisationUnit, numberToReserve, systemInfoDownloaded, storeError);
        } else {
            return Completable.complete();
        }
    });
}
 
Example 10
Source File: Server.java    From redpipe with Apache License 2.0 4 votes vote down vote up
private Completable setupPlugins() {
	return Completable.defer(() -> {
		loadPlugins();
		return doOnPlugins(plugin -> plugin.preInit());
	});
}