All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.skuzzle.inject.async.TriggerStrategy Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package de.skuzzle.inject.async;


import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ServiceLoader;
import java.util.concurrent.ScheduledExecutorService;

import de.skuzzle.inject.async.annotation.Trigger;
import de.skuzzle.inject.async.util.InjectedMethodInvocation;

/**
 * Defines how a certain {@link Trigger} annotation is handled in order to
 * extract scheduling meta information and to actually schedule method
 * invocations.
 * 

* The framework searches for implementations of this class using the Java's * {@link ServiceLoader}. In order to implement your own trigger, just follow these steps: *

*
    *
  1. Create a new Trigger annotation which will hold scheduling meta information: *
     * @Trigger
     * @Retention(RetentionPolicy.RUNTIME)
     * @Target(ElementType.METHOD)
     * public @interface CustomTrigger {
     *     // meta information fields
     * }
     * 
    *
  2. *
  3. Create an implementation of this interface to handle the new annotation: *
     * public class CustomTriggerStrategy implements TriggerStrategy {
     *
     *     // Field injection is allowed
     *     @Inject
     *     private Injector injector;
     *
     *     // public no-argument constructor required (because of the ServiceLoader)!
     *
     *     @Override
     *     public Class<CustomTrigger> getTriggerType() {
     *         return CustomTrigger.class;
     *     }
     *
     *     @Override
     *     public void schedule(Method method, Object self,
     *             ScheduledExecutorService executor) {
     *
     *         // read trigger annotation from the method
     *         CustomTrigger trigger = method.getAnnotation(getTriggerType());
     *
     *         // use meta information from the trigger to schedule the execution with the
     *         // provided executor (might want to use InjectedMethodInvocation class)
     *         executor.schedule(...);
     *     }
     * }
     * 
    *
  4. *
  5. Register your implementation for the ServiceLoader. Create the folder structure * 'META-INF/services'. Within the services folder create a file named * 'de.skuzzle.inject.async.TriggerStrategy'. Put a single line which contains the * full qualified class name of your CustomTriggerStrategy into that file.
  6. *
* @author Simon Taddiken */ public interface TriggerStrategy { /** * Returns the annotation type that this strategy can handle. * * @return The annotation type. */ Class getTriggerType(); /** * Extracts scheduling information from the provided {@link Method} and then * schedules invocations of that method according to the information. * *

* To support invocation of parameterized methods, implementors can refer to * {@link InjectedMethodInvocation} to inject actual parameters of a method. *

* * @param method The method to schedule. * @param self The object to invoke the method on. * @param executor The executor to use for scheduling. */ void schedule(Method method, Object self, ScheduledExecutorService executor); }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy