Interface Instantiator


  • public interface Instantiator
    Provides callbacks for the creation and serialization of classes by TransactionOutbox.
    • Method Detail

      • usingReflection

        static Instantiator usingReflection()
        Creates an Instantiator which records the class name as its fully qualified name (e.g. com.gruelbox.example.EnterpriseBeanProxyFactoryFactory) and instantiates instances using reflection and a no-args constructor.

        This is the default used by TransactionOutbox if nothing else is specified.

        Returns:
        A reflection instantiator
      • using

        static Instantiator using​(Function<Class<?>,​Object> fn)
        Creates an Instantiator which records the class name as its fully qualified name (e.g. com.gruelbox.example.EnterpriseBeanProxyFactoryFactory) and instantiates instances using the supplied function, which takes the fully qualified name and should return an instance.

        This is a good option to use with dependency injection frameworks such as Guice:

        TransactionOutbox outbox = TransactionOutbox.builder()
         ...
         .instantiator(Instantiator.using(injector::getInstance))
         .build();
        Parameters:
        fn - A function to create an instance of the specified class.
        Returns:
        A reflection instantiator
      • getName

        String getName​(Class<?> clazz)
        Provides the name of the specified class. This may be the classes fully-qualified name, or may be an alias of some kind. This is up to the implementer.

        Not using the actual class name can be useful in avoiding a case where queued tasks end up referencing renamed classes following a refactor. It is also useful for DI frameworks such as Spring DI, which use named bindings by default.

        Parameters:
        clazz - The class to get the name of.
        Returns:
        The class name.
      • getInstance

        Object getInstance​(String name)
        Requests an instance of the named class, where the "name" is whatever is returned by getName(Class).

        A common use-case for this method is to return a class from a DI framework such as Guice (using an injected {code Injector}), but it is perfectly valid to simply instantiate the class by name and populate its dependencies directly.

        Parameters:
        name - The class "name" as returned by getName(Class).
        Returns:
        An instance of the class.