Skip to content
This repository has been archived by the owner on May 28, 2024. It is now read-only.
/ guice-bootstrap Public archive

Guice with JSR 250 Lifecycle annotations (@ PostConstruct and @ PreDestroy)

License

Notifications You must be signed in to change notification settings

embulk/guice-bootstrap

Repository files navigation

Guice-bootstrap (org.embulk:guice-bootstrap) is no longer maintained.

If you wanted to use this updated, maintain your fork with taking care of its license.

This is licensed under the Apache License, Version 2.0, but it has some modified copies from Airlift's bootstrap.


Guice-bootstrap

Guice-bootstrap is an extension of Guice that adds support for JSR 250 Life Cycle annotations.

@PostConstruct annotation is used on methods that need to get executed after dependency injection is done to perform any initialization.

@PreDestroy annotation is used on methods that are called before the application shuts down.

PostConstruct

Methods with @PostConstruct annotation are called after all depedency injection is done.

import javax.annotation.PostConstruct;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class MyService implements Runnable
{
    private final ExecutorService executor = Executors.newSingleThreadExecutor();

    @PostConstruct
    public void start() {
        executor.submit(() -> { ... });
    }
}

PreDestroy

Methods with @PreDestroy annotation are called after the application shuts down.

import javax.annotation.PreDestroy;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

class MyService implements Runnable
{
    private final List<File> files = new ArrayList<>();

    @PreDestroy
    public void cleanup() {
        files.stream().forEach(file -> file.delete());
    }
}

Bootstrap

Bootstrap is the main class to use guice-bootstrap.

Injector injector = new Bootstrap()
    .addModules(new MyGuiceModule1(), new MyGuiceModule2(), ...)
    .addModules(new MyGuiceModule3(), new MyGuiceModule4(), ...)
    .initialize();

Overriding bindings

Bootstrap.overrideModulesWith method allows you to override bindings. This is useful to customize bindings defined by a base class.

// The base class
public class MyService {
    public Bootstrap bootstrap()
    {
        return new Bootstrap()
            .addModules(new MyGuiceModule1(), new MyGuiceModule2(), ...)
            .addModules(new MyGuiceModule3(), new MyGuiceModule4(), ...)
            ;
    }

    public void start()
    {
        Injector injector = bootstrap().initialize();
        ...
    }
}

// Extending class that overrides some bindings
public class MyExtendedService extend MyService {
    @Override
    public Bootstrap bootstrap()
    {
        return super()
            .overrideModulesWith(new MyGuiceModule4(), ...)
            ;
    }
}

CloseableInjector

Bootstrap.initialize() sets up a shutdown hook to the Java VM (@Runtime.addShutdownHook`). It ensures that PostDestroy methods are called when Java VM exits even if it's killed by a SIGTERM or Ctrl-C.

But if you want to control the exact timing of shutdown, you can use Bootstrap.initializeCloseable() instead. It returns CloseableInjector which implements Injector and Closeable interfaces.

Bootstrap bootstrap = new Bootstrap()
    .addModules(...);

try (CloseableInjector injector = bootstrap.initializeCloseable()) {
    ...
}

About

Guice with JSR 250 Lifecycle annotations (@ PostConstruct and @ PreDestroy)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages