Search in sources :

Example 11 with EventNotifierSupport

use of org.apache.camel.support.EventNotifierSupport in project camel by apache.

the class EventNotifierExchangeCompletedTest method createCamelContext.

@Override
protected CamelContext createCamelContext() throws Exception {
    DefaultCamelContext context = new DefaultCamelContext(createRegistry());
    context.getManagementStrategy().addEventNotifier(new EventNotifierSupport() {

        public void notify(EventObject event) throws Exception {
            events.add(event);
        }

        public boolean isEnabled(EventObject event) {
            // we only want the completed event
            return event instanceof ExchangeCompletedEvent;
        // you can add additional filtering such as the exchange
        // should be from a specific endpoint or route
        // just return true for the events you like
        }

        protected void doStart() throws Exception {
        // noop
        }

        protected void doStop() throws Exception {
        // noop
        }
    });
    return context;
}
Also used : EventNotifierSupport(org.apache.camel.support.EventNotifierSupport) ExchangeCompletedEvent(org.apache.camel.management.event.ExchangeCompletedEvent) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) EventObject(java.util.EventObject)

Example 12 with EventNotifierSupport

use of org.apache.camel.support.EventNotifierSupport 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 13 with EventNotifierSupport

use of org.apache.camel.support.EventNotifierSupport 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)

Example 14 with EventNotifierSupport

use of org.apache.camel.support.EventNotifierSupport in project camel by apache.

the class RemoveEventNotifierTest method createCamelContext.

@Override
protected CamelContext createCamelContext() throws Exception {
    DefaultCamelContext context = new DefaultCamelContext(createRegistry());
    notifier = new EventNotifierSupport() {

        public void notify(EventObject event) throws Exception {
            events.add(event);
        }

        public boolean isEnabled(EventObject event) {
            return true;
        }

        @Override
        protected void doStart() throws Exception {
        }

        @Override
        protected void doStop() throws Exception {
        }
    };
    context.getManagementStrategy().addEventNotifier(notifier);
    return context;
}
Also used : EventNotifierSupport(org.apache.camel.support.EventNotifierSupport) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) EventObject(java.util.EventObject)

Example 15 with EventNotifierSupport

use of org.apache.camel.support.EventNotifierSupport in project camel by apache.

the class AsyncEndpointEventNotifierSendingTest method createCamelContext.

@Override
protected CamelContext createCamelContext() throws Exception {
    DefaultCamelContext context = new DefaultCamelContext(createRegistry());
    context.getManagementStrategy().addEventNotifier(new EventNotifierSupport() {

        public void notify(EventObject event) throws Exception {
            events.add(event);
        }

        public boolean isEnabled(EventObject event) {
            return event instanceof ExchangeSendingEvent || event instanceof ExchangeSentEvent;
        }

        @Override
        protected void doStart() throws Exception {
        }

        @Override
        protected void doStop() throws Exception {
        }
    });
    return context;
}
Also used : ExchangeSentEvent(org.apache.camel.management.event.ExchangeSentEvent) EventNotifierSupport(org.apache.camel.support.EventNotifierSupport) ExchangeSendingEvent(org.apache.camel.management.event.ExchangeSendingEvent) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) EventObject(java.util.EventObject)

Aggregations

EventObject (java.util.EventObject)15 EventNotifierSupport (org.apache.camel.support.EventNotifierSupport)15 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)10 File (java.io.File)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExchangeCompletedEvent (org.apache.camel.management.event.ExchangeCompletedEvent)2 ExchangeSentEvent (org.apache.camel.management.event.ExchangeSentEvent)2 RouteAddedEvent (org.apache.camel.management.event.RouteAddedEvent)2 CamelContext (org.apache.camel.CamelContext)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 ExchangeSendingEvent (org.apache.camel.management.event.ExchangeSendingEvent)1