Author: davsclaus
Date: Fri Jun 25 09:44:14 2010
New Revision: 957866
URL: http://svn.apache.org/viewvc?rev=957866&view=rev
Log:
CAMEL-2855: Polished StartupListener a bit.
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
camel/trunk/camel-core/src/main/java/org/apache/camel/StartupListener.java
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StartupListenerComponentFromRegistryTest.java
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StartupListenerComponentTest.java
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StartupListenerTest.java
camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java?rev=957866&r1=957865&r2=957866&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java Fri
Jun 25 09:44:14 2010
@@ -106,10 +106,14 @@ public interface CamelContext extends Se
* Adds the given listener to be invoked when {...@link CamelContext} have
just been started.
* <p/>
* This allows listeners to do any custom work after the routes and other
services have been started and are running.
+ * <p/><b>Important:</b> The listener will always be invoked, also if the
{...@link CamelContext} has already been
+ * started, see the {...@link
org.apache.camel.StartupListener#onCamelContextStarted(CamelContext, boolean)}
method.
*
* @param listener the listener
+ * @throws Exception can be thrown if {...@link CamelContext} is already
started and the listener is invoked
+ * and cause an exception to be thrown
*/
- void addStartupListener(StartupListener listener);
+ void addStartupListener(StartupListener listener) throws Exception;
// Component Management Methods
//-----------------------------------------------------------------------
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/StartupListener.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/StartupListener.java?rev=957866&r1=957865&r2=957866&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/StartupListener.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/StartupListener.java
Fri Jun 25 09:44:14 2010
@@ -33,9 +33,12 @@ public interface StartupListener {
/**
* Callback invoked when the {...@link org.apache.camel.CamelContext} has
just been started.
*
- * @param context the camel context
+ * @param context the Camel context
+ * @param alreadyStarted whether or not the Camel context already has been
started. For example the Camel context
+ * could already have been started, and then a
service is added/started later which still
+ * triggers this callback to be invoked.
* @throws Exception can be thrown in case of errors to fail the startup
process and have the application
- * fail on startup.
+ * fail on startup.
*/
- void onCamelContextStarted(CamelContext context) throws Exception;
+ void onCamelContextStarted(CamelContext context, boolean alreadyStarted)
throws Exception;
}
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=957866&r1=957865&r2=957866&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
Fri Jun 25 09:44:14 2010
@@ -615,13 +615,20 @@ public class DefaultCamelContext extends
public boolean hasService(Object object) {
if (object instanceof Service) {
- return servicesToClose.contains(object);
+ Service service = (Service) object;
+ return servicesToClose.contains(service);
}
return false;
}
- public void addStartupListener(StartupListener listener) {
- startupListeners.add(listener);
+ public void addStartupListener(StartupListener listener) throws Exception {
+ // either add to listener so we can invoke then later when
CamelContext has been started
+ // or invoke the callback right now
+ if (isStarted()) {
+ listener.onCamelContextStarted(this, true);
+ } else {
+ startupListeners.add(listener);
+ }
}
// Helper methods
@@ -949,8 +956,10 @@ public class DefaultCamelContext extends
// now notify any startup aware listeners as all the routes etc has
been started.
for (StartupListener startup : startupListeners) {
- startup.onCamelContextStarted(this);
+ startup.onCamelContextStarted(this, false);
}
+ // and then get rid of the list as we dont need it anymore
+ startupListeners.clear();
stopWatch.stop();
if (LOG.isInfoEnabled()) {
@@ -1157,7 +1166,8 @@ public class DefaultCamelContext extends
// and register startup aware so they can be notified when
// camel context has been started
if (service instanceof StartupListener) {
- startupListeners.add((StartupListener) service);
+ StartupListener listener = (StartupListener) service;
+ addStartupListener(listener);
}
// and then start the service
Modified:
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StartupListenerComponentFromRegistryTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StartupListenerComponentFromRegistryTest.java?rev=957866&r1=957865&r2=957866&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StartupListenerComponentFromRegistryTest.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StartupListenerComponentFromRegistryTest.java
Fri Jun 25 09:44:14 2010
@@ -52,7 +52,7 @@ public class StartupListenerComponentFro
private int invoked;
- public void onCamelContextStarted(CamelContext context) throws
Exception {
+ public void onCamelContextStarted(CamelContext context, boolean
alreadyStarted) throws Exception {
invoked++;
// the routes should have been started
Modified:
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StartupListenerComponentTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StartupListenerComponentTest.java?rev=957866&r1=957865&r2=957866&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StartupListenerComponentTest.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StartupListenerComponentTest.java
Fri Jun 25 09:44:14 2010
@@ -43,7 +43,7 @@ public class StartupListenerComponentTes
private int invoked;
- public void onCamelContextStarted(CamelContext context) throws
Exception {
+ public void onCamelContextStarted(CamelContext context, boolean
alreadyStarted) throws Exception {
invoked++;
// the routes should have been started
Modified:
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StartupListenerTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StartupListenerTest.java?rev=957866&r1=957865&r2=957866&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StartupListenerTest.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StartupListenerTest.java
Fri Jun 25 09:44:14 2010
@@ -38,9 +38,11 @@ public class StartupListenerTest extends
private class MyStartupListener implements StartupListener {
private int invoked;
+ private boolean alreadyStarted;
- public void onCamelContextStarted(CamelContext context) throws
Exception {
+ public void onCamelContextStarted(CamelContext context, boolean
alreadyStarted) throws Exception {
invoked++;
+ this.alreadyStarted = alreadyStarted;
// the route should have been started
assertTrue(context.getRouteStatus("foo").isStarted());
@@ -49,6 +51,10 @@ public class StartupListenerTest extends
public int getInvoked() {
return invoked;
}
+
+ public boolean isAlreadyStarted() {
+ return alreadyStarted;
+ }
}
public void testStartupListenerComponent() throws Exception {
@@ -59,6 +65,21 @@ public class StartupListenerTest extends
assertMockEndpointsSatisfied();
assertEquals(1, my.getInvoked());
+ assertFalse(my.isAlreadyStarted());
+ }
+
+ public void testStartupListenerComponentAlreadyStarted() throws Exception {
+ MyStartupListener other = new MyStartupListener();
+ context.addStartupListener(other);
+
+ getMockEndpoint("mock:result").expectedMessageCount(1);
+
+ template.sendBody("direct:foo", "Hello World");
+
+ assertMockEndpointsSatisfied();
+
+ assertEquals(1, other.getInvoked());
+ assertTrue(other.isAlreadyStarted());
}
@Override
Modified:
camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java?rev=957866&r1=957865&r2=957866&view=diff
==============================================================================
---
camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java
(original)
+++
camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java
Fri Jun 25 09:44:14 2010
@@ -124,7 +124,7 @@ public class QuartzComponent extends Def
return cron;
}
- public void onCamelContextStarted(CamelContext camelContext) throws
Exception {
+ public void onCamelContextStarted(CamelContext camelContext, boolean
alreadyStarted) throws Exception {
// if not configure to auto start then don't start it
if (!isAutoStartScheduler()) {
LOG.info("QuartzComponent configured to not auto start Quartz
scheduler.");
@@ -228,7 +228,7 @@ public class QuartzComponent extends Def
* @throws SchedulerException can be thrown if error starting
*/
public void startScheduler() throws SchedulerException {
- if (!scheduler.isStarted()) {
+ if (scheduler != null && !scheduler.isStarted()) {
if (getStartDelayedSeconds() > 0) {
LOG.info("Starting Quartz scheduler: " +
scheduler.getSchedulerName() + " delayed: " + getStartDelayedSeconds() + "
seconds.");
scheduler.startDelayed(getStartDelayedSeconds());