You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2015/03/25 11:23:48 UTC
[5/6] camel git commit: CAMEL-6269: Added MainListener to make it
easier to do custom logic for the lifecycle of start|stop of the main
instance.
CAMEL-6269: Added MainListener to make it easier to do custom logic for the lifecycle of start|stop of the main instance.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/01567ab6
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/01567ab6
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/01567ab6
Branch: refs/heads/master
Commit: 01567ab644034f30c77dd10e742c23f792cdcaac
Parents: 83c56bb
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Mar 25 11:21:56 2015 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Mar 25 11:25:11 2015 +0100
----------------------------------------------------------------------
.../org/apache/camel/main/MainListener.java | 51 ++++++++++++++++++
.../apache/camel/main/MainListenerSupport.java | 39 ++++++++++++++
.../java/org/apache/camel/main/MainSupport.java | 55 +++++++++++++++++++-
.../java/org/apache/camel/main/MainExample.java | 15 ++++++
4 files changed, 158 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/01567ab6/camel-core/src/main/java/org/apache/camel/main/MainListener.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/main/MainListener.java b/camel-core/src/main/java/org/apache/camel/main/MainListener.java
new file mode 100644
index 0000000..3de7668
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/main/MainListener.java
@@ -0,0 +1,51 @@
+/**
+ * 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.main;
+
+/**
+ * A lifecycle listener to receive callbacks when the main is started and stopped.
+ */
+public interface MainListener {
+
+ /**
+ * Callback before the CamelContext(s) is being started.
+ *
+ * @param main the main instance
+ */
+ void beforeStart(MainSupport main);
+
+ /**
+ * Callback after the CamelContext(s) has been started.
+ *
+ * @param main the main instance
+ */
+ void afterStart(MainSupport main);
+
+ /**
+ * Callback before the CamelContext(s) is being stopped.
+ *
+ * @param main the main instance
+ */
+ void beforeStop(MainSupport main);
+
+ /**
+ * Callback after the CamelContext(s) has been stopped.
+ *
+ * @param main the main instance
+ */
+ void afterStop(MainSupport main);
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/01567ab6/camel-core/src/main/java/org/apache/camel/main/MainListenerSupport.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/main/MainListenerSupport.java b/camel-core/src/main/java/org/apache/camel/main/MainListenerSupport.java
new file mode 100644
index 0000000..6dda55b
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/main/MainListenerSupport.java
@@ -0,0 +1,39 @@
+/**
+ * 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.main;
+
+/**
+ * A useful base class for {@link org.apache.camel.main.MainListener} implementations.
+ */
+public class MainListenerSupport implements MainListener {
+
+ public void beforeStart(MainSupport main) {
+ // noop
+ }
+
+ public void afterStart(MainSupport main) {
+ // noop
+ }
+
+ public void beforeStop(MainSupport main) {
+ // noop
+ }
+
+ public void afterStop(MainSupport main) {
+ // noop
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/01567ab6/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/main/MainSupport.java b/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
index c328df8..726e0d6 100644
--- a/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
+++ b/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
@@ -46,6 +46,7 @@ import org.slf4j.LoggerFactory;
*/
public abstract class MainSupport extends ServiceSupport {
protected static final Logger LOG = LoggerFactory.getLogger(MainSupport.class);
+ protected final List<MainListener> listeners = new ArrayList<MainListener>();
protected final List<Option> options = new ArrayList<Option>();
protected final CountDownLatch latch = new CountDownLatch(1);
protected final AtomicBoolean completed = new AtomicBoolean(false);
@@ -119,6 +120,7 @@ public abstract class MainSupport extends ServiceSupport {
public void run() throws Exception {
if (!completed.get()) {
// if we have an issue starting then propagate the exception to caller
+ beforeStart();
start();
try {
afterStart();
@@ -126,6 +128,7 @@ public abstract class MainSupport extends ServiceSupport {
internalBeforeStop();
beforeStop();
stop();
+ afterStop();
} catch (Exception e) {
// however while running then just log errors
LOG.error("Failed: " + e, e);
@@ -143,19 +146,67 @@ public abstract class MainSupport extends ServiceSupport {
}
/**
+ * Adds a {@link org.apache.camel.main.MainListener} to receive callbacks when the main is started or stopping
+ *
+ * @param listener the listener
+ */
+ public void addMainListener(MainListener listener) {
+ listeners.add(listener);
+ }
+
+ /**
+ * Removes the {@link org.apache.camel.main.MainListener}
+ *
+ * @param listener the listener
+ */
+ public void removeMainListener(MainListener listener) {
+ listeners.remove(listener);
+ }
+
+ /**
+ * Callback to run custom logic before CamelContext is being started.
+ * <p/>
+ * It is recommended to use {@link org.apache.camel.main.MainListener} instead.
+ */
+ protected void beforeStart() throws Exception {
+ for (MainListener listener : listeners) {
+ listener.beforeStart(this);
+ }
+ }
+
+ /**
* Callback to run custom logic after CamelContext has been started.
+ * <p/>
+ * It is recommended to use {@link org.apache.camel.main.MainListener} instead.
*/
protected void afterStart() throws Exception {
- // noop
+ for (MainListener listener : listeners) {
+ listener.afterStart(this);
+ }
}
/**
* Callback to run custom logic before CamelContext is being stopped.
+ * <p/>
+ * It is recommended to use {@link org.apache.camel.main.MainListener} instead.
*/
protected void beforeStop() throws Exception {
- // noop
+ for (MainListener listener : listeners) {
+ listener.beforeStop(this);
+ }
}
+ /**
+ * Callback to run custom logic after CamelContext has been stopped.
+ * <p/>
+ * It is recommended to use {@link org.apache.camel.main.MainListener} instead.
+ */
+ protected void afterStop() throws Exception {
+ for (MainListener listener : listeners) {
+ listener.afterStop(this);
+ }
+ }
+
private void internalBeforeStop() {
try {
if (camelTemplate != null) {
http://git-wip-us.apache.org/repos/asf/camel/blob/01567ab6/camel-core/src/test/java/org/apache/camel/main/MainExample.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/main/MainExample.java b/camel-core/src/test/java/org/apache/camel/main/MainExample.java
index 8e30738..df64578 100644
--- a/camel-core/src/test/java/org/apache/camel/main/MainExample.java
+++ b/camel-core/src/test/java/org/apache/camel/main/MainExample.java
@@ -44,6 +44,8 @@ public class MainExample {
main.bind("foo", new MyBean());
// add routes
main.addRouteBuilder(new MyRouteBuilder());
+ // add event listener
+ main.addMainListener(new Events());
// run until you terminate the JVM
System.out.println("Starting Camel. Use ctrl + c to terminate the JVM.\n");
@@ -68,5 +70,18 @@ public class MainExample {
System.out.println("MyBean.calleMe method has been called");
}
}
+
+ public static class Events extends MainListenerSupport {
+
+ @Override
+ public void afterStart(MainSupport main) {
+ System.out.println("MainExample with Camel is now started!");
+ }
+
+ @Override
+ public void beforeStop(MainSupport main) {
+ System.out.println("MainExample with Camel is now being stopped!");
+ }
+ }
}
// END SNIPPET: e1