Andrej Koelewijn

  • Home
  • About
  • Publications
  • Departments
    • agile
    • architecture
    • cloud
    • java
    • mobile
    • open standards
    • oracle
    • oss
    • other
    • soa
    • software development
    • tablet
    • Uncategorized
    • web
  • Subscribe via RSS

A simple file monitoring console with camel, cometd and jquery

October 27th, 2009  |  Published in oss, soa, web  |  14 Comments

Here’s a simple file monitoring tool which uses cometd to push file changes to a webpage.

The server part consists of a groovy script which uses Apache Camel to monitor some files. It then uses Apache Camel to push the lines added to the files to the browser with Cometd. In the webpage a bit of jquery and a jquery comet library is used to add the received lines to the web page.

First the server part. I’m using the Groovy Ivy support to automatically download all the required libraries. This means that you can just start the script from the command line, no need to deploy it into any application server. Using stream:file you can monitor files. The lines appended to the files are put into a message object, LogMessage, and a source name is added so we can color the lines in the browser based on their source. Finally, the message are made available using the comet protocol on port 8082.

#!/home/akoelewijn/programs/groovy-1.6.3/bin/groovy

import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.language.groovy.GroovyRouteBuilder;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.mortbay.util.ajax.JSONPojoConvertor
import org.mortbay.util.ajax.JSON

@Grab(group='org.apache.camel', module='camel-groovy', version='2.0.0')
@Grab(group='org.apache.camel', module='camel-jetty', version='2.0.0')
@Grab(group='org.apache.camel', module='camel-cometd', version='2.0.0')
@Grab(group='org.apache.camel', module='camel-stream', version='2.0.0')
@Grab(group='org.apache.camel', module='camel-xstream', version='2.0.0')
@Grab(group='org.apache.camel', module='camel-core', version='2.0.0')
class SampleRoute extends GroovyRouteBuilder {
  void configure(){
        from("stream:in").to("seda:logmsgs?size=10000&concurrentConsumers=5")
  	from("stream:file?fileName=/var/log/messages&scanStream=true&scanStreamDelay=50").
          process(new AddSourceProcessor('file1')).
  	  to("seda:logmsgs?size=10000&concurrentConsumers=5")
  	from("stream:file?fileName=/var/log/syslog&scanStream=true&scanStreamDelay=50").
          process(new AddSourceProcessor('file2')).
  	  to("seda:logmsgs?size=10000&concurrentConsumers=5")
        from("jetty:http://localhost:8081/msgs").
          process(new AddSourceProcessor('file3')).
          to("seda:logmsgs?size=10000&concurrentConsumers=5")
  	from("seda:logmsgs?size=10000&concurrentConsumers=5").
  	  to("cometd://0.0.0.0:8082/service/logs?resourceBase=.")
  }
}

public class LogMessage implements JSON.Convertible {
  String source
  String message
  public void fromJSON(Map object){}
  public void toJSON(JSON.Output out){
    (new JSONPojoConvertor(LogMessage)).toJSON(this,out)
  }
}

public class AddSourceProcessor implements Processor {
    def sourceName
    public AddSourceProcessor(name){
      sourceName = name
    }
    public void process(Exchange exchange) throws Exception {
        exchange.in.setHeader('srcName',sourceName)
        def msg = new LogMessage(source:sourceName, message: exchange.in.body)
        exchange.in.setBody(msg)
    }
}

def camelCtx = new DefaultCamelContext()
camelCtx.addRoutes(new SampleRoute());
camelCtx.start();

The html page is pretty basic, just loading all the required javascript files:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <script src="http://www.google.com/jsapi"></script>
    <script src="log-console.js"></script>
    <link type="text/css" href="style.css"
          rel="stylesheet" media="all"/>
  </head>
  <body>
    <pre>Log output</pre>
  </body>
</html>

The javascript file uses the jquery library hosted by google. When it is loaded two jquery plugins are loaded from googlecode, jquerycomet, and jquery scrollTo. When everything is loaded, a connect is made to the comet endpoint at 8082. The receive function will be called when a message is received on /service/logs. Receive adds a pre element to the body element with the received message.

