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