Anda di halaman 1dari 20

AggregateABCCloseTest

package com; import org.apache.camel.CamelExecutionException; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.processor.aggregate.ClosedCorre lationKeyException; import org.apache.camel.test.junit4.CamelTestSupport; import org.junit.Test; public class AggregateABCCloseTest extends CamelTestSupport { @Test public void testABCClose() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); // we expect ABC in the published message // notice: Only 1 message is expected mock.expectedBodiesReceived("ABC"); // send the first message template.sendBodyAndHeader("direct:start", "A", "myId", 1); // send the 2nd message with the same correlation key

template.sendBodyAndHeader("direct:start", "B", "myId", 1); // the F message has another correlation key template.sendBodyAndHeader("direct:start", "F", "myId", 2); // now we have 3 messages with the same correlation key // and the Aggregator should publish the message template.sendBodyAndHeader("direct:start", "C", "myId", 1); // sending with correlation id 1 should fail as its closed try { template.sendBodyAndHeader("direct:start", "A2", "myId", 1); } catch (CamelExecutionException e) { ClosedCorrelationKeyException cause = assertIsInstanceOf(ClosedCorrelationKeyException .class, e.getCause()); assertEquals("1", cause.getCorrelationKey()); } assertMockEndpointsSatisfied(); } @Override

protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") // do a little logging .log("Sending ${body} with correlation key ${header.myId}") // aggregate based on header correlation key // use class MyAggregationStrategy for aggregation // and complete when we have aggregated 3 messages .aggregate(header("myId"), new MyaggregationStat()).completionSize(3) // and close completed correlation keys, and remember back the last // 2000 used keys .closeCorrelationKeyOnCo mpletion(2000) // do a little logging for the published message .log("Sending out ${body}") // and send it to the mock .to("mock:result"); } }; } }

AggregateABCEagerTest
package com; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.test.junit4.CamelTestSupport; import org.junit.Test;

public class AggregateABCEagerTest extends CamelTestSupport { @Test public void testABCEND() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); // we expect ABC in the published message // notice: Only 1 message is expected mock.expectedBodiesReceived("ABC"); // send the first message template.sendBodyAndHeader("direct:start", "A", "myId", 1); // send the 2nd message with the same correlation key template.sendBodyAndHeader("direct:start", "B", "myId", 1); // the F message has another correlation key

template.sendBodyAndHeader("direct:start", "F", "myId", 2); // now we have 3 messages with the same correlation key // and the Aggregator should publish the message template.sendBodyAndHeader("direct:start", "C", "myId", 1); // and now the END message to trigger completion template.sendBodyAndHeader("direct:start", "myId", 1); assertMockEndpointsSatisfied(); } @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") // do a little logging .log("Sending ${body} with correlation key ${header.myId}") // aggregate based on header correlation key // use class MyEndAggregationStrategy for aggregation (special for END) "END",

// and complete when the END message arrives .aggregate(header("myId"), new MyaggregationStat()) // must enable eager check to have the completion predicate // to match when we received END in the arrived message body .completionPredicate(bod y().isEqualTo("END")).eagerCheckCompletion() // do a little logging for the published message .log("Sending out ${body}") // and send it to the mock .to("mock:result"); } }; } }

AggregateABCHawtDBTest

package com;

import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.hawtdb.HawtDBAggregationRepository; import org.apache.camel.test.junit4.CamelTestSupport; import org.junit.Test;

