-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1400 from lburgazzoli/github-1398
Observe Camel's Management events
- Loading branch information
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...re/deployment/src/test/java/org/apache/camel/quarkus/core/runtime/CamelObserversTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...sions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelEventBridge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |