View difference between Paste ID: 6kw00h45 and sgq1qVC1
SHOW: | | - or go back to the newest paste.
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-
static {
14+
15
/**
16
 * CronMaster service, loads all registered jobs on startup and exposes REST service.
17
 * 
18-
	static void registerJobs(){
18+
 * @author Alexander
19-
	    final ClassLoader loader = Thread.currentThread()
19+
 *
20-
	            .getContextClassLoader();
20+
 */
21-
	    try {
21+
@ApplicationPath("/cron")
22-
	        ClassPath classpath = ClassPath.from(loader); // scans the class path used by classloader
22+
public class CronMasterApplication extends Application {
23-
	        //all job classes have to be in the fi.observis.festivals.cron package or subpackage of that
23+
	static Map<String,CronAgent> jobs = new HashMap<String, CronAgent>();
24-
	        for (ClassPath.ClassInfo classInfo : classpath.getTopLevelClassesRecursive("fi.observis.festivals.cron")) {
24+
25-
	          logger.info(classInfo.getSimpleName()+" <==> "+classInfo.getPackageName());//classes are found ok
25+
    public Set<Class<?>> getClasses() {
26-
	          /*
26+
        return new HashSet<Class<?>>(Arrays.asList(CronMasterEndpoint.class));
27-
	           * Do annotation logic here.
27+
    }
28-
	           */
28+
29-
	          Annotation[] annotations = classInfo.getClass().getAnnotations();
29+
	static { //cool stuff, calls the method whenever this class is initialized
30-
	          logger.info("Annotations are: " + annotations.length);//always prints 0
30+
31-
	          for(Annotation cur : annotations){
31+
32-
	        	  if(cur.annotationType() != null){
32+
33-
	        		  logger.info(cur.annotationType().getName());
33+
	static void registerJobs() {
34-
	        	  }
34+
		final ClassLoader loader = Thread.currentThread().getContextClassLoader();
35-
	          }
35+
		try {
36-
	        }
36+
			ClassPath classpath = ClassPath.from(loader); // scans the class
37-
	    } catch (IOException e) {
37+
															// path used by
38-
	        e.printStackTrace();
38+
															// classloader
39-
	    }
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-
package fi.observis.festivals.cron.subpack;
44+
				 * Do annotation logic here.
45-
import fi.observis.festivals.cron.CronType;
45+
				 */
46
				Class<?> currentClass = classInfo.load();//gets ur classes
47-
@CronType(name = "aaltofestival")
47+
				Annotation[] annotations = currentClass.getAnnotations(); //need to check if it has CronTask annotation
48-
public class AaltoCronJob { /* contents */ }
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
}