public class AggregateABCHawtDBTest extends CamelTestSupport {

@Test public void testABCHawtDB() throws Exception { System.out.println("Copy 3 files to target/inbox to trigger the completion"); System.out.println("Files to copy:"); System.out.println(" copy src/test/resources/a.txt target/inbox"); System.out.println(" copy src/test/resources/b.txt target/inbox"); System.out.println(" copy src/test/resources/c.txt target/inbox"); System.out.println("\nSleeping for 20 seconds"); System.out.println("You can let the test terminate (or press ctrl +c) and then start it again"); System.out.println("Which should let you be able to resume.");

Thread.sleep(20 * 1000); }

@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { HawtDBAggregationRepository hawtDB = new HawtDBAggregationRepository("myrepo", "data/myrepo.dat");

from("file:target/inbox") // do a little logging when we load the file .log("Consuming file ${file:name}") // just aggregate all messages .aggregate(constant(true), new MyaggregationStat()) // use HawtDB as the persistent repository .aggregationRepository(hawtDB) // and complete when we got 3 messages .completionSize(3) // do a little logging for the published message .log("Sending out ${body}") // and send it to the mock .to("mock:result"); } }; } }

AggregateABCInvalidTest
package com; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.test.junit4.CamelTestSupport; import org.junit.Test; public class AggregateABCInvalidTest extends CamelTestSupport { @Test public void testABCInvalid() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); // we expect ABC in the published message // notice: Only 1 message is expected mock.expectedBodiesReceived("ABC"); // send the first message template.sendBodyAndHeader("direct:start", "A", "myId", 1); // send the 2nd message with the same correlation key template.sendBodyAndHeader("direct:start", "B", "myId", 1);

// the F message has another correlation key template.sendBodyAndHeader("direct:start", "F", "myId", 2); // this message has no correlation key so its ignored template.sendBody("direct:start", "C"); // so we resend the message this time with a correlation key template.sendBodyAndHeader("direct:start", "C", "myId", 1); assertMockEndpointsSatisfied(); } @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") // do a little logging .log("Sending ${body} with correlation key ${header.myId}") // aggregate based on header correlation key // use class MyAggregationStrategy for aggregation // and complete when we have aggregated 3 messages

.aggregate(header("myId"), new MyaggregationStat()).completionSize(3) // and ignore invalid correlation keys .ignoreInvalidCorrelatio nKeys() // do a little logging for the published message .log("Sending out ${body}") // and send it to the mock .to("mock:result"); } }; } }

AggregateABCRecoverTest
package com;

import java.util.concurrent.TimeUnit;

import org.apache.camel.Exchange; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.hawtdb.HawtDBAggregationRepository; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.test.junit4.CamelTestSupport; import org.junit.Test;

public class AggregateABCRecoverTest extends CamelTestSupport {

@Override public void setUp() throws Exception { // ensure we use a fresh repo each time by deleting the data directory deleteDirectory("data"); super.setUp(); }

@Test public void testABCRecover() throws Exception {

// we should never get a result getMockEndpoint("mock:result").expectedMessageCount(0);

MockEndpoint mock = getMockEndpoint("mock:aggregate"); // we should try 1 time + 4 times as redelivery mock.expectedMessageCount(5);

MockEndpoint dead = getMockEndpoint("mock:dead"); // we should send the message to dead letter channel when exhausted dead.expectedBodiesReceived("ABC"); // should be marked as redelivered dead.message(0).header(Exchange.REDELIVERED).isEqualTo(true); // and we did try 4 times to redeliver dead.message(0).header(Exchange.REDELIVERY_COUNTER).isEqualTo(4);

// send the first message template.sendBodyAndHeader("direct:start", "A", "myId", 1); // send the 2nd message with the same correlation key template.sendBodyAndHeader("direct:start", "B", "myId", 1); // now we have 3 messages with the same correlation key // and the Aggregator should publish the message template.sendBodyAndHeader("direct:start", "C", "myId", 1);

// wait for 20 seconds as this test takes some time assertMockEndpointsSatisfied(20, TimeUnit.SECONDS);

@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { HawtDBAggregationRepository hawtDB = new HawtDBAggregationRepository("myrepo", "data/myrepo.dat"); // will recover by default hawtDB.setUseRecovery(true); // try at most 4 times hawtDB.setMaximumRedeliveries(4); // send to mock:dead if exhausted hawtDB.setDeadLetterUri("mock:dead"); // have it retry every 3th second hawtDB.setRecoveryInterval(3000);

from("direct:start") // do a little logging .log("Sending ${body} with correlation key ${header.myId}") // aggregate based on header correlation key // use class MyAggregationStrategy for aggregation // and complete when we have aggregated 3 messages .aggregate(header("myId"), new MyaggregationStat()) // use HawtDB as the persistent repository

.aggregationRepository(hawtDB) // and complete when we got 3 messages .completionSize(3) // do a little logging for the published message .log("Sending out ${body}") // use a mock to check recovery .to("mock:aggregate") // force failure to have the message being recovered .throwException(new IllegalArgumentException("Damn does not work")) // and send it to the mock (not possible, due exception being thrown) .to("mock:result"); } }; } }

AggregateXMLTest
package com;

import org.apache.camel.Exchange; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.test.junit4.CamelTestSupport; import org.junit.Test;

public class AggregateXMLTest extends CamelTestSupport {

@Test public void testXML() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(2);

template.sendBody("direct:start", "<order name=\"motor\" amount=\"1000\" customer=\"honda\"/>"); template.sendBody("direct:start", "<order name=\"motor\" amount=\"500\" customer=\"toyota\"/>"); template.sendBody("direct:start", "<order name=\"gearbox\" amount=\"200\" customer=\"toyota\"/>");

assertMockEndpointsSatisfied(); }

@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .log("Sending ${body}") // aggregate based on xpath expression which extracts from the

// arrived message body. // use class MyAggregationStrategy for aggregation .aggregate(xpath("/order/@customer"), new MyaggregationStat()) // complete either when we have 2 messages or after 5 sec timeout .completionSize(2).completionTimeout(5000) // do a little logging for the published message .log("Completed by ${property.CamelAggregatedCompletedBy} ") .log("Sending out ${body}") // and send it to the mock .to("mock:result"); System.out.println("hav a look .. " + Exchange.AGGREGATED_COMPLETED_BY); } }; } }

TestAgrregators
package com; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.test.CamelTestSupport; public class TestAgrregators extends CamelTestSupport{

public void testABC()throws Exception { MockEndpoint mock=getMockEndpoint("mock:result"); mock.expectedBodiesReceived("ABC","GXS"); template.sendBodyAndHeader("direct:start","A ","id",1); template.sendBodyAndHeader("direct:start","B ","id",1); template.sendBodyAndHeader("direct:start","G ","id",2); template.sendBodyAndHeader("direct:start","C ","id",1); template.sendBodyAndHeader("direct:start","X ","id",2); template.sendBodyAndHeader("direct:start","S ","id",2); assertMockEndpointsSatisfied(); } @Override protected RouteBuilder createRouteBuilder( )throws Exception{ return new RouteBuilder() { @Override public void configure() throws Exception {

from("direct:start") .log("sending Body now...${body} with Key ${header.id}") .aggregate(header("id"),new MyaggregationStat()).completionSize(3) .log("sending Out ${body}") .to("mock:result"); } }; } }

MyaggregationStat
package com;

import org.apache.camel.Exchange; import org.apache.camel.processor.aggregate.AggregationStrategy;

public class MyaggregationStat implements AggregationStrategy {

public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {

if (oldExchange == null) { return newExchange; }

String oldBody = oldExchange.getIn().getBody(String.class); String newBody = newExchange.getIn().getBody(String.class);

// the body should be the two bodies added together String body = oldBody + newBody;

// update the existing message with the added body oldExchange.getIn().setBody(body); // and return it

return oldExchange; } }

Anda mungkin juga menyukai