Search in sources :

Example 6 with ThrottlingInflightRoutePolicy

use of org.apache.camel.impl.ThrottlingInflightRoutePolicy in project camel by apache.

the class BackpressurePublisherRoutePolicyTest method testThatBackpressureCausesTemporaryRouteStop.

@Test
public void testThatBackpressureCausesTemporaryRouteStop() throws Exception {
    CountDownLatch generationLatch = new CountDownLatch(25);
    new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            ThrottlingInflightRoutePolicy policy = new ThrottlingInflightRoutePolicy();
            policy.setMaxInflightExchanges(10);
            policy.setScope(ThrottlingInflightRoutePolicy.ThrottlingScope.Route);
            policy.setResumePercentOfMax(70);
            policy.setLoggingLevel(LoggingLevel.INFO);
            from("timer:tick?period=50&repeatCount=35").id("policy-route").routePolicy(policy).process(x -> generationLatch.countDown()).to("reactive-streams:pub");
        }
    }.addRoutesToCamelContext(context);
    CountDownLatch receptionLatch = new CountDownLatch(35);
    Publisher<Exchange> pub = CamelReactiveStreams.get(context()).fromStream("pub", Exchange.class);
    TestSubscriber<Exchange> subscriber = new TestSubscriber<Exchange>() {

        @Override
        public void onNext(Exchange o) {
            super.onNext(o);
            receptionLatch.countDown();
        }
    };
    subscriber.setInitiallyRequested(10);
    pub.subscribe(subscriber);
    // Add another (fast) subscription that should not affect the backpressure on the route
    Observable.fromPublisher(pub).subscribe();
    context.start();
    // after 25 messages are generated
    generationLatch.await(5, TimeUnit.SECONDS);
    // The number of exchanges should be 10 (requested by the subscriber), so 35-10=25
    assertEquals(25, receptionLatch.getCount());
    // fire a delayed request from the subscriber (required by camel core)
    subscriber.request(1);
    Thread.sleep(250);
    StatefulService service = (StatefulService) context().getRoute("policy-route").getConsumer();
    // ensure the route is stopped or suspended
    assertTrue(service.isStopped() || service.isSuspended());
    // request all the remaining exchanges
    subscriber.request(24);
    assertTrue(receptionLatch.await(5, TimeUnit.SECONDS));
// The reception latch has gone to 0
}
Also used : Exchange(org.apache.camel.Exchange) ThrottlingInflightRoutePolicy(org.apache.camel.impl.ThrottlingInflightRoutePolicy) RouteBuilder(org.apache.camel.builder.RouteBuilder) TestSubscriber(org.apache.camel.component.reactive.streams.support.TestSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) StatefulService(org.apache.camel.StatefulService) Test(org.junit.Test)

Example 7 with ThrottlingInflightRoutePolicy

use of org.apache.camel.impl.ThrottlingInflightRoutePolicy in project camel by apache.

the class JmsThrottlingInflightRoutePolicyTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            ThrottlingInflightRoutePolicy policy = new ThrottlingInflightRoutePolicy();
            policy.setMaxInflightExchanges(10);
            policy.setResumePercentOfMax(50);
            policy.setScope(ThrottlingInflightRoutePolicy.ThrottlingScope.Route);
            from("activemq:queue:foo?concurrentConsumers=20").routePolicy(policy).delay(100).to("log:foo?groupSize=10").to("mock:result");
        }
    };
}
Also used : ThrottlingInflightRoutePolicy(org.apache.camel.impl.ThrottlingInflightRoutePolicy) RouteBuilder(org.apache.camel.builder.RouteBuilder)

Aggregations

ThrottlingInflightRoutePolicy (org.apache.camel.impl.ThrottlingInflightRoutePolicy)7 RouteBuilder (org.apache.camel.builder.RouteBuilder)4 CountDownLatch (java.util.concurrent.CountDownLatch)2 Exchange (org.apache.camel.Exchange)2 StatefulService (org.apache.camel.StatefulService)2 TestSubscriber (org.apache.camel.component.reactive.streams.support.TestSubscriber)2 Test (org.junit.Test)2 Channel (org.apache.camel.Channel)1 Consumer (org.apache.camel.Consumer)1 NonManagedService (org.apache.camel.NonManagedService)1 Processor (org.apache.camel.Processor)1 Producer (org.apache.camel.Producer)1 RuntimeCamelCatalog (org.apache.camel.catalog.RuntimeCamelCatalog)1 ConsumerCache (org.apache.camel.impl.ConsumerCache)1 DefaultEndpointRegistry (org.apache.camel.impl.DefaultEndpointRegistry)1 DefaultTransformerRegistry (org.apache.camel.impl.DefaultTransformerRegistry)1 DefaultValidatorRegistry (org.apache.camel.impl.DefaultValidatorRegistry)1 ProducerCache (org.apache.camel.impl.ProducerCache)1 ThrottlingExceptionRoutePolicy (org.apache.camel.impl.ThrottlingExceptionRoutePolicy)1 ManagedAsyncProcessorAwaitManager (org.apache.camel.management.mbean.ManagedAsyncProcessorAwaitManager)1