google.load("jquery", "1");
google.setOnLoadCallback(function() {
  $.getScript('http://flesler-plugins.googlecode.com/files/jquery.scrollTo-1.4.2-min.js');
  $.getScript("http://jquerycomet.googlecode.com/svn/trunk/jquery.comet.js", function(){
    console.log("done loading js");
    $.comet.init("http://localhost:8082/cometd");
    $.comet.subscribe("/service/logs", receive);
  });
});
function receive(message) {
  console.log("message: " + message + ", " + message.data + ", header: " + message.header );
  var msg = eval(message.data);
  console.log("msg: " + msg);
  $("body").append("<pre class='" + msg.source + "'>" + msg.message.replace(/</g,'\&lt;').replace(/>/,'&gt;') + "</pre>");
  $("body:last-child").scrollTo('100%',10,{axis:'y'});
}

Finally a bit a css to give the lines different colors based on their source:

body { margin: 0; padding: 0; }
pre {
  font-family: monospace;
  margin: 0; padding: 3px; border-bottom: 1px solid #ddd;
  background: #fff;
  white-space: pre-wrap;
}
pre.file1 { background-color: #f0e0e0; }
pre.file2 { background-color: #f0f0f0; }
pre.file3 { background-color: #ffeeee; }
pre:hover { background-color: #ffeeee; }

Adding remote file monitors should be pretty simple: just add another script which monitors files and pushes the changes to a shared remotely available queue, something like ActiveMq for example. The central groovy script above just needs to have one more source to read message from.

  • http://twitter.com/Dave_Bush Dave_Bush

    New Link: A simple file monitoring console with camel, cometd and jquery …: The javascript file uses.. http://bit.ly/2GGbS8
    This comment was originally posted on Twitter

  • http://twitter.com/mrhaki mrhaki

    Great #groovy script to monitor files and combine with cometd in the browser by @andrkoel: http://bit.ly/2dg2iv
    This comment was originally posted on Twitter

  • http://twitter.com/gdickens gdickens

    A simple file monitoring #console with #camel, #cometd and #jquery http://ow.ly/x3JL #java
    This comment was originally posted on Twitter

  • http://twitter.com/glaforge glaforge

    Cool stuff. RT: @mrhaki: Great #groovy script to monitor files and combine with cometd in the browser by @andrkoel: http://bit.ly/2dg2iv
    This comment was originally posted on Twitter

  • http://twitter.com/jboyens JR Boyens

    This is really cool. And I’ve tried it, but it doesn’t seem to work.

    I ended up adding a Jetty dependency and a Cometd dep to get it to at least start:

    @Grab(group=’org.mortbay.jetty’, module=’jetty’, version=’6.1.14′)
    @Grab(group=’org.mortbay.jetty’, module=’cometd-jetty’, version=’6.1.14′)

    Also, the Javascript REQUIRES Firebug open with the console on.

    Even then, no messages were ever posted. Nothing ever appears from cometd no matter what I tried.

    Help?

  • http://twitter.com/ronotica ronotica

    Really neat RT: @mrhaki: Great #groovy script to monitor files and combine with cometd in the browser by @andrkoel: http://bit.ly/2dg2iv
    This comment was originally posted on Twitter

  • http://twitter.com/jboyens JR Boyens

    This is really cool. And I’ve tried it, but it doesn’t seem to work.

    I ended up adding a Jetty dependency and a Cometd dep to get it to at least start:

    @Grab(group=’org.mortbay.jetty’, module=’jetty’, version=’6.1.14′)
    @Grab(group=’org.mortbay.jetty’, module=’cometd-jetty’, version=’6.1.14′)

    Also, the Javascript REQUIRES Firebug open with the console on.

    Even then, no messages were ever posted. Nothing ever appears from cometd no matter what I tried.

    Help?

  • http://twitter.com/jboyens JR Boyens

    Nevermind. It seems that actually adding those dependencies broke the whole thing with a class cast that I couldn’t see until I turned on Camel tracing.

    This is really sweet.

    Thanks!

  • http://twitter.com/jboyens JR Boyens

    Nevermind. It seems that actually adding those dependencies broke the whole thing with a class cast that I couldn’t see until I turned on Camel tracing.

    This is really sweet.

    Thanks!

  • http://davsclaus.blogspot.com/ Claus Ibsen

    Fantastic nice example.

    I have added a link to it from the Camel articles webpage.

  • http://davsclaus.blogspot.com/ Claus Ibsen

    Fantastic nice example.

    I have added a link to it from the Camel articles webpage.

  • KarlP

    Just tried this out, it’s very cool.

    Couple of questions though..
    1) It only reads line by line, so it only updates a line every “camel scan interval” msecs. Do you know how to make it slurp up all the lines since the last scan?

    2) It always starts from the top of the file. If I go to the “log viewer” after the app has started running, it takes a LONG time to chew through the file, line by line. Did you work around this at all?

  • KarlP

    Just tried this out, it’s very cool.

    Couple of questions though..
    1) It only reads line by line, so it only updates a line every “camel scan interval” msecs. Do you know how to make it slurp up all the lines since the last scan?

    2) It always starts from the top of the file. If I go to the “log viewer” after the app has started running, it takes a LONG time to chew through the file, line by line. Did you work around this at all?

  • http://twitter.com/marcelmaatkamp Marcel Maatkamp

    Excellent nice little demonstration!

    But I took the liberty to modify the code so that it would actually work on my system (JR Boyens seems to have the same problem). I've uploaded the code to http://my-gnuradio-toolset.googlecode.com/svn/t…
    (this link is actually clickable)

    I have upgraded the libraries, used json-lib as a bridge between java and cometd, made it actually run in Google Chrome and stripped out all firebug console.log messages so that the code will run even without firebug.

    Marcel Maatkamp

