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 2019/10/30 10:05:50 UTC

[camel] 01/01: CAMEL-13914: Add build phase to service lifecycle in Camel which can be leveraged by camel-quarkus and others where we can prepare for special build-time optimizations.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch doBuild
in repository https://gitbox.apache.org/repos/asf/camel.git

commit b89b4fbacbfbd96d6c2a45e6a895980499d9fa34
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Oct 30 11:05:28 2019 +0100

    CAMEL-13914: Add build phase to service lifecycle in Camel which can be leveraged by camel-quarkus and others where we can prepare for special build-time optimizations.
---
 .../src/main/java/org/apache/camel/Service.java    |  9 ++++
 .../camel/support/service/ServiceSupport.java      | 53 +++++++++++++++++-----
 .../apache/camel/support/ServiceSupportTest.java   | 22 +++++++++
 3 files changed, 72 insertions(+), 12 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/Service.java b/core/camel-api/src/main/java/org/apache/camel/Service.java
index f58f4bb..f98762f 100644
--- a/core/camel-api/src/main/java/org/apache/camel/Service.java
+++ b/core/camel-api/src/main/java/org/apache/camel/Service.java
@@ -24,6 +24,15 @@ import java.io.IOException;
 public interface Service extends AutoCloseable {
 
     /**
+     * Optional build phase which is executed by frameworks that supports pre-building projects (pre-compile)
+     * which allows special optimizations such as camel-quarkus.
+     *
+     * @throws RuntimeCamelException is thrown if build failed
+     */
+    default void build() {
+    }
+
+    /**
      * Initialize the service
      *
      * @throws RuntimeCamelException is thrown if initialization failed
diff --git a/core/camel-api/src/main/java/org/apache/camel/support/service/ServiceSupport.java b/core/camel-api/src/main/java/org/apache/camel/support/service/ServiceSupport.java
index 23a1e68..be5db4f 100644
--- a/core/camel-api/src/main/java/org/apache/camel/support/service/ServiceSupport.java
+++ b/core/camel-api/src/main/java/org/apache/camel/support/service/ServiceSupport.java
@@ -37,26 +37,44 @@ import org.slf4j.LoggerFactory;
 public abstract class ServiceSupport implements StatefulService {
 
     protected static final int NEW = 0;
-    protected static final int INITIALIZED = 1;
-    protected static final int STARTING = 2;
-    protected static final int STARTED = 3;
-    protected static final int SUSPENDING = 4;
-    protected static final int SUSPENDED = 5;
-    protected static final int STOPPING = 6;
-    protected static final int STOPPED = 7;
-    protected static final int SHUTTINGDOWN = 8;
-    protected static final int SHUTDOWN = 9;
-    protected static final int FAILED = 10;
+    protected static final int BUILDED = 1;
+    protected static final int INITIALIZED = 2;
+    protected static final int STARTING = 3;
+    protected static final int STARTED = 4;
+    protected static final int SUSPENDING = 5;
+    protected static final int SUSPENDED = 6;
+    protected static final int STOPPING = 7;
+    protected static final int STOPPED = 8;
+    protected static final int SHUTTINGDOWN = 9;
+    protected static final int SHUTDOWN = 10;
+    protected static final int FAILED = 11;
 
     protected final Logger log = LoggerFactory.getLogger(getClass());
     protected final Object lock = new Object();
     protected volatile int status = NEW;
 
     @Override
-    public void init() {
+    public void build() {
         if (status == NEW) {
             synchronized (lock) {
                 if (status == NEW) {
+                    log.trace("Building service: {}", this);
+                    try {
+                        doBuild();
+                    } catch (Exception e) {
+                        throw RuntimeCamelException.wrapRuntimeException(e);
+                    }
+                    status = BUILDED;
+                }
+            }
+        }
+    }
+
+    @Override
+    public void init() {
+        if (status <= BUILDED) {
+            synchronized (lock) {
+                if (status <= BUILDED) {
                     log.trace("Initializing service: {}", this);
                     try {
                         doInit();
@@ -261,6 +279,10 @@ public abstract class ServiceSupport implements StatefulService {
         return status == NEW;
     }
 
+    public boolean isBuild() {
+        return status == BUILDED;
+    }
+
     public boolean isInit() {
         return status == INITIALIZED;
     }
@@ -282,7 +304,7 @@ public abstract class ServiceSupport implements StatefulService {
 
     @Override
     public boolean isStopped() {
-        return status == NEW || status == INITIALIZED || status == STOPPED || status == SHUTTINGDOWN || status == SHUTDOWN || status == FAILED;
+        return status == NEW || status == INITIALIZED || status == BUILDED || status == STOPPED || status == SHUTTINGDOWN || status == SHUTDOWN || status == FAILED;
     }
 
     @Override
@@ -326,6 +348,13 @@ public abstract class ServiceSupport implements StatefulService {
     }
 
     /**
+     * Optional build phase of the service.
+     * This method will only be called by frameworks which supports pre-building projects such as camel-quarkus.
+     */
+    protected void doBuild() throws Exception {
+    }
+
+    /**
      * Initialize the service.
      * This method will only be called once before starting.
      */
diff --git a/core/camel-core/src/test/java/org/apache/camel/support/ServiceSupportTest.java b/core/camel-core/src/test/java/org/apache/camel/support/ServiceSupportTest.java
index 3ac0d33..c1655cd 100644
--- a/core/camel-core/src/test/java/org/apache/camel/support/ServiceSupportTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/support/ServiceSupportTest.java
@@ -132,6 +132,28 @@ public class ServiceSupportTest extends TestSupport {
         }
     }
 
+    @Test
+    public void testServiceBuild() throws Exception {
+        MyService service = new MyService();
+        assertTrue(service.isNew());
+        service.build();
+        assertTrue(service.isBuild());
+        assertFalse(service.isInit());
+        service.start();
+
+        assertEquals(true, service.isStarted());
+        assertEquals(false, service.isStarting());
+        assertEquals(false, service.isStopped());
+        assertEquals(false, service.isStopping());
+
+        service.stop();
+
+        assertEquals(true, service.isStopped());
+        assertEquals(false, service.isStopping());
+        assertEquals(false, service.isStarted());
+        assertEquals(false, service.isStarting());
+    }
+
     public static class ServiceSupportTestExOnStart extends ServiceSupport {
 
         public ServiceSupportTestExOnStart() {