Search in sources :

Example 71 with HashMap

use of java.util.HashMap in project camel by apache.

the class ManagedRoute method dumpRouteStatsAsXml.

public String dumpRouteStatsAsXml(boolean fullStats, boolean includeProcessors) throws Exception {
    // in this logic we need to calculate the accumulated processing time for the processor in the route
    // and hence why the logic is a bit more complicated to do this, as we need to calculate that from
    // the bottom -> top of the route but this information is valuable for profiling routes
    StringBuilder sb = new StringBuilder();
    // need to calculate this value first, as we need that value for the route stat
    Long processorAccumulatedTime = 0L;
    // gather all the processors for this route, which requires JMX
    if (includeProcessors) {
        sb.append("  <processorStats>\n");
        MBeanServer server = getContext().getManagementStrategy().getManagementAgent().getMBeanServer();
        if (server != null) {
            // get all the processor mbeans and sort them accordingly to their index
            String prefix = getContext().getManagementStrategy().getManagementAgent().getIncludeHostName() ? "*/" : "";
            ObjectName query = ObjectName.getInstance(jmxDomain + ":context=" + prefix + getContext().getManagementName() + ",type=processors,*");
            Set<ObjectName> names = server.queryNames(query, null);
            List<ManagedProcessorMBean> mps = new ArrayList<ManagedProcessorMBean>();
            for (ObjectName on : names) {
                ManagedProcessorMBean processor = context.getManagementStrategy().getManagementAgent().newProxyClient(on, ManagedProcessorMBean.class);
                // the processor must belong to this route
                if (getRouteId().equals(processor.getRouteId())) {
                    mps.add(processor);
                }
            }
            mps.sort(new OrderProcessorMBeans());
            // walk the processors in reverse order, and calculate the accumulated total time
            Map<String, Long> accumulatedTimes = new HashMap<String, Long>();
            Collections.reverse(mps);
            for (ManagedProcessorMBean processor : mps) {
                processorAccumulatedTime += processor.getTotalProcessingTime();
                accumulatedTimes.put(processor.getProcessorId(), processorAccumulatedTime);
            }
            // and reverse back again
            Collections.reverse(mps);
            // and now add the sorted list of processors to the xml output
            for (ManagedProcessorMBean processor : mps) {
                sb.append("    <processorStat").append(String.format(" id=\"%s\" index=\"%s\" state=\"%s\"", processor.getProcessorId(), processor.getIndex(), processor.getState()));
                // do we have an accumulated time then append that
                Long accTime = accumulatedTimes.get(processor.getProcessorId());
                if (accTime != null) {
                    sb.append(" accumulatedProcessingTime=\"").append(accTime).append("\"");
                }
                // use substring as we only want the attributes
                sb.append(" ").append(processor.dumpStatsAsXml(fullStats).substring(7)).append("\n");
            }
        }
        sb.append("  </processorStats>\n");
    }
    // route self time is route total - processor accumulated total)
    long routeSelfTime = getTotalProcessingTime() - processorAccumulatedTime;
    if (routeSelfTime < 0) {
        // ensure we don't calculate that as negative
        routeSelfTime = 0;
    }
    StringBuilder answer = new StringBuilder();
    answer.append("<routeStat").append(String.format(" id=\"%s\"", route.getId())).append(String.format(" state=\"%s\"", getState()));
    // use substring as we only want the attributes
    String stat = dumpStatsAsXml(fullStats);
    answer.append(" exchangesInflight=\"").append(getInflightExchanges()).append("\"");
    answer.append(" selfProcessingTime=\"").append(routeSelfTime).append("\"");
    InFlightKey oldestInflightEntry = getOldestInflightEntry();
    if (oldestInflightEntry == null) {
        answer.append(" oldestInflightExchangeId=\"\"");
        answer.append(" oldestInflightDuration=\"\"");
    } else {
        answer.append(" oldestInflightExchangeId=\"").append(oldestInflightEntry.exchangeId).append("\"");
        answer.append(" oldestInflightDuration=\"").append(System.currentTimeMillis() - oldestInflightEntry.timeStamp).append("\"");
    }
    answer.append(" ").append(stat.substring(7, stat.length() - 2)).append(">\n");
    if (includeProcessors) {
        answer.append(sb);
    }
    answer.append("</routeStat>");
    return answer.toString();
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) ManagedProcessorMBean(org.apache.camel.api.management.mbean.ManagedProcessorMBean) ObjectName(javax.management.ObjectName) MBeanServer(javax.management.MBeanServer)

