Search in sources :

Example 1 with RouteAddedEvent

use of org.apache.camel.management.event.RouteAddedEvent in project camel by apache.

the class DefaultRuntimeEndpointRegistry method notify.

@Override
public void notify(EventObject event) throws Exception {
    if (event instanceof RouteAddedEvent) {
        RouteAddedEvent rse = (RouteAddedEvent) event;
        Endpoint endpoint = rse.getRoute().getEndpoint();
        String routeId = rse.getRoute().getId();
        // a HashSet is fine for inputs as we only have a limited number of those
        Set<String> uris = new HashSet<String>();
        uris.add(endpoint.getEndpointUri());
        inputs.put(routeId, uris);
        // use a LRUCache for outputs as we could potential have unlimited uris if dynamic routing is in use
        // and therefore need to have the limit in use
        outputs.put(routeId, new LRUCache<String, String>(limit));
    } else if (event instanceof RouteRemovedEvent) {
        RouteRemovedEvent rse = (RouteRemovedEvent) event;
        String routeId = rse.getRoute().getId();
        inputs.remove(routeId);
        outputs.remove(routeId);
        if (extended) {
            String uri = rse.getRoute().getEndpoint().getEndpointUri();
            String key = asUtilizationKey(routeId, uri);
            if (key != null) {
                inputUtilization.remove(key);
            }
        }
    } else if (extended && event instanceof ExchangeCreatedEvent) {
        // we only capture details in extended mode
        ExchangeCreatedEvent ece = (ExchangeCreatedEvent) event;
        Endpoint endpoint = ece.getExchange().getFromEndpoint();
        if (endpoint != null) {
            String routeId = ece.getExchange().getFromRouteId();
            String uri = endpoint.getEndpointUri();
            String key = asUtilizationKey(routeId, uri);
            if (key != null) {
                inputUtilization.onHit(key);
            }
        }
    } else if (event instanceof ExchangeSendingEvent) {
        ExchangeSendingEvent ese = (ExchangeSendingEvent) event;
        Endpoint endpoint = ese.getEndpoint();
        String routeId = getRouteId(ese.getExchange());
        String uri = endpoint.getEndpointUri();
        Map<String, String> uris = outputs.get(routeId);
        if (uris != null && !uris.containsKey(uri)) {
            uris.put(uri, uri);
        }
        if (extended) {
            String key = asUtilizationKey(routeId, uri);
            if (key != null) {
                outputUtilization.onHit(key);
            }
        }
    }
}
Also used : RouteAddedEvent(org.apache.camel.management.event.RouteAddedEvent) ExchangeCreatedEvent(org.apache.camel.management.event.ExchangeCreatedEvent) ExchangeSendingEvent(org.apache.camel.management.event.ExchangeSendingEvent) Endpoint(org.apache.camel.Endpoint) RouteRemovedEvent(org.apache.camel.management.event.RouteRemovedEvent) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with RouteAddedEvent

use of org.apache.camel.management.event.RouteAddedEvent in project camel by apache.

the class FileWatcherReloadStrategyTest method testUpdateXmlRoute.

