Search in sources :

Example 41 with ArrayList

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

the class SamplingThrottlerTest method testSamplingFromExchangeStream.

public void testSamplingFromExchangeStream() throws Exception {
    NotifyBuilder notify = new NotifyBuilder(context).whenDone(15).create();
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMinimumMessageCount(2);
    mock.setResultWaitTime(3000);
    List<Exchange> sentExchanges = new ArrayList<Exchange>();
    sendExchangesThroughDroppingThrottler(sentExchanges, 15);
    notify.matchesMockWaitTime();
    mock.assertIsSatisfied();
    validateDroppedExchanges(sentExchanges, mock.getReceivedCounter());
}
Also used : NotifyBuilder(org.apache.camel.builder.NotifyBuilder) Exchange(org.apache.camel.Exchange) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ArrayList(java.util.ArrayList)

Example 42 with ArrayList

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

the class DistributedConcurrentPerCorrelationKeyTest method testAggregateConcurrentPerCorrelationKey.

public void testAggregateConcurrentPerCorrelationKey() throws Exception {
    ExecutorService service = Executors.newFixedThreadPool(50);
    List<Callable<Object>> tasks = new ArrayList<Callable<Object>>();
    for (int i = 0; i < size; i++) {
        final int id = i % 25;
        final int choice = i % 2;
        final int count = i;
        tasks.add(new Callable<Object>() {

            public Object call() throws Exception {
                if (choice == 0) {
                    template.sendBodyAndHeader(uri, "" + count, "id", id);
                } else {
                    template2.sendBodyAndHeader(uri, "" + count, "id", id);
                }
                return null;
            }
        });
    }
    MockEndpoint mock = getMockEndpoint("mock:result");
    MockEndpoint mock2 = getMockEndpoint2("mock:result");
    // submit all tasks
    service.invokeAll(tasks);
    service.shutdown();
    service.awaitTermination(10, TimeUnit.SECONDS);
    int contextCount = mock.getReceivedCounter();
    int context2Count = mock2.getReceivedCounter();
    assertEquals(25, contextCount + context2Count);
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Example 43 with ArrayList

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

the class DistributedOptimisticLockFailingTest method testEverySecondOneFails.

public void testEverySecondOneFails() throws Exception {
    int size = 200;
    ExecutorService service = Executors.newFixedThreadPool(50);
    List<Callable<Object>> tasks = new ArrayList<Callable<Object>>();
    for (int i = 0; i < size; i++) {
        final int id = i % 25;
        final int choice = i % 2;
        final int count = i;
        tasks.add(new Callable<Object>() {

            public Object call() throws Exception {
                if (choice == 0) {
                    template.sendBodyAndHeader("direct:everysecondone", "" + count, "id", id);
                } else {
                    template2.sendBodyAndHeader("direct:everysecondone", "" + count, "id", id);
                }
                return null;
            }
        });
    }
    MockEndpoint mock = getMockEndpoint("mock:result");
    MockEndpoint mock2 = getMockEndpoint2("mock:result");
    // submit all tasks
    service.invokeAll(tasks);
    service.shutdown();
    service.awaitTermination(10, TimeUnit.SECONDS);
    int contextCount = mock.getReceivedCounter();
    int context2Count = mock2.getReceivedCounter();
    assertEquals(25, contextCount + context2Count);
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Callable(java.util.concurrent.Callable) CamelExecutionException(org.apache.camel.CamelExecutionException) CamelExchangeException(org.apache.camel.CamelExchangeException)

Example 44 with ArrayList

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

the class AlbertoAggregatorTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        AggregationStrategy surnameAggregator = new AggregationStrategy() {

            @SuppressWarnings("unchecked")
            public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
                debugIn("Surname Aggregator", oldExchange, newExchange);
                Exchange answer = newExchange;
                if (oldExchange != null) {
                    List<String> brothers = oldExchange.getIn().getBody(List.class);
                    brothers.add(newExchange.getIn().getBody(String.class));
                    answer = oldExchange;
                } else {
                    List<String> brothers = new ArrayList<String>();
                    brothers.add(newExchange.getIn().getBody(String.class));
                    newExchange.getIn().setBody(brothers);
                }
                debugOut("Surname Aggregator", answer);
                return answer;
            }
        };

        @SuppressWarnings("unchecked")
        AggregationStrategy brothersAggregator = new AggregationStrategy() {

            public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
                debugIn("Brothers Aggregator", oldExchange, newExchange);
                Exchange answer = newExchange;
                if (oldExchange != null) {
                    Map<String, List<?>> brothers = oldExchange.getIn().getBody(Map.class);
                    brothers.put(newExchange.getIn().getHeader(SURNAME_HEADER, String.class), newExchange.getIn().getBody(List.class));
                    answer = oldExchange;
                } else {
                    Map<String, List<?>> brothers = new HashMap<String, List<?>>();
                    brothers.put(newExchange.getIn().getHeader(SURNAME_HEADER, String.class), newExchange.getIn().getBody(List.class));
                    newExchange.getIn().setBody(brothers);
                }
                debugOut("Brothers Aggregator", answer);
                return answer;
            }
        };

        private void debugIn(String stringId, Exchange oldExchange, Exchange newExchange) {
            if (oldExchange != null) {
                log.debug(stringId + " old headers in: " + oldExchange.getIn().getHeaders());
                log.debug(stringId + " old body in: " + oldExchange.getIn().getBody());
            }
            log.debug(stringId + " new headers in: " + newExchange.getIn().getHeaders());
            log.debug(stringId + " new body in: " + newExchange.getIn().getBody());
        }

        private void debugOut(String stringId, Exchange exchange) {
            log.debug(stringId + " old headers out: " + exchange.getIn().getHeaders());
            log.debug(stringId + " old body out: " + exchange.getIn().getBody());
        }

        @Override
        public void configure() throws Exception {
            from("direct:start").split(bodyAs(String.class).tokenize(",")).process(// header
            new Processor() {

                public void process(Exchange exchange) throws Exception {
                    String[] parts = exchange.getIn().getBody(String.class).split(" ");
                    exchange.getIn().setBody(parts[0]);
                    exchange.getIn().setHeader(SURNAME_HEADER, parts[1]);
                }
            }).to("direct:joinSurnames");
            from("direct:joinSurnames").aggregate(header(SURNAME_HEADER), surnameAggregator).completionTimeout(2000L).setHeader(TYPE_HEADER, constant(BROTHERS_TYPE)).to("direct:joinBrothers");
            // Join all brothers lists and remove surname and type headers
            AggregateDefinition agg = from("direct:joinBrothers").aggregate(header(TYPE_HEADER), brothersAggregator);
            agg.setCompletionTimeout(2000L);
            agg.removeHeader(SURNAME_HEADER).removeHeader(TYPE_HEADER).to("mock:result");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) List(java.util.List) ArrayList(java.util.ArrayList) AggregateDefinition(org.apache.camel.model.AggregateDefinition) Map(java.util.Map) HashMap(java.util.HashMap) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 45 with ArrayList

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