Tags

activemq agile bi camel css esb google governance grails groovy gtalk html5 internet ipad ivy J2EE java javascript jaxrs jmx jquery lean linkeddata linux maven mule noiv openoffice opensource Open Source oracle osgi oss rdbms rest scrum servicemix soa sql svg tablet web 2.0 XML xmpp yql

Archives

  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • June 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Recent Posts

  • Updating a vagrant box
  • Using littleproxy in Mule unit tests
  • Useful site to determine what html5, css3 & svg you can use
  • A Product Owner is a Project Manager
  • Using css webfonts in inkscape

Categories

  • agile
  • architecture
  • cloud
  • java
  • mobile
  • open standards
  • oracle
  • oss
  • other
  • soa
  • software development
  • tablet
  • Uncategorized
  • web

Recent Comments

  • Pcdiggs on Software architecture, PHP and Javascript
  • Een Scrum Product Owner is een Project Leider on A Product Owner is a Project Manager
  • Using css webfonts in inkscape :: Andrej Koelewijn on Create presentations using inkscape
  • Create presentations using inkscape :: Andrej Koelewijn on Presentation: Introduction to Scrum
  • Gebhard Greiter on What is Agile?
Buzz
andrkoel: Utrecht hele dag mist, scheveningen zomerse dag... http://t.co/cDRCJHr9
4:35 PM Nov 10, 2011, comment
andrkoel: RT @stefanvdkamp: Beter filmpje van Garret McNamara in de 30 meter hoge golf. http://t.co/9abiWkYX
9:20 AM Nov 10, 2011, comment
andrkoel: Mmm, een uur voor den haag - leiden lijkt te weinig, ga mijn afspraak niet redden...
7:34 AM Nov 10, 2011, comment
andrkoel: Big Data is Useless without Science http://t.co/2I8EiLsH
6:18 AM Nov 10, 2011, comment
andrkoel: Just tried #vagrant to quickly setup virtualbox development environment. Looks good, although provisioning apache through puppet failed...
10:47 PM Nov 09, 2011, comment
 


©2012 Andrej Koelewijn
Powered by WordPress using the Gridline Lite theme by Graph Paper Press.