Skip to content

Commit

Permalink
Merge pull request #1400 from lburgazzoli/github-1398
Browse files Browse the repository at this point in the history
Observe Camel's Management events
  • Loading branch information
oscerd committed Jun 22, 2020
2 parents c492548 + 5e418ad commit 6f08473
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
5 changes: 5 additions & 0 deletions extensions-core/core/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.core.runtime;

import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.TimeUnit;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;

import io.quarkus.test.QuarkusUnitTest;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.event.RouteStartedEvent;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

public class CamelObserversTest {
private static final Logger LOGGER = LoggerFactory.getLogger(CamelObserversTest.class);

@RegisterExtension
static final QuarkusUnitTest CONFIG = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));

@Inject
EventHandler handler;

@Test
public void testObservers() {
await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> {
assertThat(handler.routes()).contains("myRoute");
});
}

@ApplicationScoped
public static class EventHandler {
private final Set<String> routes = new CopyOnWriteArraySet<>();

public void onRouteStarted(@Observes RouteStartedEvent event) {
routes.add(event.getRoute().getRouteId());
}

public Set<String> routes() {
return routes;
}
}

@ApplicationScoped
public static class MyRoutes extends RouteBuilder {
@Override
public void configure() throws Exception {
from("direct:start")
.routeId("myRoute")
.log("${body}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public RuntimeValue<CamelContext> createContext(
context.setLoadTypeConverters(false);
context.setModelJAXBContextFactory(contextFactory.getValue());
context.build();
context.getManagementStrategy().addEventNotifier(new CamelEventBridge());

// register to the container
beanContainer.instance(CamelProducers.class).setContext(context);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.core;

import javax.enterprise.inject.spi.BeanManager;

import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import org.apache.camel.spi.CamelEvent;
import org.apache.camel.support.EventNotifierSupport;

public class CamelEventBridge extends EventNotifierSupport {
@Override
public void notify(CamelEvent event) throws Exception {
ArcContainer container = Arc.container();
if (container == null) {
return;
}

BeanManager manager = container.beanManager();
if (manager != null) {
manager.fireEvent(event);
}
}

@Override
public boolean isEnabled(CamelEvent event) {
return true;
}
}

0 comments on commit 6f08473

Please sign in to comment.