the class AlbertoAggregatorTest method testAggregator.

public void testAggregator() throws Exception {
    String allNames = "Harpo Marx,Fiodor Karamazov,Chico Marx,Ivan Karamazov,Groucho Marx,Alexei Karamazov,Dimitri Karamazov";
    List<String> marxBrothers = new ArrayList<String>();
    marxBrothers.add("Harpo");
    marxBrothers.add("Chico");
    marxBrothers.add("Groucho");
    List<String> karamazovBrothers = new ArrayList<String>();
    karamazovBrothers.add("Fiodor");
    karamazovBrothers.add("Ivan");
    karamazovBrothers.add("Alexei");
    karamazovBrothers.add("Dimitri");
    Map<String, List<String>> allBrothers = new HashMap<String, List<String>>();
    allBrothers.put("Marx", marxBrothers);
    allBrothers.put("Karamazov", karamazovBrothers);
    MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
    resultEndpoint.expectedMessageCount(1);
    resultEndpoint.expectedBodiesReceived(allBrothers);
    template.sendBody("direct:start", allNames);
    assertMockEndpointsSatisfied();
}
Also used : HashMap(java.util.HashMap) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)55702 Test (org.junit.Test)8169 List (java.util.List)6815 HashMap (java.util.HashMap)5856 IOException (java.io.IOException)3899 Map (java.util.Map)3195 File (java.io.File)3090 HashSet (java.util.HashSet)2245 Iterator (java.util.Iterator)1591 Test (org.testng.annotations.Test)1074 SQLException (java.sql.SQLException)1046 ResultSet (java.sql.ResultSet)1017 Date (java.util.Date)997 Set (java.util.Set)917 LinkedHashMap (java.util.LinkedHashMap)886 PreparedStatement (java.sql.PreparedStatement)882 Collection (java.util.Collection)751 LinkedList (java.util.LinkedList)677 BufferedReader (java.io.BufferedReader)663 Path (org.apache.hadoop.fs.Path)611