Skip to content

Commit

Permalink
Observe Camel's Lifecycle events apache#1399
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Jun 22, 2020
1 parent 6f08473 commit 39b4e8f
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,18 @@

import io.quarkus.test.QuarkusUnitTest;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.direct.DirectComponent;
import org.apache.camel.impl.event.RouteStartedEvent;
import org.apache.camel.quarkus.core.events.ComponentAddEvent;
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));
Expand All @@ -50,29 +48,43 @@ public class CamelObserversTest {
@Test
public void testObservers() {
await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> {
assertThat(handler.routes()).contains("myRoute");
assertThat(handler.routes())
.contains(MyRoutes.ROUTE_ID);
assertThat(handler.components())
.contains(DirectComponent.class.getName());
});
}

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

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

public void onComponentAdd(@Observes ComponentAddEvent event) {
components.add(event.getComponent().getClass().getName());
}

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

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

@ApplicationScoped
public static class MyRoutes extends RouteBuilder {
public static String ROUTE_ID = "myRoute";

@Override
public void configure() throws Exception {
from("direct:start")
.routeId("myRoute")
.routeId(ROUTE_ID)
.log("${body}");
}
}
Expand Down
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.addLifecycleStrategy(new CamelLifecycleEventBridge());
context.getManagementStrategy().addEventNotifier(new CamelEventBridge());

// register to the container
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.Component;
import org.apache.camel.Endpoint;
import org.apache.camel.quarkus.core.events.ComponentAddEvent;
import org.apache.camel.quarkus.core.events.ComponentRemoveEvent;
import org.apache.camel.quarkus.core.events.EndpointAddEvent;
import org.apache.camel.quarkus.core.events.EndpointRemoveEvent;
import org.apache.camel.spi.CamelEvent;
import org.apache.camel.support.LifecycleStrategySupport;

public class CamelLifecycleEventBridge extends LifecycleStrategySupport {
@Override
public void onComponentAdd(String name, Component component) {

fireEvent(new ComponentAddEvent(component));
}

@Override
public void onComponentRemove(String name, Component component) {
fireEvent(new ComponentRemoveEvent(component));
}

@Override
public void onEndpointAdd(Endpoint endpoint) {
fireEvent(new EndpointAddEvent(endpoint));
}

@Override
public void onEndpointRemove(Endpoint endpoint) {
fireEvent(new EndpointRemoveEvent(endpoint));
}

private static <T extends CamelEvent> void fireEvent(T event) {
fireEvent(CamelEvent.class, event);
}

private static <T> void fireEvent(Class<T> clazz, T event) {
ArcContainer container = Arc.container();
if (container != null) {
BeanManager beanManager = container.beanManager();
if (beanManager != null) {
beanManager.getEvent().select(clazz).fire(event);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.events;

import org.apache.camel.Component;

public class ComponentAddEvent extends ComponentEvent {
public ComponentAddEvent(Component component) {
super(component);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.events;

import org.apache.camel.CamelContext;
import org.apache.camel.Component;
import org.apache.camel.spi.CamelEvent;

public class ComponentEvent implements CamelEvent.CamelContextEvent {
private final Component component;

public ComponentEvent(Component component) {
this.component = component;
}

@Override
public CamelContext getContext() {
return this.component.getCamelContext();
}

public Component getComponent() {
return this.component;
}

public <T extends Component> T getComponent(Class<T> type) {
return type.cast(this.component);
}

@Override
public Type getType() {
return Type.Custom;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.events;

import org.apache.camel.Component;

public class ComponentRemoveEvent extends ComponentEvent {
public ComponentRemoveEvent(Component component) {
super(component);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.events;

import org.apache.camel.Endpoint;

public class EndpointAddEvent extends EndpointEvent {
public EndpointAddEvent(Endpoint endpoint) {
super(endpoint);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.events;

import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.spi.CamelEvent;

public class EndpointEvent implements CamelEvent.CamelContextEvent {
private final Endpoint endpoint;

public EndpointEvent(Endpoint endpoint) {
this.endpoint = endpoint;
}

@Override
public CamelContext getContext() {
return this.endpoint.getCamelContext();
}

public Endpoint getEndpoint() {
return this.endpoint;
}

public <T extends Endpoint> T getEndpoint(Class<T> type) {
return type.cast(this.endpoint);
}

@Override
public Type getType() {
return Type.Custom;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.events;

import org.apache.camel.Endpoint;

public class EndpointRemoveEvent extends EndpointEvent {
public EndpointRemoveEvent(Endpoint endpoint) {
super(endpoint);
}
}

0 comments on commit 39b4e8f

Please sign in to comment.