Repository: camel Updated Branches: refs/heads/master ed6056171 -> 384a86005
CAMEL-9878 camel-commands - Add command to show top N inflight exchanges per routes Added new argument (`route`) to `context-inflight` command. Client can use the `route` argument to specify the route which inflight exchanges should be displayed. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/384a8600 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/384a8600 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/384a8600 Branch: refs/heads/master Commit: 384a86005ea17538f1911bfced81c98a202003fc Parents: ed60561 Author: Zoran Regvart <[email protected]> Authored: Sat Jun 18 18:53:22 2016 +0200 Committer: Claus Ibsen <[email protected]> Committed: Tue Jun 21 08:47:41 2016 +0200 ---------------------------------------------------------------------- .../commands/AbstractLocalCamelController.java | 4 +- .../apache/camel/commands/CamelController.java | 3 +- .../camel/commands/ContextInflightCommand.java | 6 +- .../AbstractLocalCamelControllerTest.java | 95 ++++++++++++++++++++ .../jolokia/DefaultJolokiaCamelController.java | 4 +- .../commands/jolokia/JolokiaRemoteTest.java | 2 +- .../camel/karaf/commands/ContextInflight.java | 5 +- 7 files changed, 110 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/384a8600/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractLocalCamelController.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractLocalCamelController.java b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractLocalCamelController.java index 6d80584..03f82dc 100644 --- a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractLocalCamelController.java +++ b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractLocalCamelController.java @@ -174,7 +174,7 @@ public abstract class AbstractLocalCamelController extends AbstractCamelControll } @SuppressWarnings("unchecked") - public List<Map<String, Object>> browseInflightExchanges(String camelContextName, int limit, boolean sortByLongestDuration) throws Exception { + public List<Map<String, Object>> browseInflightExchanges(String camelContextName, String route, int limit, boolean sortByLongestDuration) throws Exception { CamelContext context = this.getLocalCamelContext(camelContextName); if (context == null) { return null; @@ -187,7 +187,7 @@ public abstract class AbstractLocalCamelController extends AbstractCamelControll MBeanServer mBeanServer = agent.getMBeanServer(); ObjectName on = new ObjectName(agent.getMBeanObjectDomainName() + ":type=services,name=DefaultInflightRepository,context=" + context.getManagementName()); if (mBeanServer.isRegistered(on)) { - TabularData list = (TabularData) mBeanServer.invoke(on, "browse", new Object[]{limit, sortByLongestDuration}, new String[]{"int", "boolean"}); + TabularData list = (TabularData) mBeanServer.invoke(on, "browse", new Object[]{route, limit, sortByLongestDuration}, new String[]{"java.lang.String", "int", "boolean"}); Collection<CompositeData> values = (Collection<CompositeData>) list.values(); for (CompositeData data : values) { Map<String, Object> row = new LinkedHashMap<String, Object>(); http://git-wip-us.apache.org/repos/asf/camel/blob/384a8600/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java index 65c2139..a13b10f 100644 --- a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java +++ b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java @@ -66,12 +66,13 @@ public interface CamelController { * Browses the inflight exchanges * * @param camelContextName the Camel context. + * @param route the Camel route ID * @param limit maximum number of exchanges to return * @param sortByLongestDuration <tt>true</tt> to sort by longest duration, <tt>false</tt> to sort by exchange id * @return a list of key/value pairs with inflight exchange information * @throws java.lang.Exception can be thrown */ - List<Map<String, Object>> browseInflightExchanges(String camelContextName, int limit, boolean sortByLongestDuration) throws Exception; + List<Map<String, Object>> browseInflightExchanges(String camelContextName, String route, int limit, boolean sortByLongestDuration) throws Exception; /** * Starts the given Camel context. http://git-wip-us.apache.org/repos/asf/camel/blob/384a8600/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ContextInflightCommand.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ContextInflightCommand.java b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ContextInflightCommand.java index 896b719..8c78aac 100644 --- a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ContextInflightCommand.java +++ b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/ContextInflightCommand.java @@ -43,17 +43,19 @@ public class ContextInflightCommand extends AbstractContextCommand { private static final int MIN_COLUMN_WIDTH = 12; private int limit; + private String route; private boolean sortByLongestDuration; - public ContextInflightCommand(String context, int limit, boolean sortByLongestDuration) { + public ContextInflightCommand(String context, String route, int limit, boolean sortByLongestDuration) { super(context); + this.route = route; this.limit = limit; this.sortByLongestDuration = sortByLongestDuration; } @Override protected Object performContextCommand(CamelController camelController, String contextName, PrintStream out, PrintStream err) throws Exception { - List<Map<String, Object>> inflight = camelController.browseInflightExchanges(contextName, limit, sortByLongestDuration); + List<Map<String, Object>> inflight = camelController.browseInflightExchanges(contextName, route, limit, sortByLongestDuration); final Map<String, Integer> columnWidths = computeColumnWidths(inflight); final String headerFormat = buildFormatString(columnWidths, true); http://git-wip-us.apache.org/repos/asf/camel/blob/384a8600/platforms/commands/commands-core/src/test/java/org/apache/camel/commands/AbstractLocalCamelControllerTest.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-core/src/test/java/org/apache/camel/commands/AbstractLocalCamelControllerTest.java b/platforms/commands/commands-core/src/test/java/org/apache/camel/commands/AbstractLocalCamelControllerTest.java new file mode 100644 index 0000000..c55be8b --- /dev/null +++ b/platforms/commands/commands-core/src/test/java/org/apache/camel/commands/AbstractLocalCamelControllerTest.java @@ -0,0 +1,95 @@ +package org.apache.camel.commands; + +import java.util.List; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.impl.ExplicitCamelContextNameStrategy; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class AbstractLocalCamelControllerTest { + + private final DummyCamelController localCamelController; + + private final CamelContext context; + + public AbstractLocalCamelControllerTest() throws Exception { + context = new DefaultCamelContext(); + context.setNameStrategy(new ExplicitCamelContextNameStrategy("context1")); + + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start1").id("route1").delay(100).to("mock:result1"); + from("direct:start2").id("route2").delay(100).to("mock:result2"); + from("direct:start3").id("route3").delay(100).to("mock:result3"); + } + }); + + localCamelController = new DummyCamelController(context); + } + + @Before + public void startContext() throws Exception { + context.start(); + } + + @After + public void stopContext() throws Exception { + context.stop(); + } + + @Test + public void testBrowseInflightExchangesWithMoreRoutes() throws Exception { + context.createProducerTemplate().asyncSendBody("direct:start1", "Start one"); + context.createProducerTemplate().asyncSendBody("direct:start2", "Start two"); + context.createProducerTemplate().asyncSendBody("direct:start3", "Start three"); + + // let the exchange proceed + Thread.sleep(50); + + final List<Map<String, Object>> inflightExchanges = localCamelController.browseInflightExchanges("context1", null, 0, false); + + assertEquals("Context should contain three inflight exchanges", 3, inflightExchanges.size()); + } + + @Test + public void testBrowseInflightExchangesWithNoRoutes() throws Exception { + final List<Map<String, Object>> inflightExchanges = localCamelController.browseInflightExchanges("context1", null, 0, false); + + assertTrue("Context without routes should not have any inflight exchanges", inflightExchanges.isEmpty()); + } + + @Test + public void testBrowseInflightExchangesWithOneRoute() throws Exception { + context.createProducerTemplate().asyncSendBody("direct:start1", "Start one"); + + // let the exchange proceed + Thread.sleep(50); + + final List<Map<String, Object>> inflightExchanges = localCamelController.browseInflightExchanges("context1", null, 0, false); + + assertEquals("Context should contain one inflight exchange", 1, inflightExchanges.size()); + } + + @Test + public void testBrowseInflightExchangesWithSpecificRoute() throws Exception { + context.createProducerTemplate().asyncSendBody("direct:start1", "Start one"); + context.createProducerTemplate().asyncSendBody("direct:start2", "Start two"); + context.createProducerTemplate().asyncSendBody("direct:start3", "Start three"); + + // let the exchanges proceed + Thread.sleep(50); + + final List<Map<String, Object>> inflightExchanges = localCamelController.browseInflightExchanges("context1", "route2", 0, false); + + assertEquals("Context should contain one inflight exchange for specific route", 1, inflightExchanges.size()); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/384a8600/platforms/commands/commands-jolokia/src/main/java/org/apache/camel/commands/jolokia/DefaultJolokiaCamelController.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-jolokia/src/main/java/org/apache/camel/commands/jolokia/DefaultJolokiaCamelController.java b/platforms/commands/commands-jolokia/src/main/java/org/apache/camel/commands/jolokia/DefaultJolokiaCamelController.java index d550be5..5b02abb 100644 --- a/platforms/commands/commands-jolokia/src/main/java/org/apache/camel/commands/jolokia/DefaultJolokiaCamelController.java +++ b/platforms/commands/commands-jolokia/src/main/java/org/apache/camel/commands/jolokia/DefaultJolokiaCamelController.java @@ -235,7 +235,7 @@ public class DefaultJolokiaCamelController extends AbstractCamelController imple } @Override - public List<Map<String, Object>> browseInflightExchanges(String camelContextName, int limit, boolean sortByLongestDuration) throws Exception { + public List<Map<String, Object>> browseInflightExchanges(String camelContextName, String route, int limit, boolean sortByLongestDuration) throws Exception { if (jolokia == null) { throw new IllegalStateException("Need to connect to remote jolokia first"); } @@ -246,7 +246,7 @@ public class DefaultJolokiaCamelController extends AbstractCamelController imple if (found != null) { String pattern = String.format("%s:context=%s,type=services,name=DefaultInflightRepository", found.getDomain(), found.getKeyProperty("context")); ObjectName on = ObjectName.getInstance(pattern); - J4pExecResponse er = jolokia.execute(new J4pExecRequest(on, "browse(int,boolean)", limit, sortByLongestDuration)); + J4pExecResponse er = jolokia.execute(new J4pExecRequest(on, "browse(String,int,boolean)", route, limit, sortByLongestDuration)); if (er != null) { JSONObject data = er.getValue(); if (data != null) { http://git-wip-us.apache.org/repos/asf/camel/blob/384a8600/platforms/commands/commands-jolokia/src/test/java/org/apache/camel/commands/jolokia/JolokiaRemoteTest.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-jolokia/src/test/java/org/apache/camel/commands/jolokia/JolokiaRemoteTest.java b/platforms/commands/commands-jolokia/src/test/java/org/apache/camel/commands/jolokia/JolokiaRemoteTest.java index 2b41add..c06eaa4 100644 --- a/platforms/commands/commands-jolokia/src/test/java/org/apache/camel/commands/jolokia/JolokiaRemoteTest.java +++ b/platforms/commands/commands-jolokia/src/test/java/org/apache/camel/commands/jolokia/JolokiaRemoteTest.java @@ -218,7 +218,7 @@ public class JolokiaRemoteTest { controller = new DefaultJolokiaCamelController(); controller.connect(url, null, null); - List<Map<String, Object>> data = controller.browseInflightExchanges("camel-1", 500, false); + List<Map<String, Object>> data = controller.browseInflightExchanges("camel-1", "route1", 500, false); System.out.println(data); } http://git-wip-us.apache.org/repos/asf/camel/blob/384a8600/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ContextInflight.java ---------------------------------------------------------------------- diff --git a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ContextInflight.java b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ContextInflight.java index cba5ca3..1ff5e7f 100644 --- a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ContextInflight.java +++ b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ContextInflight.java @@ -31,12 +31,15 @@ public class ContextInflight extends CamelCommandSupport { required = false, multiValued = false) int limit = -1; + @Argument(index = 1, name = "route", description = "The Camel route ID", required = false, multiValued = false) + String route; + @Option(name = "--sort", aliases = "-s", description = "true = sort by longest duration, false = sort by exchange id", required = false, multiValued = false, valueToShowInHelp = "false") boolean sortByLongestDuration; protected Object doExecute() throws Exception { - ContextInflightCommand command = new ContextInflightCommand(name, limit, sortByLongestDuration); + ContextInflightCommand command = new ContextInflightCommand(name, route, limit, sortByLongestDuration); return command.execute(camelController, System.out, System.err); }