public void testUpdateXmlRoute() throws Exception {
    deleteDirectory("target/dummy");
    createDirectory("target/dummy");
    // the bar route is added two times, at first, and then when updated
    final CountDownLatch latch = new CountDownLatch(2);
    context.getManagementStrategy().addEventNotifier(new EventNotifierSupport() {

        @Override
        public void notify(EventObject event) throws Exception {
            latch.countDown();
        }

        @Override
        public boolean isEnabled(EventObject event) {
            return event instanceof RouteAddedEvent;
        }
    });
    context.start();
    // there are 0 routes to begin with
    assertEquals(0, context.getRoutes().size());
    Thread.sleep(1000);
    log.info("Copying file to target/dummy");
    // create an xml file with some routes
    FileUtil.copyFile(new File("src/test/resources/org/apache/camel/model/barRoute.xml"), new File("target/dummy/barRoute.xml"));
    // (is slow on osx, so wait up till 20 seconds)
    for (int i = 0; i < 20; i++) {
        if (context.getRoutes().size() > 0) {
            break;
        }
        Thread.sleep(1000);
    }
    assertEquals(1, context.getRoutes().size());
    // and the route should work
    getMockEndpoint("mock:bar").expectedMessageCount(1);
    template.sendBody("direct:bar", "Hello World");
    assertMockEndpointsSatisfied();
    resetMocks();
    // now update the file
    log.info("Updating file in target/dummy");
    // create an xml file with some routes
    FileUtil.copyFile(new File("src/test/resources/org/apache/camel/model/barUpdatedRoute.xml"), new File("target/dummy/barRoute.xml"));
    // wait for that file to be processed and remove/add the route
    // (is slow on osx, so wait up till 20 seconds)
    boolean done = latch.await(20, TimeUnit.SECONDS);
    assertTrue("Should reload file within 20 seconds", done);
    // and the route should work with the update
    Thread.sleep(1000);
    getMockEndpoint("mock:bar").expectedBodiesReceived("Bye Camel");
    template.sendBody("direct:bar", "Camel");
    assertMockEndpointsSatisfied();
}
Also used : RouteAddedEvent(org.apache.camel.management.event.RouteAddedEvent) EventNotifierSupport(org.apache.camel.support.EventNotifierSupport) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File) EventObject(java.util.EventObject)

Example 3 with RouteAddedEvent

use of org.apache.camel.management.event.RouteAddedEvent in project camel by apache.

the class FileWatcherReloadStrategyTest method testUpdateExistingRoute.

public void testUpdateExistingRoute() throws Exception {
    deleteDirectory("target/dummy");
    createDirectory("target/dummy");
    // the bar route is added two times, at first, and then when updated
    final CountDownLatch latch = new CountDownLatch(2);
    context.getManagementStrategy().addEventNotifier(new EventNotifierSupport() {

        @Override
        public void notify(EventObject event) throws Exception {
            latch.countDown();
        }

        @Override
        public boolean isEnabled(EventObject event) {
            return event instanceof RouteAddedEvent;
        }
    });
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:bar").routeId("bar").to("mock:foo");
        }
    });
    context.start();
    assertEquals(1, context.getRoutes().size());
    // and the route should work sending to mock:foo
    getMockEndpoint("mock:bar").expectedMessageCount(0);
    getMockEndpoint("mock:foo").expectedMessageCount(1);
    template.sendBody("direct:bar", "Hello World");
    assertMockEndpointsSatisfied();
    resetMocks();
    Thread.sleep(1000);
    log.info("Copying file to target/dummy");
    // create an xml file with some routes
    FileUtil.copyFile(new File("src/test/resources/org/apache/camel/model/barRoute.xml"), new File("target/dummy/barRoute.xml"));
    // wait for that file to be processed and remove/add the route
    // (is slow on osx, so wait up till 20 seconds)
    boolean done = latch.await(20, TimeUnit.SECONDS);
    assertTrue("Should reload file within 20 seconds", done);
    // and the route should be changed to route to mock:bar instead of mock:foo
    Thread.sleep(1000);
    getMockEndpoint("mock:bar").expectedMessageCount(1);
    getMockEndpoint("mock:foo").expectedMessageCount(0);
    template.sendBody("direct:bar", "Bye World");
    assertMockEndpointsSatisfied();
}
Also used : RouteAddedEvent(org.apache.camel.management.event.RouteAddedEvent) EventNotifierSupport(org.apache.camel.support.EventNotifierSupport) RouteBuilder(org.apache.camel.builder.RouteBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File) EventObject(java.util.EventObject)

Aggregations

RouteAddedEvent (org.apache.camel.management.event.RouteAddedEvent)3 File (java.io.File)2 EventObject (java.util.EventObject)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 EventNotifierSupport (org.apache.camel.support.EventNotifierSupport)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Endpoint (org.apache.camel.Endpoint)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 ExchangeCreatedEvent (org.apache.camel.management.event.ExchangeCreatedEvent)1 ExchangeSendingEvent (org.apache.camel.management.event.ExchangeSendingEvent)1 RouteRemovedEvent (org.apache.camel.management.event.RouteRemovedEvent)1