Rev 1332 | Rev 1353 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
995 | jmachado | 1 | package pt.estgp.estgweb.services.jobs; |
2 | |||
1076 | jmachado | 3 | import jomm.dao.impl.AbstractDao; |
995 | jmachado | 4 | import org.apache.log4j.FileAppender; |
1332 | jmachado | 5 | import org.apache.log4j.Level; |
995 | jmachado | 6 | import org.apache.log4j.Logger; |
7 | import org.apache.log4j.PatternLayout; |
||
8 | import pt.estgp.estgweb.Globals; |
||
9 | import pt.estgp.estgweb.domain.JobServiceTask; |
||
10 | import pt.estgp.estgweb.domain.JobServiceTaskImpl; |
||
11 | import pt.estgp.estgweb.domain.JobServiceTaskParameter; |
||
1070 | jmachado | 12 | import pt.estgp.estgweb.services.logresults.ILogMessages; |
995 | jmachado | 13 | |
14 | import java.io.IOException; |
||
15 | import java.util.*; |
||
16 | |||
17 | /** |
||
18 | * Created by jorgemachado on 02/11/15. |
||
19 | * |
||
20 | * Todos os serviços podem correr em BackGround através da chamada ao JobRunner |
||
21 | * desde que implementem esta interface |
||
22 | */ |
||
23 | public abstract class ServiceJob implements JobHandler |
||
24 | { |
||
25 | |||
1133 | jmachado | 26 | public static final 1.5.0/docs/api/java/lang/String.html">String JOB_importYear_KEY = "JOB_importYear_KEY"; |
27 | public static final 1.5.0/docs/api/java/lang/String.html">String JOB_semestre_KEY = "JOB_semestre_KEY"; |
||
28 | public static final 1.5.0/docs/api/java/lang/String.html">String JOB_sendIonline_KEY = "JOB_sendIonline_KEY"; |
||
29 | public static final 1.5.0/docs/api/java/lang/String.html">String JOB_sendEmail_KEY = "JOB_sendEmail_KEY"; |
||
30 | public static final 1.5.0/docs/api/java/lang/String.html">String JOB_cloneOnlyNews_KEY = "JOB_cloneOnlyNews_KEY"; |
||
31 | public static final 1.5.0/docs/api/java/lang/String.html">String JOB_generateAllPdfs_KEY = "JOB_generateAllPdfs_KEY"; |
||
32 | public static final 1.5.0/docs/api/java/lang/String.html">String JOB_validate_KEY = "JOB_validade_KEY"; |
||
1312 | jmachado | 33 | public static final 1.5.0/docs/api/java/lang/String.html">String JOB_institution_KEY = "JOB_institution_KEY"; |
1327 | jmachado | 34 | |
35 | public static final 1.5.0/docs/api/java/lang/String.html">String JOB_questionario_courseList_KEY = "JOB_questionario_courseList_KEY"; |
||
36 | public static final 1.5.0/docs/api/java/lang/String.html">String JOB_questionario_id_KEY = "JOB_questionario_id_KEY"; |
||
995 | jmachado | 37 | /*Log for service run*/ |
1133 | jmachado | 38 | |
1332 | jmachado | 39 | 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("JobServiceTaskLog"); |
995 | jmachado | 40 | |
41 | /*Class regular log*/ |
||
1332 | jmachado | 42 | 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(ServiceJob.class); |
995 | jmachado | 43 | |
44 | private Map<String,JobServiceTaskParameter> parameters = new HashMap<String, JobServiceTaskParameter>(); |
||
45 | protected pt.estgp.estgweb.domain.JobServiceTaskImpl jobServiceTask = null; //to be used in subService in order to update log and check stopRequest |
||
46 | |||
47 | public ServiceJob() |
||
48 | { |
||
49 | |||
50 | } |
||
51 | |||
52 | public Collection<JobServiceTaskParameter> getParameters() { return parameters.values(); } |
||
53 | public Map<String,JobServiceTaskParameter> getParametersMap() { return parameters;} |
||
54 | public JobServiceTask getJobServiceTask() { return jobServiceTask; } |
||
55 | |||
56 | public void setParameters(Set<JobServiceTaskParameter> parameters) |
||
57 | { |
||
58 | this.parameters.clear(); |
||
59 | for(JobServiceTaskParameter param: parameters) |
||
60 | { |
||
61 | this.parameters.put(param.getName(),param); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Inicialisation |
||
67 | * @param jobServiceTask |
||
68 | */ |
||
69 | public void setJobServiceTask(JobServiceTaskImpl jobServiceTask) { |
||
70 | this.jobServiceTask = jobServiceTask; |
||
71 | jobServiceTask.setProgress(0); |
||
72 | //Service Log initialization |
||
73 | logger.info("Defining service log " + jobServiceTask.getLogFilePath()); |
||
74 | serviceLog.removeAllAppenders(); |
||
75 | serviceLog.setLevel(org.apache.log4j.1.5.0/docs/api/java/util/logging/Level.html">Level.INFO); |
||
76 | 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(); |
||
77 | 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(); |
||
78 | if(!dirs.exists()) |
||
79 | dirs.mkdirs(); |
||
80 | |||
81 | try { |
||
82 | serviceLog.addAppender(new FileAppender(new PatternLayout("%d (%F:%L) %-5r %-5p [%t] %c{2} - %m%n"), logPath)); |
||
83 | } catch (1.5.0/docs/api/java/io/IOException.html">IOException ex) { |
||
84 | logger.info("Error defining service log", ex); |
||
85 | } |
||
86 | serviceLog.setAdditivity(false); |
||
87 | serviceLog.info("STARTING INFO serviceLog FOR SERVICE " + jobServiceTask.getLogFilePath() + " LOG at " + jobServiceTask.getLogFilePath()); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Run the job |
||
92 | * This method is called from JobDaemon is synchronous |
||
93 | */ |
||
94 | public void runJob() |
||
95 | { |
||
1333 | jmachado | 96 | 1.5.0/docs/api/java/lang/String.html">String msg; |
995 | jmachado | 97 | if(jobServiceTask == null) |
98 | throw new 1.5.0/docs/api/java/lang/RuntimeException.html">RuntimeException("This Service as not been initialized to run as JobService"); |
||
99 | |||
1333 | jmachado | 100 | msg = "STARTING SERVICE"; |
101 | serviceLog.info(msg); |
||
102 | logger.info(msg); |
||
103 | msg = "CLASS: " + jobServiceTask.getTargetService(); |
||
104 | serviceLog.info(msg); |
||
105 | logger.info(msg); |
||
995 | jmachado | 106 | if(jobServiceTask.getCreatedBy() != null) |
1333 | jmachado | 107 | { |
108 | msg = "CREATED BY: " + jobServiceTask.getCreatedBy().getUsername() + " - " + jobServiceTask.getCreatedBy().getName(); |
||
109 | serviceLog.info(msg); |
||
110 | logger.info(msg); |
||
111 | } |
||
995 | jmachado | 112 | else |
1333 | jmachado | 113 | { |
114 | msg ="CREATED BY: unknown"; |
||
115 | serviceLog.info(msg); |
||
116 | logger.info(msg); |
||
117 | } |
||
995 | jmachado | 118 | for(JobServiceTaskParameter parameter: parameters.values()) |
119 | { |
||
1333 | jmachado | 120 | msg = "PARAMETER: " + parameter.getName() + " : " + parameter.getObject().toString(); |
121 | serviceLog.info(msg); |
||
122 | logger.info(msg); |
||
995 | jmachado | 123 | } |
124 | logger.info("Starting job service " + getClass().getName() + " more info in serviceLog " + jobServiceTask.getLogFilePath()); |
||
125 | jobServiceTask.setProgress(0); |
||
126 | 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())); |
||
127 | jobServiceTask.setStatusEnum(JobServiceTaskImpl.JobStatus.STARTED); |
||
128 | jobServiceTask.setServiceStartDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
||
129 | jobServiceTask.setServiceLastUpdateDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
||
1076 | jmachado | 130 | commitPartially(); |
995 | jmachado | 131 | try { |
132 | //call the implementation of service this will update serviceLog and progress |
||
1070 | jmachado | 133 | ILogMessages messages = runJobServiceTask(); |
134 | if(messages.hasErrors()) |
||
135 | jobServiceTask.setStatusEnum(JobServiceTaskImpl.JobStatus.FINISHED_ERRORS); |
||
136 | else if(messages.hasWarnings()) |
||
137 | jobServiceTask.setStatusEnum(JobServiceTaskImpl.JobStatus.FINISHED_WARNINGS); |
||
138 | else |
||
139 | jobServiceTask.setStatusEnum(JobServiceTaskImpl.JobStatus.FINISHED); |
||
995 | jmachado | 140 | jobServiceTask.setServiceLastUpdateDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
141 | jobServiceTask.setServiceFinishDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
||
142 | serviceLog.info("FINISHING SERVICE"); |
||
143 | logger.info("Finishing job service " + getClass().getName() + " more info in serviceLog " + jobServiceTask.getLogFilePath()); |
||
144 | } catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable throwable) { |
||
145 | jobServiceTask.setStatusEnum(JobServiceTaskImpl.JobStatus.FAILED); |
||
146 | jobServiceTask.setServiceLastUpdateDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
||
147 | jobServiceTask.setServiceFinishDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
||
148 | serviceLog.info("ERROR IN SERVICE", throwable); |
||
149 | logger.info("Error in job service " + getClass().getName() + " more info in serviceLog " + jobServiceTask.getLogFilePath()); |
||
150 | } |
||
151 | } |
||
152 | |||
1312 | jmachado | 153 | public void setProgress(int progress){ |
154 | 1.5.0/docs/api/java/lang/System.html">System.out.println(progress+"% PROGRESS##############"); |
||
155 | if(jobServiceTask != null) jobServiceTask.setProgress(progress); |
||
156 | } |
||
995 | jmachado | 157 | public void serviceLogInfo(1.5.0/docs/api/java/lang/String.html">String str){ if(jobServiceTask != null) serviceLog.info(str);} |
158 | public void serviceLogWarn(1.5.0/docs/api/java/lang/String.html">String str){ if(jobServiceTask != null) serviceLog.warn(str);} |
||
159 | public void serviceLogError(1.5.0/docs/api/java/lang/String.html">String str){ if(jobServiceTask != null) serviceLog.error(str);} |
||
160 | public void serviceLogFatal(1.5.0/docs/api/java/lang/String.html">String str){ if(jobServiceTask != null) serviceLog.fatal(str);} |
||
161 | 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);} |
||
162 | 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);} |
||
163 | 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);} |
||
164 | 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);} |
||
165 | |||
166 | //Implemented in SubService to run the Job |
||
1070 | jmachado | 167 | protected abstract ILogMessages runJobServiceTask() throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable; |
1076 | jmachado | 168 | |
1312 | jmachado | 169 | /** |
170 | * Should be override on services wich can run only once at a time |
||
171 | * Should return false is schedulle over time is permited |
||
172 | * If not only RunNowService will schedulle |
||
173 | * @return |
||
174 | */ |
||
175 | public boolean runOnlyOnDemand() |
||
176 | { |
||
177 | return false; |
||
178 | } |
||
179 | |||
1076 | jmachado | 180 | protected void commitPartially() { |
1119 | jmachado | 181 | |
1076 | jmachado | 182 | AbstractDao.getCurrentSession().getTransaction().commit(); |
183 | AbstractDao.getCurrentSession().beginTransaction(); |
||
1312 | jmachado | 184 | printMem(false); |
185 | AbstractDao.getCurrentSession().clear(); |
||
186 | printMem(false); |
||
187 | 1.5.0/docs/api/java/lang/System.html">System.gc(); |
||
188 | printMem(true); |
||
1119 | jmachado | 189 | if(jobServiceTask != null) |
190 | AbstractDao.getCurrentSession().update(jobServiceTask); |
||
1076 | jmachado | 191 | } |
1312 | jmachado | 192 | |
193 | private void printMem(boolean printService) |
||
194 | { |
||
195 | int mb = 1024*1024; |
||
196 | |||
197 | //Getting the runtime reference from system |
||
198 | 1.5.0/docs/api/java/lang/Runtime.html">Runtime runtime = 1.5.0/docs/api/java/lang/Runtime.html">Runtime.getRuntime(); |
||
199 | |||
200 | |||
201 | //serviceLogInfo("##### Heap utilization statistics [MB] #####"); |
||
202 | logger.info("##### Heap utilization statistics [MB] #####"); |
||
203 | |||
204 | //Print used memory |
||
205 | logger.info("Used Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / mb); |
||
206 | if(printService) |
||
207 | serviceLogInfo("#####Heap Used Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / mb + "[MB]"); |
||
208 | |||
209 | |||
210 | //Print free memory |
||
211 | //serviceLogInfo("Free Memory:" + runtime.freeMemory() / mb); |
||
212 | logger.info("Free Memory:" + runtime.freeMemory() / mb); |
||
213 | |||
214 | //Print total available memory |
||
215 | //serviceLogInfo("Total Memory:" + runtime.totalMemory() / mb); |
||
216 | logger.info("Total Memory:" + runtime.totalMemory() / mb); |
||
217 | |||
218 | //Print Maximum available memory |
||
219 | //serviceLogInfo("Max Memory:" + runtime.maxMemory() / mb); |
||
220 | logger.info("Max Memory:" + runtime.maxMemory() / mb); |
||
221 | } |
||
1332 | jmachado | 222 | |
223 | public void serviceLogInfoWithLoggerSource(1.5.0/docs/api/java/lang/String.html">String message) |
||
224 | { |
||
225 | serviceLogInfo(message); |
||
226 | logger.log(this.getClass().getCanonicalName(), 1.5.0/docs/api/java/util/logging/Level.html">Level.INFO, message, null); |
||
227 | } |
||
995 | jmachado | 228 | } |