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 2020/10/08 16:56:14 UTC

[camel] branch master updated: main: provide a minimal Main implementation tailored for embedding it (#4398)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new bdbe15b  main: provide a minimal Main implementation tailored for embedding it (#4398)
bdbe15b is described below

commit bdbe15b5ba52fa984fb6fd2cd69f2b1fc1a0c93a
Author: Luca Burgazzoli <lb...@users.noreply.github.com>
AuthorDate: Thu Oct 8 18:56:02 2020 +0200

    main: provide a minimal Main implementation tailored for embedding it (#4398)
---
 .../java/org/apache/camel/main/SimpleMain.java     |  64 ++++++++++++
 .../java/org/apache/camel/main/SimpleMainTest.java | 107 +++++++++++++++++++++
 2 files changed, 171 insertions(+)

diff --git a/core/camel-main/src/main/java/org/apache/camel/main/SimpleMain.java b/core/camel-main/src/main/java/org/apache/camel/main/SimpleMain.java
new file mode 100644
index 0000000..1023eea
--- /dev/null
+++ b/core/camel-main/src/main/java/org/apache/camel/main/SimpleMain.java
@@ -0,0 +1,64 @@
+/*
+ * 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;
+
+import org.apache.camel.CamelContext;
+
+/**
+ * A minimal Main class for booting Camel.
+ */
+public class SimpleMain extends BaseMainSupport {
+    public SimpleMain(CamelContext camelContext) {
+        super(camelContext);
+    }
+
+    @Override
+    protected void doInit() throws Exception {
+        super.doInit();
+        postProcessCamelContext(camelContext);
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        for (MainListener listener : listeners) {
+            listener.beforeStart(this);
+        }
+
+        super.doStart();
+
+        getCamelContext().start();
+
+        for (MainListener listener : listeners) {
+            listener.afterStart(this);
+        }
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        for (MainListener listener : listeners) {
+            listener.beforeStop(this);
+        }
+
+        super.doStart();
+
+        getCamelContext().stop();
+
+        for (MainListener listener : listeners) {
+            listener.afterStop(this);
+        }
+    }
+}
diff --git a/core/camel-main/src/test/java/org/apache/camel/main/SimpleMainTest.java b/core/camel-main/src/test/java/org/apache/camel/main/SimpleMainTest.java
new file mode 100644
index 0000000..15c642b
--- /dev/null
+++ b/core/camel-main/src/test/java/org/apache/camel/main/SimpleMainTest.java
@@ -0,0 +1,107 @@
+/*
+ * 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;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class SimpleMainTest {
+
+    @Test
+    public void testSimpleMain() throws Exception {
+        List<String> events = new ArrayList<>();
+        CamelContext context = new DefaultCamelContext();
+
+        SimpleMain main = new SimpleMain(context);
+        main.configure().addRoutesBuilder(new MyRouteBuilder());
+        main.addMainListener(new MainListenerSupport() {
+            @Override
+            public void beforeInitialize(BaseMainSupport main) {
+                events.add("beforeInitialize");
+            }
+
+            @Override
+            public void beforeConfigure(BaseMainSupport main) {
+                events.add("beforeConfigure");
+            }
+
+            @Override
+            public void afterConfigure(BaseMainSupport main) {
+                events.add("afterConfigure");
+            }
+
+            @Override
+            public void beforeStart(BaseMainSupport main) {
+                events.add("beforeStart");
+            }
+
+            @Override
+            public void afterStart(BaseMainSupport main) {
+                events.add("afterStart");
+            }
+
+            @Override
+            public void beforeStop(BaseMainSupport main) {
+                events.add("beforeStop");
+            }
+
+            @Override
+            public void afterStop(BaseMainSupport main) {
+                events.add("afterStop");
+            }
+
+        });
+        main.start();
+
+        try {
+            assertSame(context, main.getCamelContext());
+
+            MockEndpoint endpoint = context.getEndpoint("mock:results", MockEndpoint.class);
+            endpoint.expectedMinimumMessageCount(1);
+
+            context.createProducerTemplate().sendBody("direct:start", "<message>1</message>");
+
+            endpoint.assertIsSatisfied();
+        } finally {
+            main.stop();
+        }
+
+        assertTrue(events.contains("beforeInitialize"));
+        assertTrue(events.contains("beforeConfigure"));
+        assertTrue(events.contains("afterConfigure"));
+        assertTrue(events.contains("beforeStart"));
+        assertTrue(events.contains("afterStart"));
+        assertTrue(events.contains("beforeStop"));
+        assertTrue(events.contains("afterStop"));
+    }
+
+    public static class MyRouteBuilder extends RouteBuilder {
+        @Override
+        public void configure() throws Exception {
+            from("direct:start").to("mock:results");
+        }
+    }
+}