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/06/20 06:14:34 UTC
[camel] 04/18: CAMEL-13636: camel3 - SPI for ReactiveHelper so we
can plugin different reactive engines
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
commit f96f3ab5c66adfd6cfc94b1c8dc22b5c85aab671
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Jun 12 13:57:26 2019 +0200
CAMEL-13636: camel3 - SPI for ReactiveHelper so we can plugin different reactive engines
---
.../camel/impl/engine/AbstractCamelContext.java | 5 +-
.../camel/impl/engine/DefaultReactiveExecutor.java | 3 +
.../impl/engine/ReactiveExecutorResolver.java | 73 ++++++++++++++++++++++
.../org/apache/camel/impl/DefaultCamelContext.java | 3 +-
4 files changed, 81 insertions(+), 3 deletions(-)
diff --git a/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java b/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index 4606bc3..01f5d6e 100644
--- a/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ b/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -3256,6 +3256,8 @@ public abstract class AbstractCamelContext extends ServiceSupport implements Ext
}
protected void doStartEagerServices() {
+ getFactoryFinderResolver();
+ getDefaultFactoryFinder();
getComponentResolver();
getDataFormatResolver();
getManagementStrategy();
@@ -3264,8 +3266,6 @@ public abstract class AbstractCamelContext extends ServiceSupport implements Ext
getNodeIdFactory();
getProcessorFactory();
getMessageHistoryFactory();
- getFactoryFinderResolver();
- getDefaultFactoryFinder();
getStreamCachingStrategy();
getModelJAXBContextFactory();
getUuidGenerator();
@@ -3274,6 +3274,7 @@ public abstract class AbstractCamelContext extends ServiceSupport implements Ext
getBeanProxyFactory();
getBeanProcessorFactory();
getBeanPostProcessor();
+ getReactiveExecutor();
}
/**
diff --git a/core/camel-base/src/main/java/org/apache/camel/impl/engine/DefaultReactiveExecutor.java b/core/camel-base/src/main/java/org/apache/camel/impl/engine/DefaultReactiveExecutor.java
index d700e3c..0448037 100644
--- a/core/camel-base/src/main/java/org/apache/camel/impl/engine/DefaultReactiveExecutor.java
+++ b/core/camel-base/src/main/java/org/apache/camel/impl/engine/DefaultReactiveExecutor.java
@@ -25,6 +25,9 @@ import org.apache.camel.support.ReactiveHelper;
*/
public class DefaultReactiveExecutor implements ReactiveExecutor {
+ // TODO: ReactiveHelper code should be moved here and not static
+ // ppl should use the SPI interface
+
@Override
public void scheduleMain(Runnable runnable) {
ReactiveHelper.scheduleMain(runnable);
diff --git a/core/camel-base/src/main/java/org/apache/camel/impl/engine/ReactiveExecutorResolver.java b/core/camel-base/src/main/java/org/apache/camel/impl/engine/ReactiveExecutorResolver.java
new file mode 100644
index 0000000..3a78412
--- /dev/null
+++ b/core/camel-base/src/main/java/org/apache/camel/impl/engine/ReactiveExecutorResolver.java
@@ -0,0 +1,73 @@
+/*
+ * 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.impl.engine;
+
+import java.io.IOException;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.spi.FactoryFinder;
+import org.apache.camel.spi.ReactiveExecutor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Factory resolver to create the {@link org.apache.camel.spi.ReactiveExecutor} to be used.
+ */
+public class ReactiveExecutorResolver {
+
+ public static final String RESOURCE_PATH = "META-INF/services/org/apache/camel/";
+
+ private static final Logger LOG = LoggerFactory.getLogger(ReactiveExecutorResolver.class);
+
+ private FactoryFinder factoryFinder;
+
+ public ReactiveExecutor resolve(CamelContext context) {
+ // use factory finder to find a custom implementations
+ Class<?> type = null;
+ try {
+ type = findFactory("reactive-executor-factory", context);
+ } catch (Exception e) {
+ // ignore
+ }
+
+ if (type != null) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Found ReactiveExecutor: {} via: {}{}", type.getName(), factoryFinder.getResourcePath(), "reactive-executor-factory");
+ }
+ if (ReactiveExecutor.class.isAssignableFrom(type)) {
+ ReactiveExecutor answer = (ReactiveExecutor) context.getInjector().newInstance(type, false);
+ LOG.debug("Detected and using ReactiveExecutor: {}", answer);
+ return answer;
+ } else {
+ throw new IllegalArgumentException("Type is not a ReactiveExecutor implementation. Found: " + type.getName());
+ }
+ }
+
+ // fallback to default
+ LOG.debug("Creating default ReactiveExecutor");
+ return new DefaultReactiveExecutor();
+ }
+
+ private Class<?> findFactory(String name, CamelContext context) throws ClassNotFoundException, IOException {
+ if (factoryFinder == null) {
+ factoryFinder = context.adapt(ExtendedCamelContext.class).getFactoryFinder(RESOURCE_PATH);
+ }
+ return factoryFinder.findClass(name);
+ }
+
+}
diff --git a/core/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java b/core/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
index 1cce2b8..ee0a939 100644
--- a/core/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
+++ b/core/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
@@ -53,6 +53,7 @@ import org.apache.camel.impl.engine.DefaultUnitOfWorkFactory;
import org.apache.camel.impl.engine.DefaultUuidGenerator;
import org.apache.camel.impl.engine.EndpointKey;
import org.apache.camel.impl.engine.HeadersMapFactoryResolver;
+import org.apache.camel.impl.engine.ReactiveExecutorResolver;
import org.apache.camel.impl.engine.RestRegistryFactoryResolver;
import org.apache.camel.impl.engine.ServicePool;
import org.apache.camel.impl.engine.WebSpherePackageScanClassResolver;
@@ -308,6 +309,6 @@ public class DefaultCamelContext extends AbstractModelCamelContext {
}
protected ReactiveExecutor createReactiveExecutor() {
- return new DefaultReactiveExecutor();
+ return new ReactiveExecutorResolver().resolve(this);
}
}