Skip to content

Getting started

Simon Taddiken edited this page Jan 26, 2020 · 5 revisions

This guice extension is driven by Annotation based configuration of scheduling plans and behavior. In order to hook this extension into your guice environment all you need to do is to enable it for a Binder or install it as a module.

Simple set up

The simple setup hooks the full async framework into your Injector instance with sensitive default settings and no way to customize the behavior. That should do it for most users.

public class MyModule extends AbstractModule {

    @Override
    public void configure() {
        GuiceAsync.enableFor(binder());
    }
}

The second possibility is to obtain a Module which can directly be passed to guice when creating your Injector instance:

    Injector injector = Guice.createInjector(yourModule1, yourModule2, GuiceAsync.createModule());

After enabling the extension, you can use the annotations to define scheduled and asynchronous methods.

Advanced set up

The advanced set up allows you to choose which feature to enable and to customize certain aspects of those features. This is helpful if, for example, you only want @Scheduled methods but don't need @Async methods.

public class MyModule extends AbstractModule {

    @Override
    public void configure() {
        // only enable `@Scheduled` annotation handling
        GuiceAsync.enableFeaturesFor(binder(), DefaultFeatures.SCHEDULE);
    }
}

In case of the scheduling feature it is also possible to override some default settings with custom values.

ScheduleProperties disableAutoScheduling = ScheduleProperties.defaultProperties()
                        .disableAutoScheduling();
GuiceAsync.enableFeaturesFor(binder(), ScheduleFeature.withProperties(disableAutoScheduling));

(See also: Manual scheduling).

Clean up

Since version 1.2.0 the framework offers a way for properly shutting down the internally used thread pools. If your application has a defined exit point it is encouraged to shut down the threads to allow for a clean exit.

@Inject
private GuiceAsyncService guiceAsyncService;

// ...
guiceAsyncService.shutdown(1000, TimeUnit.MILLISECONDS);