Skip to content

Latest commit

 

History

History
105 lines (76 loc) · 2.93 KB

File metadata and controls

105 lines (76 loc) · 2.93 KB

transaction-outbox-guice

Guice on Maven Central Guice Javadoc Latest snapshot

Extension for transaction-outbox-core which integrates with Guice.

Installation

Stable releases

Maven

<dependency>
  <groupId>com.gruelbox</groupId>
  <artifactId>transactionoutbox-guice</artifactId>
  <version>5.5.447</version>
</dependency>

Gradle

implementation 'com.gruelbox:transactionoutbox-guice:5.5.447'

Development snapshots

See transactionoutbox-core for more information.

Standard usage

Configuration

To get a TransactionOutbox for use throughout your application, add a Singleton binding for your chosen transaction manager and then wire in GuiceInstantiator as follows:

@Provides
@Singleton
TransactionOutbox transactionOutbox(Injector injector, TransactionManager transactionManager) {
  return TransactionOutbox.builder()
    .transactionManager(transactionManager)
    .persistor(Persistor.forDialect(Dialect.MY_SQL_8))
    .instantiator(GuiceInstantiator.builder().injector(injector).build())
    .build();
}

Usage

@Inject private TransactionOutbox outbox;

void doSomething() {
  // Obtains a MyService from the injector
  outbox.schedule(MyService.class).doAThing(1, 2, 3);
}

Remote injection

Alternatively, you may prefer to hide the use of TransactionOutbox and create injectable "remote" implementations of specific services. This is a stylistic choice, and is more a Guice thing than a TransactionOutbox thing, but is presented here for illustration.

Configuration

Create a suitable binding annotation to specify that you want to inject the remote version of a service:

@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@BindingAnnotation
public @interface Remote {}

Bind TransactionOutbox as per the example above, and add two more bindings to expose the "real" and "remote" versions of the service:

@Provides
@Remote
@Singleton // Can help performance
MyService remote(TransactionOutbox outbox) {
  return outbox.schedule(MyService.class);
}

@Provides
MyService local() {
  return new MyService();
}

Usage

Now you can inject the remote implementation and use it to schedule work. The following is exactly equivalent to the usage example above, just using an injected remote:

@Inject
@Remote
private MyService myService;

void doSomething() {
  myService.doAThing(1, 2, 3);
}