Example 72 with HashMap

use of java.util.HashMap in project camel by apache.

the class MockEndpointTest method testSetMultipleExpectedHeaders4.

public void testSetMultipleExpectedHeaders4() throws Exception {
    // to test the header value with Stream which can only be consumed once
    InputStream is = new ByteArrayInputStream("Test".getBytes());
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    mock.expectedHeaderReceived("foo", 123);
    mock.expectedHeaderReceived("bar", "Test");
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("foo", 123);
    map.put("bar", is);
    template.sendBodyAndHeaders("direct:a", "Hello World", map);
    mock.assertIsSatisfied();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 73 with HashMap

use of java.util.HashMap in project camel by apache.

the class CollectionConverterTest method testToProperties.

public void testToProperties() {
    Map<Object, Object> map = new HashMap<Object, Object>();
    map.put("foo", "bar");
    Properties prop = CollectionConverter.toProperties(map);
    assertNotNull(prop);
    assertEquals(1, prop.size());
    assertEquals("bar", prop.get("foo"));
}
Also used : HashMap(java.util.HashMap) Properties(java.util.Properties)

Example 74 with HashMap

use of java.util.HashMap in project camel by apache.

the class DefaultExchangeHolderTest method testSkipNonSerializableDataFromMap.

public void testSkipNonSerializableDataFromMap() throws Exception {
    // use a mixed Map, the MyFoo is not serializable so the entire map should be skipped
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("A", "I am okay");
    map.put("B", new MyFoo("Tiger"));
    Exchange exchange = new DefaultExchange(context);
    exchange.getIn().setBody("Hello World");
    exchange.getIn().setHeader("Foo", map);
    exchange.getIn().setHeader("Bar", 123);
    DefaultExchangeHolder holder = DefaultExchangeHolder.marshal(exchange);
    exchange = new DefaultExchange(context);
    DefaultExchangeHolder.unmarshal(exchange, holder);
    // the non serializable header should be skipped
    assertEquals("Hello World", exchange.getIn().getBody());
    assertEquals(123, exchange.getIn().getHeader("Bar"));
    assertNull(exchange.getIn().getHeader("Foo"));
}
Also used : Exchange(org.apache.camel.Exchange) HashMap(java.util.HashMap)

Example 75 with HashMap

use of java.util.HashMap in project camel by apache.

the class DefaultProducerTemplateAsyncTest method testRequestAsyncBodyAndHeaders.

public void testRequestAsyncBodyAndHeaders() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("Hello World");
    mock.expectedHeaderReceived("foo", 123);
    mock.expectedHeaderReceived("bar", "cheese");
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put("foo", 123);
    headers.put("bar", "cheese");
    Future<Object> future = template.asyncRequestBodyAndHeaders("direct:start", "Hello", headers);
    long start = System.currentTimeMillis();
    // you can do other stuff
    String echo = template.requestBody("direct:echo", "Hi", String.class);
    assertEquals("HiHi", echo);
    // we can use extract body to convert to expect body type
    String result = template.extractFutureBody(future, String.class);
    assertMockEndpointsSatisfied();
    long delta = System.currentTimeMillis() - start;
    assertEquals("Hello World", result);
    assertTrue("Should take longer than: " + delta, delta > 250);
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) HashMap(java.util.HashMap)

Aggregations

HashMap (java.util.HashMap)24090 ArrayList (java.util.ArrayList)5632 Map (java.util.Map)5586 Test (org.junit.Test)5079 List (java.util.List)2721 HashSet (java.util.HashSet)2103 IOException (java.io.IOException)1780 Set (java.util.Set)1465 File (java.io.File)1206 Iterator (java.util.Iterator)1198 LinkedHashMap (java.util.LinkedHashMap)1138 Test (org.testng.annotations.Test)884 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)680 Date (java.util.Date)545 LinkedList (java.util.LinkedList)494 URI (java.net.URI)415 Properties (java.util.Properties)381 InputStream (java.io.InputStream)343 Collection (java.util.Collection)342 TreeMap (java.util.TreeMap)333