Skip to content
Stefan Domnanovits edited this page Mar 10, 2019 · 8 revisions

The saga lib ships with a Guice integration module. This makes it very easy to create new saga instances and have custom dependencies injected directly.

In fact it is much easier this way because with Guice it is not necessary to provide a SagaProviderFactory implemenation. Instead the saga-lib integration module provides its own class, forwarding all saga creation requests to the Guice injector.

To use the integration package reference the matching maven module.

<dependency>
   <groupId>com.codebullets.saga-lib</groupId>
   <artifactId>saga-lib-guice</artifactId>
   <version>3.3.0</version>
</dependency>

Starting the saga-lib

Starting the lib works almost the same as without Guice. There is specfic SagaModuleBuilder that has all the same configuration setup methods like the plain EventStreamBuilder from the base lib. The difference here is that it will not directly create a MessageStream instance. Instead a Guice module is created containing all the binding necessary to start the saga-lib. Once this binding module is available it can be used with Guice to start the DI container and request the MessageStream to handle messages.

// create the Guice module using saga-libs builder using default settings
Module sagaLibModule = SagaModuleBuilder.configure().build();

// here only the saga-lib module is used to configure Guice. Most
// likely you want to specify additional modules containing other
// custom bindings.
Injector injector = Guice.createInjector(sagaLibModule);
MessageStream msgStream = injector.getInstance(MessageStream.class);

// start using the saga-lib
msgStream.add(theMessage)

Writing Sagas

Writing the saga itself works the same as without Guice. However the benefit now is that there is no need to manually create a provider to inject all the dependencies. Instead just add the dependencies to the sagas constructor and Guice will take care to resolve them as needed.

public class LoginSaga : AbstractSingleEventSaga {
  private final AvailableUsers users;

  @Inject
  public LoginSaga(final AvailableUsers users) {
    this.users = users;
  }

  @StartsSaga
  public void userLoggedIn(final UserLoggedInMessage message) {
    users.add(message.getUser());
  }
}
Clone this wiki locally