Rev 996 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
995 | jmachado | 1 | package pt.estgp.estgweb.services.jobs; |
2 | |||
3 | import org.apache.log4j.FileAppender; |
||
4 | import org.apache.log4j.Logger; |
||
5 | import org.apache.log4j.PatternLayout; |
||
6 | import pt.estgp.estgweb.Globals; |
||
7 | import pt.estgp.estgweb.domain.JobServiceTask; |
||
8 | import pt.estgp.estgweb.domain.JobServiceTaskImpl; |
||
9 | import pt.estgp.estgweb.domain.JobServiceTaskParameter; |
||
10 | |||
11 | import java.io.IOException; |
||
12 | import java.util.*; |
||
13 | |||
14 | /** |
||
15 | * Created by jorgemachado on 02/11/15. |
||
16 | * |
||
17 | * Todos os serviços podem correr em BackGround através da chamada ao JobRunner |
||
18 | * desde que implementem esta interface |
||
19 | */ |
||
20 | public abstract class ServiceJob implements JobHandler |
||
21 | { |
||
22 | |||
23 | /*Log for service run*/ |
||
24 | protected final 1.5.0/docs/api/java/util/logging/Logger.html">Logger serviceLog = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(ServiceJob.class); |
||
25 | |||
26 | /*Class regular log*/ |
||
27 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger("JobServiceTaskLog"); |
||
28 | |||
29 | private Map<String,JobServiceTaskParameter> parameters = new HashMap<String, JobServiceTaskParameter>(); |
||
30 | protected pt.estgp.estgweb.domain.JobServiceTaskImpl jobServiceTask = null; //to be used in subService in order to update log and check stopRequest |
||
31 | |||
32 | public ServiceJob() |
||
33 | { |
||
34 | |||
35 | } |
||
36 | |||
37 | public Collection<JobServiceTaskParameter> getParameters() { return parameters.values(); } |
||
38 | public Map<String,JobServiceTaskParameter> getParametersMap() { return parameters;} |
||
39 | public JobServiceTask getJobServiceTask() { return jobServiceTask; } |
||
40 | |||
41 | public void setParameters(Set<JobServiceTaskParameter> parameters) |
||
42 | { |
||
43 | this.parameters.clear(); |
||
44 | for(JobServiceTaskParameter param: parameters) |
||
45 | { |
||
46 | this.parameters.put(param.getName(),param); |
||
47 | } |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Inicialisation |
||
52 | * @param jobServiceTask |
||
53 | */ |
||
54 | public void setJobServiceTask(JobServiceTaskImpl jobServiceTask) { |
||
55 | this.jobServiceTask = jobServiceTask; |
||
56 | jobServiceTask.setProgress(0); |
||
57 | //Service Log initialization |
||
58 | logger.info("Defining service log " + jobServiceTask.getLogFilePath()); |
||
59 | serviceLog.removeAllAppenders(); |
||
60 | serviceLog.setLevel(org.apache.log4j.1.5.0/docs/api/java/util/logging/Level.html">Level.INFO); |
||
61 | 1.5.0/docs/api/java/lang/String.html">String logPath = Globals.JOB_SERVICES_LOG_DIR + java.io.1.5.0/docs/api/java/io/File.html">File.separator + jobServiceTask.getLogFilePath(); |
||
62 | java.io.1.5.0/docs/api/java/io/File.html">File dirs = new java.io.1.5.0/docs/api/java/io/File.html">File(logPath).getParentFile(); |
||
63 | if(!dirs.exists()) |
||
64 | dirs.mkdirs(); |
||
65 | |||
66 | try { |
||
67 | serviceLog.addAppender(new FileAppender(new PatternLayout("%d (%F:%L) %-5r %-5p [%t] %c{2} - %m%n"), logPath)); |
||
68 | } catch (1.5.0/docs/api/java/io/IOException.html">IOException ex) { |
||
69 | logger.info("Error defining service log", ex); |
||
70 | } |
||
71 | serviceLog.setAdditivity(false); |
||
72 | serviceLog.info("STARTING INFO serviceLog FOR SERVICE " + jobServiceTask.getLogFilePath() + " LOG at " + jobServiceTask.getLogFilePath()); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Run the job |
||
77 | * This method is called from JobDaemon is synchronous |
||
78 | */ |
||
79 | public void runJob() |
||
80 | { |
||
81 | if(jobServiceTask == null) |
||
82 | throw new 1.5.0/docs/api/java/lang/RuntimeException.html">RuntimeException("This Service as not been initialized to run as JobService"); |
||
83 | |||
84 | serviceLog.info("STARTING SERVICE"); |
||
85 | serviceLog.info("CLASS: " + jobServiceTask.getTargetService()); |
||
86 | if(jobServiceTask.getCreatedBy() != null) |
||
87 | serviceLog.info("CREATED BY: " + jobServiceTask.getCreatedBy().getUsername() + " - " + jobServiceTask.getCreatedBy().getName()); |
||
88 | else |
||
89 | serviceLog.info("CREATED BY: unknown"); |
||
90 | for(JobServiceTaskParameter parameter: parameters.values()) |
||
91 | { |
||
92 | serviceLog.info("PARAMETER: " + parameter.getName() + " : " + parameter.getObject().toString()); |
||
93 | } |
||
94 | logger.info("Starting job service " + getClass().getName() + " more info in serviceLog " + jobServiceTask.getLogFilePath()); |
||
95 | jobServiceTask.setProgress(0); |
||
96 | jobServiceTask.setThreadId(new 1.5.0/docs/api/java/lang/Integer.html">Integer("" + 1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getId())); |
||
97 | jobServiceTask.setStatusEnum(JobServiceTaskImpl.JobStatus.STARTED); |
||
98 | jobServiceTask.setServiceStartDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
||
99 | jobServiceTask.setServiceLastUpdateDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
||
100 | try { |
||
101 | //call the implementation of service this will update serviceLog and progress |
||
102 | runJobServiceTask(); |
||
103 | jobServiceTask.setStatusEnum(JobServiceTaskImpl.JobStatus.FINISHED); |
||
104 | jobServiceTask.setServiceLastUpdateDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
||
105 | jobServiceTask.setServiceFinishDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
||
106 | serviceLog.info("FINISHING SERVICE"); |
||
107 | logger.info("Finishing job service " + getClass().getName() + " more info in serviceLog " + jobServiceTask.getLogFilePath()); |
||
108 | } catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable throwable) { |
||
109 | jobServiceTask.setStatusEnum(JobServiceTaskImpl.JobStatus.FAILED); |
||
110 | jobServiceTask.setServiceLastUpdateDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
||
111 | jobServiceTask.setServiceFinishDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
||
112 | serviceLog.info("ERROR IN SERVICE", throwable); |
||
113 | logger.info("Error in job service " + getClass().getName() + " more info in serviceLog " + jobServiceTask.getLogFilePath()); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | public void setProgress(int progress){ if(jobServiceTask != null) jobServiceTask.setProgress(progress);} |
||
118 | public void serviceLogInfo(1.5.0/docs/api/java/lang/String.html">String str){ if(jobServiceTask != null) serviceLog.info(str);} |
||
119 | public void serviceLogWarn(1.5.0/docs/api/java/lang/String.html">String str){ if(jobServiceTask != null) serviceLog.warn(str);} |
||
120 | public void serviceLogError(1.5.0/docs/api/java/lang/String.html">String str){ if(jobServiceTask != null) serviceLog.error(str);} |
||
121 | public void serviceLogFatal(1.5.0/docs/api/java/lang/String.html">String str){ if(jobServiceTask != null) serviceLog.fatal(str);} |
||
122 | public void serviceLogInfo(1.5.0/docs/api/java/lang/String.html">String str, 1.5.0/docs/api/java/lang/Throwable.html">Throwable e){ if(jobServiceTask != null) serviceLog.info(str,e);} |
||
123 | public void serviceLogWarn(1.5.0/docs/api/java/lang/String.html">String str, 1.5.0/docs/api/java/lang/Throwable.html">Throwable e){ if(jobServiceTask != null) serviceLog.warn(str,e);} |
||
124 | public void serviceLogError(1.5.0/docs/api/java/lang/String.html">String str, 1.5.0/docs/api/java/lang/Throwable.html">Throwable e){ if(jobServiceTask != null) serviceLog.error(str,e);} |
||
125 | public void serviceLogFatal(1.5.0/docs/api/java/lang/String.html">String str, 1.5.0/docs/api/java/lang/Throwable.html">Throwable e){ if(jobServiceTask != null) serviceLog.fatal(str,e);} |
||
126 | |||
127 | //Implemented in SubService to run the Job |
||
128 | protected abstract void runJobServiceTask() throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable; |
||
129 | public abstract 1.5.0/docs/api/java/lang/String.html">String getLogPathPrefix(); |
||
130 | } |