camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > EventNotifier to log details about all sent Exchanges
Date Thu, 13 May 2010 08:28:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="http://cwiki.apache.org/confluence/display/CAMEL/EventNotifier+to+log+details+about+all+sent+Exchanges">EventNotifier
to log details about all sent Exchanges</a></h2>
    <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (6)</h4>
                                 
    
<div id="page-diffs">
            <table class="diff" cellpadding="0" cellspacing="0">
            <tr><td class="diff-unchanged" >h2. EventNotifier to log details about
all sent Exchanges <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >If you want to log information
all time taken to send and receive replies when sending to external endpoints, then you can
leverage the <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">[Event
Notifier]</span> <span class="diff-added-words"style="background-color: #dfd;">{{EventNotifier}}</span>
in Camel. It allows you to subscribe to events and react upon them. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>For example we want to
log the time it takes to send to endpoints which can be done by implementing a class which
this logic as follows: <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">TODO:
e1 - MyLoggingSentEventNotifer.java <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{snippet:id=e1|lang=java|title=MyLoggingSentEventNotifier|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/MyLoggingSentEventNotifer.java}
<br></td></tr>
            <tr><td class="diff-unchanged" > <br>Then in Java DSL you can
tell Camel to use your custom {{EventNotifier}} simply by adding it: <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>And in Spring XML you declare
a Spring bean and Camel will automatic pick it up: <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">TODO:
e1 - EventNotifierExchangeSentExampleTest.xml <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/EventNotifierExchangeSentExampleTest.xml}
<br></td></tr>
            <tr><td class="diff-unchanged" > <br>If you run this example
you will notice how Camel logs this now: <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >Then you have a logger which logs
all _slow_ events. <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">Tip
you can use the {{EndpointHelper.matchEndpoint}} method to leverage the same filtering capabilities
that for example the [Intercept] uses in Camel. <br> <br>You can see which {{EventObject}}
Camel provides from the [javadoc|http://camel.apache.org/maven/camel-2.2.0/camel-core/apidocs/org/apache/camel/management/event/package-summary.html].
<br> <br> <br> <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="EventNotifiertologdetailsaboutallsentExchanges-EventNotifiertologdetailsaboutallsentExchanges"></a>EventNotifier
to log details about all sent Exchanges</h2>

<p>If you want to log information all time taken to send and receive replies when sending
to external endpoints, then you can leverage the <tt>EventNotifier</tt> in Camel.
It allows you to subscribe to events and react upon them.</p>

<p>For example we want to log the time it takes to send to endpoints which can be done
by implementing a class which this logic as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>MyLoggingSentEventNotifier</b></div><div
class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class MyLoggingSentEventNotifer
<span class="code-keyword">extends</span> EventNotifierSupport {

    <span class="code-keyword">public</span> void notify(EventObject event) <span
class="code-keyword">throws</span> Exception {
        <span class="code-keyword">if</span> (event <span class="code-keyword">instanceof</span>
ExchangeSentEvent) {
            ExchangeSentEvent sent = (ExchangeSentEvent) event;
            log.info(<span class="code-quote">"Took "</span> + sent.getTimeTaken()
+ <span class="code-quote">" millis to send to: "</span> + sent.getEndpoint());
        }
    }

    <span class="code-keyword">public</span> <span class="code-object">boolean</span>
isEnabled(EventObject event) {
        <span class="code-comment">// we only want the sent events
</span>        <span class="code-keyword">return</span> event <span class="code-keyword">instanceof</span>
ExchangeSentEvent;
    }

    <span class="code-keyword">protected</span> void doStart() <span class="code-keyword">throws</span>
Exception {
        <span class="code-comment">// noop
</span>    }

    <span class="code-keyword">protected</span> void doStop() <span class="code-keyword">throws</span>
Exception {
        <span class="code-comment">// noop
</span>    }

}
</pre>
</div></div>

<p>Then in Java DSL you can tell Camel to use your custom <tt>EventNotifier</tt>
simply by adding it:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
context.getManagementStrategy().addEventNotifier(<span class="code-keyword">new</span>
MyLoggingSentEventNotifer());
</pre>
</div></div> 

<p>And in Spring XML you declare a Spring bean and Camel will automatic pick it up:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;bean id=<span class="code-quote">"myLoggingEventNotifier"</span>
class=<span class="code-quote">"org.apache.camel.processor.MyLoggingSentEventNotifer"</span>/&gt;</span>

<span class="code-tag">&lt;camelContext id=<span class="code-quote">"camel"</span>
xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>

    <span class="code-tag">&lt;route&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"direct:bar"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>

    <span class="code-tag">&lt;route&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:bar"</span>/&gt;</span>
        <span class="code-tag">&lt;delay&gt;</span><span class="code-tag">&lt;constant&gt;</span>1000<span
class="code-tag">&lt;/constant&gt;</span><span class="code-tag">&lt;/delay&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>

<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<p>If you run this example you will notice how Camel logs this now:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
INFO  CamelContextFactoryBean        - Using custom EventNotifier with id: myLoggingEventNotifier
and implementation: org.apache.camel.processor.MyLoggingSentEventNotifer@76bf9e
INFO  MyLoggingSentEventNotifer      - Took 1001 millis to send to: Endpoint[direct:<span
class="code-comment">//bar]
</span>INFO  MyLoggingSentEventNotifer      - Took 0 millis to send to: Endpoint[mock:<span
class="code-comment">//result]
</span>INFO  MyLoggingSentEventNotifer      - Took 1013 millis to send to: Endpoint[direct:<span
class="code-comment">//start]</span>
</pre>
</div></div>

<p>You can implement logic to filter which endpoints to log, and maybe also thresholds
to skip logging if it was fast, e.g. &lt; 1 sec.<br/>
Then you have a logger which logs all <em>slow</em> events.</p>

<p>Tip you can use the <tt>EndpointHelper.matchEndpoint</tt> method to leverage
the same filtering capabilities that for example the <a href="/confluence/display/CAMEL/Intercept"
title="Intercept">Intercept</a> uses in Camel.</p>

<p>You can see which <tt>EventObject</tt> Camel provides from the <a
href="http://camel.apache.org/maven/camel-2.2.0/camel-core/apidocs/org/apache/camel/management/event/package-summary.html"
class="external-link" rel="nofollow">javadoc</a>.</p>




    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="http://cwiki.apache.org/confluence/display/CAMEL/EventNotifier+to+log+details+about+all+sent+Exchanges">View
Online</a>
        |
        <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=21266487&revisedVersion=3&originalVersion=2">View
Changes</a>
                |
        <a href="http://cwiki.apache.org/confluence/display/CAMEL/EventNotifier+to+log+details+about+all+sent+Exchanges?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message