1 | /* |
2 | * jDTAUS Core Utilities |
3 | * Copyright (C) 2005 Christian Schulte |
4 | * <cs@schulte.it> |
5 | * |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Lesser General Public |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2.1 of the License, or any later version. |
10 | * |
11 | * This library is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * Lesser General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Lesser General Public |
17 | * License along with this library; if not, write to the Free Software |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
19 | * |
20 | */ |
21 | package org.jdtaus.core.monitor.util; |
22 | |
23 | import java.util.Date; |
24 | import java.util.Locale; |
25 | import org.jdtaus.core.container.ContainerFactory; |
26 | import org.jdtaus.core.logging.spi.Logger; |
27 | import org.jdtaus.core.monitor.TaskEvent; |
28 | import org.jdtaus.core.monitor.TaskListener; |
29 | |
30 | /** |
31 | * {@code TaskListener} logging the duration of an operation performed by |
32 | * a {@code Task}. |
33 | * |
34 | * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> |
35 | * @version $JDTAUS: TaskDurationLogger.java 8641 2012-09-27 06:45:17Z schulte $ |
36 | * |
37 | * @see #onTaskEvent(TaskEvent) |
38 | */ |
39 | public final class TaskDurationLogger implements TaskListener |
40 | { |
41 | //--Dependencies------------------------------------------------------------ |
42 | |
43 | // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausDependencies |
44 | // This section is managed by jdtaus-container-mojo. |
45 | |
46 | /** |
47 | * Gets the configured <code>Logger</code> implementation. |
48 | * |
49 | * @return The configured <code>Logger</code> implementation. |
50 | */ |
51 | private Logger getLogger() |
52 | { |
53 | return (Logger) ContainerFactory.getContainer(). |
54 | getDependency( this, "Logger" ); |
55 | |
56 | } |
57 | |
58 | /** |
59 | * Gets the configured <code>Locale</code> implementation. |
60 | * |
61 | * @return The configured <code>Locale</code> implementation. |
62 | */ |
63 | private Locale getLocale() |
64 | { |
65 | return (Locale) ContainerFactory.getContainer(). |
66 | getDependency( this, "Locale" ); |
67 | |
68 | } |
69 | |
70 | // </editor-fold>//GEN-END:jdtausDependencies |
71 | |
72 | //------------------------------------------------------------Dependencies-- |
73 | //--Properties-------------------------------------------------------------- |
74 | |
75 | // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausProperties |
76 | // This section is managed by jdtaus-container-mojo. |
77 | |
78 | /** |
79 | * Gets the value of property <code>defaultLoggingThresholdMillis</code>. |
80 | * |
81 | * @return Default number of milliseconds a task at least needs to run to trigger a message when finished. |
82 | */ |
83 | private java.lang.Long getDefaultLoggingThresholdMillis() |
84 | { |
85 | return (java.lang.Long) ContainerFactory.getContainer(). |
86 | getProperty( this, "defaultLoggingThresholdMillis" ); |
87 | |
88 | } |
89 | |
90 | // </editor-fold>//GEN-END:jdtausProperties |
91 | |
92 | //--------------------------------------------------------------Properties-- |
93 | //--TaskListener------------------------------------------------------------ |
94 | |
95 | /** |
96 | * {@inheritDoc} |
97 | * <p>This method measures the time a task is running and logs |
98 | * information for tasks running longer than specified by configuration |
99 | * property {@code loggingThresholdMillis} (defaults to 60000).</p> |
100 | * |
101 | * @param event the event send by a {@code Task}. |
102 | */ |
103 | public void onTaskEvent( final TaskEvent event ) |
104 | { |
105 | if ( event != null ) |
106 | { |
107 | final long now = System.currentTimeMillis(); |
108 | final long start = event.getTask().getTimestamp(); |
109 | |
110 | if ( TaskEvent.ENDED == event.getType() && |
111 | now - start > this.getLoggingThresholdMillis() ) |
112 | { |
113 | this.getLogger().info( |
114 | this.getDurationInfoMessage( |
115 | this.getLocale(), |
116 | event.getTask().getDescription(). |
117 | getText( this.getLocale() ), |
118 | new Date( start ), |
119 | new Date( now ), |
120 | new Long( now - start ) ) ); |
121 | |
122 | } |
123 | } |
124 | } |
125 | |
126 | //------------------------------------------------------------TaskListener-- |
127 | //--TaskDurationLogger------------------------------------------------------ |
128 | |
129 | /** |
130 | * The number of milliseconds a task at least needs to run to trigger a |
131 | * message when finished. |
132 | */ |
133 | private Long loggingTresholdMillis; |
134 | |
135 | /** Creates a new {@code TaskDurationLogger} instance. */ |
136 | public TaskDurationLogger() |
137 | { |
138 | super(); |
139 | } |
140 | |
141 | /** |
142 | * Creates a new {@code TaskDurationLogger} instance taking the number of |
143 | * milliseconds a task at least needs to run to trigger a message when |
144 | * finished. |
145 | * |
146 | * @param loggingThresholdMillis the number of milliseconds a task at least |
147 | * needs to run to trigger a message when finished. |
148 | */ |
149 | public TaskDurationLogger( final long loggingThresholdMillis ) |
150 | { |
151 | super(); |
152 | |
153 | if ( loggingThresholdMillis > 0 ) |
154 | { |
155 | this.loggingTresholdMillis = new Long( loggingThresholdMillis ); |
156 | } |
157 | } |
158 | |
159 | /** |
160 | * Gets the number of milliseconds a task at least needs to run to trigger |
161 | * a message when finished. |
162 | * |
163 | * @return the number of milliseconds a task at least needs to run to |
164 | * trigger a message when finished. |
165 | */ |
166 | public long getLoggingThresholdMillis() |
167 | { |
168 | if ( this.loggingTresholdMillis == null ) |
169 | { |
170 | this.loggingTresholdMillis = |
171 | this.getDefaultLoggingThresholdMillis(); |
172 | |
173 | } |
174 | |
175 | return this.loggingTresholdMillis.intValue(); |
176 | } |
177 | |
178 | //------------------------------------------------------TaskDurationLogger-- |
179 | //--Messages---------------------------------------------------------------- |
180 | |
181 | // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausMessages |
182 | // This section is managed by jdtaus-container-mojo. |
183 | |
184 | /** |
185 | * Gets the text of message <code>durationInfo</code>. |
186 | * <blockquote><pre>Taskinformation: |
187 | *Beschreibung ... : {0} |
188 | *Start .......... : {1,date,long} um {1,time,long} |
189 | *Ende ........... : {2,date,long} um {2,time,long} |
190 | *Laufzeit ....... : {3}ms</pre></blockquote> |
191 | * <blockquote><pre>Taskinformation: |
192 | *Description ... : {0} |
193 | *Start ......... : {1,date,long} at {1,time,long} |
194 | *End ........... : {2,date,long} at {2,time,long} |
195 | *Duration ...... : {3}ms</pre></blockquote> |
196 | * |
197 | * @param locale The locale of the message instance to return. |
198 | * @param taskDescription The description of the task. |
199 | * @param startDate The start date of the task. |
200 | * @param endDate The end date of the task. |
201 | * @param durationMillis The number of milliseconds the task ran. |
202 | * |
203 | * @return Information about a task. |
204 | */ |
205 | private String getDurationInfoMessage( final Locale locale, |
206 | final java.lang.String taskDescription, |
207 | final java.util.Date startDate, |
208 | final java.util.Date endDate, |
209 | final java.lang.Number durationMillis ) |
210 | { |
211 | return ContainerFactory.getContainer(). |
212 | getMessage( this, "durationInfo", locale, |
213 | new Object[] |
214 | { |
215 | taskDescription, |
216 | startDate, |
217 | endDate, |
218 | durationMillis |
219 | }); |
220 | |
221 | } |
222 | |
223 | // </editor-fold>//GEN-END:jdtausMessages |
224 | |
225 | //----------------------------------------------------------------Messages-- |
226 | } |