Author: davsclaus Date: Thu Apr 7 14:49:28 2011 New Revision: 1089901 URL: http://svn.apache.org/viewvc?rev=1089901&view=rev Log: CAMEL-3833: Added more stats to JMX.
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRedeliverRouteOnlyTest.java camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRedeliverTest.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/DelegatePerformanceCounter.java camel/trunk/camel-core/src/main/java/org/apache/camel/management/InstrumentationProcessor.java camel/trunk/camel-core/src/main/java/org/apache/camel/management/PerformanceCounter.java camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedPerformanceCounter.java camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/DelegatePerformanceCounter.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/DelegatePerformanceCounter.java?rev=1089901&r1=1089900&r2=1089901&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/management/DelegatePerformanceCounter.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/DelegatePerformanceCounter.java Thu Apr 7 14:49:28 2011 @@ -16,6 +16,8 @@ */ package org.apache.camel.management; +import org.apache.camel.Exchange; + /** * Delegates to another {@link org.apache.camel.management.PerformanceCounter}. * <p/> @@ -44,17 +46,17 @@ public class DelegatePerformanceCounter this.counter.setStatisticsEnabled(statisticsEnabled); } - public void completedExchange(long time) { - counter.completedExchange(time); + public void completedExchange(Exchange exchange, long time) { + counter.completedExchange(exchange, time); } - public void failedExchange() { - counter.failedExchange(); + public void failedExchange(Exchange exchange) { + counter.failedExchange(exchange); } public boolean isStatisticsEnabled() { // statistics is only considered enabled if we have a counter to delegate to - // otherwise we do not want to gather statistics (we are just a delegate with noone to delegate to) + // otherwise we do not want to gather statistics (we are just a delegate with none to delegate to) return counter != null && counter.isStatisticsEnabled(); } Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/InstrumentationProcessor.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/InstrumentationProcessor.java?rev=1089901&r1=1089900&r2=1089901&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/management/InstrumentationProcessor.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/InstrumentationProcessor.java Thu Apr 7 14:49:28 2011 @@ -82,9 +82,9 @@ public class InstrumentationProcessor ex } if (!exchange.isFailed() && exchange.getException() == null) { - counter.completedExchange(duration); + counter.completedExchange(exchange, duration); } else { - counter.failedExchange(); + counter.failedExchange(exchange); } } Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/PerformanceCounter.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/PerformanceCounter.java?rev=1089901&r1=1089900&r2=1089901&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/management/PerformanceCounter.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/PerformanceCounter.java Thu Apr 7 14:49:28 2011 @@ -16,6 +16,8 @@ */ package org.apache.camel.management; +import org.apache.camel.Exchange; + /** * A counter that gathers performance metrics when {@link org.apache.camel.Exchange} are routed in Camel. * @@ -26,14 +28,17 @@ public interface PerformanceCounter { /** * Executed when an {@link org.apache.camel.Exchange} is complete. * + * @param exchange the exchange * @param time the time it took in millis to complete it */ - void completedExchange(long time); + void completedExchange(Exchange exchange, long time); /** * Executed when an {@link org.apache.camel.Exchange} failed. + * + * @param exchange the exchange */ - void failedExchange(); + void failedExchange(Exchange exchange); /** * Is statistics enabled. Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedPerformanceCounter.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedPerformanceCounter.java?rev=1089901&r1=1089900&r2=1089901&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedPerformanceCounter.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedPerformanceCounter.java Thu Apr 7 14:49:28 2011 @@ -18,8 +18,10 @@ package org.apache.camel.management.mbea import java.util.Date; +import org.apache.camel.Exchange; import org.apache.camel.management.PerformanceCounter; import org.apache.camel.spi.ManagementStrategy; +import org.apache.camel.util.ExchangeHelper; import org.fusesource.commons.management.Statistic; import org.springframework.jmx.export.annotation.ManagedAttribute; import org.springframework.jmx.export.annotation.ManagedOperation; @@ -29,6 +31,8 @@ import org.springframework.jmx.export.an public abstract class ManagedPerformanceCounter extends ManagedCounter implements PerformanceCounter { private Statistic exchangesCompleted; private Statistic exchangesFailed; + private Statistic failuresHandled; + private Statistic redeliveries; private Statistic minProcessingTime; private Statistic maxProcessingTime; private Statistic totalProcessingTime; @@ -44,6 +48,10 @@ public abstract class ManagedPerformance super.init(strategy); this.exchangesCompleted = strategy.createStatistic("org.apache.camel.exchangesCompleted", this, Statistic.UpdateMode.COUNTER); this.exchangesFailed = strategy.createStatistic("org.apache.camel.exchangesFailed", this, Statistic.UpdateMode.COUNTER); + + this.failuresHandled = strategy.createStatistic("org.apache.camel.failuresHandled", this, Statistic.UpdateMode.COUNTER); + this.redeliveries = strategy.createStatistic("org.apache.camel.redeliveries", this, Statistic.UpdateMode.COUNTER); + this.minProcessingTime = strategy.createStatistic("org.apache.camel.minimumProcessingTime", this, Statistic.UpdateMode.MINIMUM); this.maxProcessingTime = strategy.createStatistic("org.apache.camel.maximumProcessingTime", this, Statistic.UpdateMode.MAXIMUM); this.totalProcessingTime = strategy.createStatistic("org.apache.camel.totalProcessingTime", this, Statistic.UpdateMode.COUNTER); @@ -62,6 +70,8 @@ public abstract class ManagedPerformance super.reset(); exchangesCompleted.reset(); exchangesFailed.reset(); + failuresHandled.reset(); + redeliveries.reset(); minProcessingTime.reset(); maxProcessingTime.reset(); totalProcessingTime.reset(); @@ -83,6 +93,16 @@ public abstract class ManagedPerformance return exchangesFailed.getValue(); } + @ManagedAttribute(description = "Number of failures handled") + public long getFailuresHandled() throws Exception { + return failuresHandled.getValue(); + } + + @ManagedAttribute(description = "Number of redeliveries") + public long getRedeliveries() throws Exception { + return redeliveries.getValue(); + } + @ManagedAttribute(description = "Min Processing Time [milliseconds]") public long getMinProcessingTime() throws Exception { return minProcessingTime.getValue(); @@ -142,15 +162,14 @@ public abstract class ManagedPerformance this.statisticsEnabled = statisticsEnabled; } - /** - * This method is called when an exchange has been processed successfully. - * - * @param time in milliseconds it spent on processing the exchange - */ - public synchronized void completedExchange(long time) { + public synchronized void completedExchange(Exchange exchange, long time) { increment(); exchangesCompleted.increment(); + if (ExchangeHelper.isFailureHandled(exchange)) { + failuresHandled.increment(); + } + minProcessingTime.updateValue(time); maxProcessingTime.updateValue(time); totalProcessingTime.updateValue(time); @@ -169,13 +188,14 @@ public abstract class ManagedPerformance meanProcessingTime.updateValue(mean); } - /** - * This method is called when an exchange has been processed and failed. - */ - public synchronized void failedExchange() { + public synchronized void failedExchange(Exchange exchange) { increment(); exchangesFailed.increment(); + if (ExchangeHelper.isRedelivered(exchange)) { + redeliveries.increment(); + } + long now = new Date().getTime(); if (firstExchangeFailureTimestamp.getUpdateCount() == 0) { firstExchangeFailureTimestamp.updateValue(now); Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java?rev=1089901&r1=1089900&r2=1089901&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java Thu Apr 7 14:49:28 2011 @@ -455,6 +455,10 @@ public final class ExchangeHelper { return exchange.getProperty(Exchange.REDELIVERY_EXHAUSTED, false, Boolean.class); } + public static boolean isRedelivered(Exchange exchange) { + return exchange.getIn().hasHeaders() && exchange.getIn().getHeader(Exchange.REDELIVERED, false, Boolean.class); + } + public static boolean isInterrupted(Exchange exchange) { return exchange.getException(InterruptedException.class) != null; } Added: camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRedeliverRouteOnlyTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRedeliverRouteOnlyTest.java?rev=1089901&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRedeliverRouteOnlyTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRedeliverRouteOnlyTest.java Thu Apr 7 14:49:28 2011 @@ -0,0 +1,77 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.management; + +import javax.management.MBeanServer; +import javax.management.ObjectName; + +import org.apache.camel.Exchange; +import org.apache.camel.ManagementStatisticsLevel; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; + +/** + * + */ +public class ManagedRedeliverRouteOnlyTest extends ManagementTestSupport { + + public void testRedeliver() throws Exception { + MBeanServer mbeanServer = getMBeanServer(); + + getMockEndpoint("mock:foo").expectedMessageCount(1); + + Object out = template.requestBody("direct:start", "Hello World"); + assertEquals("Error", out); + + assertMockEndpointsSatisfied(); + + ObjectName on = ObjectName.getInstance("org.apache.camel:context=localhost/camel-1,type=routes,name=\"route1\""); + + Long num = (Long) mbeanServer.getAttribute(on, "ExchangesCompleted"); + assertEquals(1, num.longValue()); + + num = (Long) mbeanServer.getAttribute(on, "ExchangesFailed"); + assertEquals(0, num.longValue()); + + num = (Long) mbeanServer.getAttribute(on, "FailuresHandled"); + assertEquals(1, num.longValue()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + context.getManagementStrategy().setStatisticsLevel(ManagementStatisticsLevel.RoutesOnly); + + onException(Exception.class).handled(true) + .maximumRedeliveries(4).logStackTrace(false) + .setBody().constant("Error"); + + from("direct:start") + .to("mock:foo") + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + log.info("Invoking me"); + + throw new IllegalArgumentException("Damn"); + } + }).id("myprocessor"); + } + }; + } +} Added: camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRedeliverTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRedeliverTest.java?rev=1089901&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRedeliverTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRedeliverTest.java Thu Apr 7 14:49:28 2011 @@ -0,0 +1,85 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.management; + +import javax.management.MBeanServer; +import javax.management.ObjectName; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; + +/** + * + */ +public class ManagedRedeliverTest extends ManagementTestSupport { + + public void testRedeliver() throws Exception { + MBeanServer mbeanServer = getMBeanServer(); + + getMockEndpoint("mock:foo").expectedMessageCount(1); + + Object out = template.requestBody("direct:start", "Hello World"); + assertEquals("Error", out); + + assertMockEndpointsSatisfied(); + + ObjectName on = ObjectName.getInstance("org.apache.camel:context=localhost/camel-1,type=routes,name=\"route1\""); + + Long num = (Long) mbeanServer.getAttribute(on, "ExchangesCompleted"); + assertEquals(1, num.longValue()); + + num = (Long) mbeanServer.getAttribute(on, "ExchangesFailed"); + assertEquals(0, num.longValue()); + + num = (Long) mbeanServer.getAttribute(on, "FailuresHandled"); + assertEquals(1, num.longValue()); + + on = ObjectName.getInstance("org.apache.camel:context=localhost/camel-1,type=processors,name=\"myprocessor\""); + + num = (Long) mbeanServer.getAttribute(on, "ExchangesCompleted"); + assertEquals(0, num.longValue()); + + num = (Long) mbeanServer.getAttribute(on, "ExchangesFailed"); + assertEquals(5, num.longValue()); + + num = (Long) mbeanServer.getAttribute(on, "Redeliveries"); + assertEquals(4, num.longValue()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + onException(Exception.class).handled(true) + .maximumRedeliveries(4).logStackTrace(false) + .setBody().constant("Error"); + + from("direct:start") + .to("mock:foo") + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + log.info("Invoking me"); + + throw new IllegalArgumentException("Damn"); + } + }).id("myprocessor"); + } + }; + } +}