Advertisement
smiche

Untitled

Mar 23rd, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.lang.annotation.Retention;
  2. import java.lang.annotation.RetentionPolicy;
  3. import java.lang.annotation.Target;
  4. import java.lang.annotation.ElementType;
  5.  
  6. @Retention(RetentionPolicy.RUNTIME)
  7. @Target(ElementType.TYPE)
  8. public @interface CronType {
  9.     public String name();
  10. }
  11.  
  12. ////////////////////////////
  13.  
  14.  
  15. /**
  16.  * CronMaster service, loads all registered jobs on startup and exposes REST service.
  17.  *
  18.  * @author Alexander
  19.  *
  20.  */
  21. @ApplicationPath("/cron")
  22. public class CronMasterApplication extends Application {
  23.     static Map<String,CronAgent> jobs = new HashMap<String, CronAgent>();
  24.    
  25.     public Set<Class<?>> getClasses() {
  26.         return new HashSet<Class<?>>(Arrays.asList(CronMasterEndpoint.class));
  27.     }
  28.    
  29.     static { //cool stuff, calls the method whenever this class is initialized
  30.         registerJobs();
  31.     }
  32.  
  33.     static void registerJobs() {
  34.         final ClassLoader loader = Thread.currentThread().getContextClassLoader();
  35.         try {
  36.             ClassPath classpath = ClassPath.from(loader); // scans the class
  37.                                                             // path used by
  38.                                                             // classloader
  39.             // all job classes have to be in the fi.observis.festivals.cron
  40.             // package or subpackage of that
  41.             for (ClassPath.ClassInfo classInfo : classpath.getTopLevelClassesRecursive("fi.observis.festival.cron")) {
  42.                 logger.info(classInfo.getSimpleName() + " <==> " + classInfo.getPackageName());
  43.                 /*
  44.                  * Do annotation logic here.
  45.                  */
  46.                 Class<?> currentClass = classInfo.load();//gets ur classes
  47.                 Annotation[] annotations = currentClass.getAnnotations(); //need to check if it has CronTask annotation
  48.                 logger.info("Annotations are: " + annotations.length);
  49.                 for (Annotation cur : annotations) {
  50.                     if (cur.annotationType().equals(CronTask.class)) {
  51.                         //Annotation is of type CronType
  52.                         CronTask type = (CronTask) cur;
  53.                        
  54.                         logger.info(cur.annotationType().getName());
  55.                        
  56.                         //Put the job in our hashmap with the key of appid
  57.                         @SuppressWarnings("unchecked")
  58.                         Class<CronAgent> tempClass = (Class<CronAgent>) classInfo.load(); //generic class :D
  59.                         jobs.put(type.name(), tempClass.newInstance()); //instantiate object from the generic class, and store to hash!
  60.                         logger.info(type.name());
  61.                     }
  62.                 }
  63.             }
  64.            
  65.             //calling a job for example
  66.             //jobs.get("aaltofestival").executeJob("aaltofestival");
  67.         } catch (IOException | InstantiationException | IllegalAccessException e) {
  68.             e.printStackTrace();
  69.         }
  70.  
  71.     }
  72. }
  73.  
  74.  
  75. ///////////
  76. package fi.observis.festival.cron.aaltofestival;
  77.  
  78. import *...
  79. //using app id in the annotation for classes that extend the interface
  80. @CronTask(name = "aaltofestival")
  81. public class AaltoCronJob implements CronAgent {
  82.  
  83.     //interface method
  84.     @Override
  85.     public void executeJob(String appId) {
  86.     }
  87.  
  88.  
  89. }
  90.  
  91. //////////
  92.  
  93. public interface CronAgent {
  94.     public void executeJob(String appId);
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement