3030import de .taimos .dvalin .interconnect .core .daemon .model .InterconnectResponseContext ;
3131import de .taimos .dvalin .interconnect .core .daemon .util .DaemonMethodRegistry ;
3232import de .taimos .dvalin .interconnect .core .daemon .util .DaemonMethodRegistry .RegistryEntry ;
33+ import de .taimos .dvalin .interconnect .core .exceptions .MessageCryptoException ;
3334import de .taimos .dvalin .interconnect .model .InterconnectObject ;
34- import de .taimos .dvalin .interconnect .model .ivo .IPageable ;
3535import de .taimos .dvalin .interconnect .model .ivo .IVO ;
3636import de .taimos .dvalin .interconnect .model .ivo .daemon .DaemonErrorIVO ;
3737import de .taimos .dvalin .interconnect .model .ivo .daemon .PingIVO ;
4141import de .taimos .dvalin .interconnect .model .service .DaemonScanner .DaemonMethod ;
4242import de .taimos .dvalin .interconnect .model .service .IDaemonHandler ;
4343import de .taimos .dvalin .jms .crypto .ICryptoService ;
44- import de . taimos . dvalin . interconnect . core . exceptions . MessageCryptoException ;
44+ import org . joda . time . DateTime ;
4545import org .slf4j .Logger ;
4646
4747import javax .jms .JMSException ;
4848import javax .jms .Message ;
4949import javax .jms .TextMessage ;
5050import java .lang .reflect .InvocationTargetException ;
51+ import java .util .ArrayList ;
5152import java .util .Collection ;
53+ import java .util .List ;
5254import java .util .UUID ;
55+ import java .util .function .Function ;
5356
5457
5558/**
@@ -66,12 +69,16 @@ public abstract class ADaemonMessageHandler implements IDaemonMessageHandler {
6669
6770 private final boolean throwExceptionOnTimeout ;
6871
72+ protected final List <Function <InterconnectObject , String >> additionalLogAppender = new ArrayList <>();
73+
6974
70- protected ADaemonMessageHandler (final Collection <Class <? extends IDaemonHandler >> aHandlerClazzes , IDaemonMessageSender messageSender , ICryptoService cryptoService , final boolean aThrowExceptionOnTimeout ) {
75+ protected ADaemonMessageHandler (final Collection <Class <? extends IDaemonHandler >> aHandlerClazzes , IDaemonMessageSender messageSender , ICryptoService cryptoService ,
76+ final boolean aThrowExceptionOnTimeout ) {
7177 this .registry = new DaemonMethodRegistry (aHandlerClazzes );
7278 this .throwExceptionOnTimeout = aThrowExceptionOnTimeout ;
7379 this .cryptoService = cryptoService ;
7480 this .messageSender = messageSender ;
81+ this .additionalLogAppender .add (DefaultMessageLogAppender ::pageAppender );
7582 }
7683
7784
@@ -119,6 +126,7 @@ public final void onMessage(final Message message) throws Exception {
119126
120127 if (context .getReceivedContext ().getIcoClass ().equals (PingIVO .class )) {
121128 this .handlePing (context );
129+ this .logInvoke (context , message );
122130 return ;
123131 }
124132
@@ -131,7 +139,7 @@ public final void onMessage(final Message message) throws Exception {
131139 final DaemonMethod method = ADaemonMessageHandler .getDaemonMethod (registryEntry , context );
132140 context .setCreateResponseMethod (method );
133141
134- this .logInvoke (context );
142+ this .logInvoke (context , message );
135143
136144 if (method .getType () == DaemonScanner .Type .voit ) {
137145 this .handleWithoutReply (handler , context );
@@ -157,9 +165,11 @@ private void handleWithReply(IDaemonHandler handler, InterconnectResponseContext
157165 this .reply (context );
158166 } catch (final DaemonError e ) {
159167 if (e .getNumber ().get () < 0 ) {
160- this .getLogger ().error ("DaemonError for " + context .getCreateResponseMethod ().getMethod ().getName () + "(" + context .getReceivedContext ().getIcoClass ().getSimpleName () + ")" + " with " + de .taimos .dvalin .interconnect .model .InterconnectContext .getContext (), e );
168+ this .getLogger ().error ("DaemonError for {}({}) with {}" , context .getCreateResponseMethod ().getMethod ().getName (), context .getReceivedContext ().getIcoClass ()
169+ .getSimpleName (), de .taimos .dvalin .interconnect .model .InterconnectContext .getContext (), e );
161170 } else {
162- this .getLogger ().debug ("DaemonError for " + context .getCreateResponseMethod ().getMethod ().getName () + "(" + context .getReceivedContext ().getIcoClass ().getSimpleName () + ")" + " with " + de .taimos .dvalin .interconnect .model .InterconnectContext .getContext (), e );
171+ this .getLogger ().debug ("DaemonError for {}({}) with {}" , context .getCreateResponseMethod ().getMethod ().getName (), context .getReceivedContext ().getIcoClass ()
172+ .getSimpleName (), de .taimos .dvalin .interconnect .model .InterconnectContext .getContext (), e );
163173 }
164174 this .sendErrorResponse (e , context );
165175 }
@@ -189,15 +199,17 @@ private void updateThreadContext(InterconnectResponseContext context) throws Exc
189199 private static DaemonMethod getDaemonMethod (RegistryEntry registryEntry , InterconnectResponseContext context ) throws Exception {
190200 final DaemonMethod method = registryEntry .getMethod ();
191201 if (method .isSecure () != context .getReceivedContext ().isSecure ()) {
192- throw new Exception ("Insecure call (is " + context .getReceivedContext ().isSecure () + " should be " + method .isSecure () + ") for " + context .getReceivedContext ().getIcoClass ().getSimpleName () + " from " + context .getReceivedMessage ().getJMSReplyTo ());
202+ throw new Exception ("Insecure call (is " + context .getReceivedContext ().isSecure () + " should be " + method .isSecure () + ") for " + context .getReceivedContext ()
203+ .getIcoClass ().getSimpleName () + " from " + context .getReceivedMessage ().getJMSReplyTo ());
193204 }
194205 return method ;
195206 }
196207
197208 private RegistryEntry getRegistryEntry (InterconnectResponseContext context ) throws Exception {
198209 final RegistryEntry registryEntry = this .registry .get (context .getReceivedContext ().getIcoClass ());
199210 if (registryEntry == null ) {
200- throw new Exception ("No registered method found for " + context .getReceivedContext ().getIcoClass ().getSimpleName () + " from " + context .getReceivedMessage ().getJMSReplyTo ());
211+ throw new Exception ("No registered method found for " + context .getReceivedContext ().getIcoClass ().getSimpleName () + " from " + context .getReceivedMessage ()
212+ .getJMSReplyTo ());
201213 }
202214 return registryEntry ;
203215 }
@@ -225,18 +237,54 @@ private HandlingDurationType duration(InterconnectResponseContext context) throw
225237 return handlingDuration .getHandlingDurationType ();
226238 }
227239
228- private void logInvoke (InterconnectResponseContext context ) {
240+ private void logInvoke (InterconnectResponseContext context , Message message ) {
241+ if (!this .getLogger ().isInfoEnabled () && !this .getLogger ().isWarnEnabled ()) {
242+ return ;
243+ }
244+ long inflightDuration = this .getInflightDuration (message );
245+ if (inflightDuration > 5000 && this .getLogger ().isWarnEnabled ()) {
246+ final String sbInvokeLog = this .createLogMessageForInvoke (context , inflightDuration );
247+ this .getLogger ().warn (sbInvokeLog );
248+ return ;
249+ }
229250 if (this .getLogger ().isInfoEnabled ()) {
230- final StringBuilder sbInvokeLog = new StringBuilder () //
231- .append ("Invoke " ) //
232- .append (context .getCreateResponseMethod ().getMethod ().getName ()) //
233- .append ("(" ).append (context .getReceivedContext ().getIcoClass ().getSimpleName ()).append (")" );
234- if (context .getReceivedContext ().getRequestIco () instanceof IPageable ) {
235- sbInvokeLog //
236- .append (" at Page " ).append (((IPageable ) context .getReceivedContext ().getRequestIco ()).getOffset ()).append (";" ).append (((IPageable ) context .getReceivedContext ().getRequestIco ()).getLimit ());
251+ final String sbInvokeLog = this .createLogMessageForInvoke (context , inflightDuration );
252+ this .getLogger ().info (sbInvokeLog );
253+ }
254+ }
255+
256+ private String createLogMessageForInvoke (InterconnectResponseContext context , long inflightDuration ) {
257+ final StringBuilder sbInvokeLog = new StringBuilder () //
258+ .append ("Invoked: " ) //
259+ .append (context .getReceivedContext ().getIcoClass ().getSimpleName ());
260+ if (inflightDuration > 0 ) {
261+ sbInvokeLog .append (" | inflight: " ).append (inflightDuration ).append (" ms" );
262+ }
263+ for (Function <InterconnectObject , String > appender : this .additionalLogAppender ) {
264+ String logAppend = appender .apply (context .getReceivedContext ().getRequestIco ());
265+ if (logAppend != null && !logAppend .trim ().isEmpty ()) {
266+ sbInvokeLog .append (" | " ).append (logAppend .trim ());
237267 }
238- sbInvokeLog .append (" with " ).append (de .taimos .dvalin .interconnect .model .InterconnectContext .getContext ());
239- this .getLogger ().info (sbInvokeLog .toString ());
268+ }
269+ return sbInvokeLog .toString ();
270+ }
271+
272+ private long getInflightDuration (Message message ) {
273+ if (message == null ) {
274+ return 0 ;
275+ }
276+ try {
277+ long expireTime = message .getJMSExpiration ();
278+ if (expireTime <= 0 ) {
279+ return 0 ;
280+ }
281+ long sendTime = message .getJMSTimestamp ();
282+ if (sendTime <= 0 ) {
283+ return 0 ;
284+ }
285+ return DateTime .now ().getMillis () - sendTime ;
286+ } catch (JMSException e ) {
287+ return 0 ;
240288 }
241289 }
242290
@@ -302,7 +350,8 @@ private InterconnectObject handleRequest(final IDaemonHandler handler, final Dae
302350 throw new IdemponentRetryException (targetException );
303351 }
304352
305- this .getLogger ().error ("Exception in non-idempotent " + method .getMethod ().getName () + "(" + ico .getClass ().getSimpleName () + ")" + " with " + de .taimos .dvalin .interconnect .model .InterconnectContext .getContext (), e );
353+ this .getLogger ().error ("Exception in non-idempotent {}({}) with {}" , method .getMethod ().getName (), ico .getClass ()
354+ .getSimpleName (), de .taimos .dvalin .interconnect .model .InterconnectContext .getContext (), e );
306355 throw new DaemonError (FrameworkErrors .FRAMEWORK_ERROR , targetException );
307356 } catch (final Exception e ) {
308357 throw new RuntimeException (e );
@@ -331,4 +380,5 @@ private void handleWithoutReply(final IDaemonHandler handler, InterconnectRespon
331380 }
332381 }
333382
383+
334384}
0 commit comments