Try setting the route policy before adding the routes On Fri, Nov 7, 2014 at 1:35 AM, James L Greene <jlgree...@aep.com> wrote: > I have written a simple Java class to attempt to exercise the camel-metrics > component. In this example, I'm writing a standalone Java batch process, > which will read files from an input directory, and deposit the files in an > output directory. I'm attempting to use the camel-metrics component to > capture how many files were read (using the name "files-processed"). I've > configured the class to only run for seventy seconds, giving the > camel-metrics component an opportunity to output the values of the metric > being captured to the application log (which its configured to do at sixty > seconds intervals, and which appears to work properly). > > Prior to suspending/stopping my CamelContext, I attempted to retrieve the > metrics as described in the camel-metrics component documentation. > Unfortunately, when I look at the contents of the metrics, my custom metric > is nowhere to be found. Below, please find the source for the sample class > that I created, and its logged output. > > Am I doing something wrong? > > ============================ > > Camel Version > 2.14.0 > > JRE Version > java version "1.7.0_25" > Java(TM) SE Runtime Environment (build 1.7.0_25-b17) > Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode) > > ============================ > > package ... > > import com.codahale.metrics.Counter; > import com.codahale.metrics.MetricRegistry; > import java.util.SortedMap; > import org.apache.camel.CamelContext; > import org.apache.camel.builder.RouteBuilder; > import org.apache.camel.component.metrics.routepolicy.MetricsRegistryService; > import > org.apache.camel.component.metrics.routepolicy.MetricsRoutePolicyFactory; > import org.apache.camel.impl.DefaultCamelContext; > import org.slf4j.Logger; > import org.slf4j.LoggerFactory; > > public class Main { > > public static void main(String[] args) throws Exception { > Logger LOG = LoggerFactory.getLogger(Main.class); > > CamelContext context = new DefaultCamelContext(); > > RouteBuilder rb = new RouteBuilder() { > @Override > public void configure() throws Exception { > from("file://c:/test/input"). > routeId("testroute"). > log("Processing file [${header.CamelFileName}]"). > to("metrics:counter:files-processed"). > to("file://c:/test/output"); > } > }; > context.addRoutes(rb); > > MetricsRoutePolicyFactory mrpf = new MetricsRoutePolicyFactory(); > context.addRoutePolicyFactory(mrpf); > > context.start(); > > int sleepCounter = 0; > while (sleepCounter < 7) { > sleepCounter++; > Thread.sleep(10000); > LOG.info("Zzz...({} seconds)", sleepCounter * 10); > } > > MetricsRegistryService regSvc = > context.hasService(MetricsRegistryService.class); > if(regSvc != null) { > // Dump the statistics to the log in JSON format. > regSvc.setPrettyPrint(true); > LOG.info(regSvc.dumpStatisticsAsJson()); > > // Do it again, retrieving the map of counters from the > MetricRegistry object. > LOG.info("----------"); > MetricRegistry reg = regSvc.getMetricsRegistry(); > SortedMap<String, Counter> counters = reg.getCounters(); > LOG.info("Keys found {{}]", counters.keySet().size()); > for(String key : counters.keySet()) { > LOG.info("--- key [{}] - value [{}]", key, > counters.get("key").getCount()); > } > } else { > System.out.println("Couldn't find MetricsRegisteryService > instance"); > } > context.suspend(); > context.stop(); > } > } > > ============================ > --- exec-maven-plugin:1.2.1:exec (default-cli) @ metricstest --- > 2014-11-06 19:15:23 INFO DefaultCamelContext :1731 - Apache Camel > 2.14.0 (CamelContext: camel-1) is starting > 2014-11-06 19:15:23 INFO ManagedManagementStrategy : 187 - JMX is enabled > 2014-11-06 19:15:23 INFO DefaultTypeConverter : 50 - Loaded 178 type > converters > 2014-11-06 19:15:23 INFO MetricsComponent : 108 - Creating new > default MetricRegistry > 2014-11-06 19:15:23 INFO MetricsComponent : 57 - Metrics type: > counter; name: files-processed > 2014-11-06 19:15:23 INFO DefaultCamelContext :1931 - > AllowUseOriginalMessage is enabled. If access to the original message is not > needed, then its recommended to turn this option off as it may improve > performance. > 2014-11-06 19:15:23 INFO DefaultCamelContext :1941 - StreamCaching is > not in use. If using streams then its recommended to enable stream caching. > See more details at http://camel.apache.org/stream-caching.html > 2014-11-06 19:15:23 INFO DefaultCamelContext :2453 - Route: testroute > started and consuming from: Endpoint[file://c:/mdm/test/input] > 2014-11-06 19:15:23 INFO DefaultCamelContext :1766 - Total 1 routes, > of which 1 is started. > 2014-11-06 19:15:23 INFO DefaultCamelContext :1767 - Apache Camel > 2.14.0 (CamelContext: camel-1) started in 0.608 seconds > 2014-11-06 19:15:24 INFO testroute : 96 - Processing file > [testfile1.txt] > 2014-11-06 19:15:25 INFO testroute : 96 - Processing file > [testfile2.txt] > 2014-11-06 19:15:25 INFO testroute : 96 - Processing file > [testfile3.txt] > 2014-11-06 19:15:33 INFO Main : 47 - Zzz...(10 > seconds) > 2014-11-06 19:15:43 INFO Main : 47 - Zzz...(20 > seconds) > 2014-11-06 19:15:53 INFO Main : 47 - Zzz...(30 > seconds) > 2014-11-06 19:16:03 INFO Main : 47 - Zzz...(40 > seconds) > 2014-11-06 19:16:13 INFO Main : 47 - Zzz...(50 > seconds) > 2014-11-06 19:16:23 INFO MetricsComponent : 104 - type=COUNTER, > name=files-processed, count=3 > 2014-11-06 19:16:23 INFO Main : 47 - Zzz...(60 > seconds) > 2014-11-06 19:16:33 INFO Main : 47 - Zzz...(70 > seconds) > 2014-11-06 19:16:34 INFO Main : 53 - { > "version" : "3.0.0", > "gauges" : { }, > "counters" : { }, > "histograms" : { }, > "meters" : { }, > "timers" : { > "camel-1:testroute.responses" : { > "count" : 3, > "max" : 157.060387, > "mean" : 88.249202, > "min" : 44.265085, > "p50" : 63.422134, > "p75" : 157.060387, > "p95" : 157.060387, > "p98" : 157.060387, > "p99" : 157.060387, > "p999" : 157.060387, > "stddev" : 60.35712482735794, > "m15_rate" : 0.558194480485237, > "m1_rate" : 0.20307925506404548, > "m5_rate" : 0.48311899441084233, > "mean_rate" : 0.04263368988938351, > "duration_units" : "milliseconds", > "rate_units" : "calls/second" > } > } > } > 2014-11-06 19:16:34 INFO Main : 54 - ---------- > 2014-11-06 19:16:34 INFO Main : 57 - Keys found {0] > 2014-11-06 19:16:34 INFO DefaultCamelContext :1648 - Apache Camel > 2.14.0 (CamelContext: camel-1) is suspending > 2014-11-06 19:16:34 INFO DefaultShutdownStrategy : 172 - Starting to > graceful shutdown 1 routes (timeout 300 seconds) > 2014-11-06 19:16:34 INFO DefaultShutdownStrategy : 606 - Route: testroute > suspend complete, was consuming from: Endpoint[file://c:/mdm/test/input] > 2014-11-06 19:16:34 INFO DefaultShutdownStrategy : 222 - Graceful > shutdown of 1 routes completed in 0 seconds > 2014-11-06 19:16:34 INFO DefaultCamelContext :1686 - Apache Camel > 2.14.0 (CamelContext: camel-1) is suspended in 0.032 seconds > 2014-11-06 19:16:34 INFO DefaultCamelContext :1958 - Apache Camel > 2.14.0 (CamelContext: camel-1) is shutting down > 2014-11-06 19:16:34 INFO DefaultShutdownStrategy : 172 - Starting to > graceful shutdown 1 routes (timeout 300 seconds) > 2014-11-06 19:16:34 INFO DefaultShutdownStrategy : 609 - Route: testroute > shutdown complete, was consuming from: Endpoint[file://c:/mdm/test/input] > 2014-11-06 19:16:34 INFO DefaultShutdownStrategy : 222 - Graceful > shutdown of 1 routes completed in 0 seconds > 2014-11-06 19:16:34 INFO DefaultCamelContext :2040 - Apache Camel > 2.14.0 (CamelContext: camel-1) uptime 1 minute > 2014-11-06 19:16:34 INFO DefaultCamelContext :2041 - Apache Camel > 2.14.0 (CamelContext: camel-1) is shutdown in 0.000 seconds > ------------------------------------------------------------------------ > BUILD SUCCESS > ------------------------------------------------------------------------ > Total time: 1:12.415s > Finished at: Thu Nov 06 19:16:34 EST 2014 > Final Memory: 5M/120M > ------------------------------------------------------------------------
-- Claus Ibsen ----------------- Red Hat, Inc. Email: cib...@redhat.com Twitter: davsclaus Blog: http://davsclaus.com Author of Camel in Action: http://www.manning.com/ibsen hawtio: http://hawt.io/ fabric8: http://fabric8.io/