Skip to content

Commit

Permalink
CAMEL-13243: Main class should be more easier to use, so lets drop th…
Browse files Browse the repository at this point in the history
…e confusing multiple CamelContext stuff that was not really intended/working.
  • Loading branch information
davsclaus committed Feb 28, 2019
1 parent b017abe commit bfe9fc4
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 311 deletions.
4 changes: 4 additions & 0 deletions MIGRATION.md
Expand Up @@ -126,6 +126,10 @@ In Camel 2.x we have deprecated `getProperties` on `CamelContext` in favour of `
</globalOptions> </globalOptions>




### Main class

The `Main` class from `camel-core`, `camel-spring` and `camel-cdi` has been modified to only support a single `CamelContext` which was really its intention, but there was some old crufy code for multiple Camels. The method `getCamelContextMap` has been removed, and there is just a `getCamelContext` method now.

### Moved APIs ### Moved APIs


The following API changes may affect your existing Camel applications, which needs to be migrated. The following API changes may affect your existing Camel applications, which needs to be migrated.
Expand Down
29 changes: 15 additions & 14 deletions components/camel-cdi/src/main/java/org/apache/camel/cdi/Main.java
Expand Up @@ -18,11 +18,6 @@


import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;

import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;

import javax.enterprise.inject.UnsatisfiedResolutionException;
import javax.enterprise.inject.spi.Bean; import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager; import javax.enterprise.inject.spi.BeanManager;


Expand All @@ -33,9 +28,10 @@
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;


import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;
import static org.apache.camel.cdi.AnyLiteral.ANY; import static org.apache.camel.cdi.AnyLiteral.ANY;
import static org.apache.camel.cdi.BeanManagerHelper.getReference; import static org.apache.camel.cdi.BeanManagerHelper.getReference;
import static org.apache.camel.cdi.BeanManagerHelper.getReferenceByType;
import static org.apache.deltaspike.cdise.api.CdiContainerLoader.getCdiContainer; import static org.apache.deltaspike.cdise.api.CdiContainerLoader.getCdiContainer;


/** /**
Expand Down Expand Up @@ -75,19 +71,24 @@ public static Main getInstance() {


@Override @Override
protected ProducerTemplate findOrCreateCamelTemplate() { protected ProducerTemplate findOrCreateCamelTemplate() {
return getReferenceByType(cdiContainer.getBeanManager(), CamelContext.class) if (getCamelContext() == null) {
.orElseThrow( throw new IllegalArgumentException("No CamelContext are available so cannot create a ProducerTemplate!");
() -> new UnsatisfiedResolutionException("No default Camel context is deployed, " }
+ "cannot create default ProducerTemplate!")) return getCamelContext().createProducerTemplate();
.createProducerTemplate();
} }


@Override @Override
protected Map<String, CamelContext> getCamelContextMap() { protected CamelContext createCamelContext() {
BeanManager manager = cdiContainer.getBeanManager(); BeanManager manager = cdiContainer.getBeanManager();
return manager.getBeans(CamelContext.class, ANY).stream() Map<String, CamelContext> camels = manager.getBeans(CamelContext.class, ANY).stream()
.map(bean -> getReference(manager, CamelContext.class, bean)) .map(bean -> getReference(manager, CamelContext.class, bean))
.collect(toMap(CamelContext::getName, identity())); .collect(toMap(CamelContext::getName, identity()));
if (camels.size() > 1) {
throw new IllegalArgumentException("Multiple CamelContext detected. This Main class only supports single CamelContext");
} else if (camels.size() == 1) {
return camels.values().iterator().next();
}
return null;
} }


@Override @Override
Expand All @@ -98,7 +99,7 @@ protected void doStart() throws Exception {
container.getContextControl().startContexts(); container.getContextControl().startContexts();
cdiContainer = container; cdiContainer = container;
super.doStart(); super.doStart();
postProcessContext(); initCamelContext();
warnIfNoCamelFound(); warnIfNoCamelFound();
} }


Expand Down
Expand Up @@ -16,8 +16,6 @@
*/ */
package org.apache.camel.spring.boot; package org.apache.camel.spring.boot;


import java.util.Collections;
import java.util.Map;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.PreDestroy; import javax.annotation.PreDestroy;
Expand Down Expand Up @@ -45,8 +43,8 @@ protected ProducerTemplate findOrCreateCamelTemplate() {
} }


@Override @Override
protected Map<String, CamelContext> getCamelContextMap() { protected CamelContext createCamelContext() {
return Collections.singletonMap("camelContext", camelContext); return camelContext;
} }


@Override @Override
Expand Down
Expand Up @@ -22,7 +22,6 @@
import java.net.URL; import java.net.URL;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Map; import java.util.Map;
Expand Down Expand Up @@ -144,6 +143,17 @@ public void setParentApplicationContextUri(String parentApplicationContextUri) {
// Implementation methods // Implementation methods
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------


@Override
protected CamelContext createCamelContext() {
Map<String, SpringCamelContext> camels = applicationContext.getBeansOfType(SpringCamelContext.class);
if (camels.size() > 1) {
throw new IllegalArgumentException("Multiple CamelContext detected. This Main class only supports single CamelContext");
} else if (camels.size() == 1) {
return camels.values().iterator().next();
}
return null;
}

@Override @Override
protected void doStart() throws Exception { protected void doStart() throws Exception {
try { try {
Expand All @@ -164,13 +174,11 @@ protected void doStart() throws Exception {
LOG.debug("Starting Spring ApplicationContext: {}", applicationContext.getId()); LOG.debug("Starting Spring ApplicationContext: {}", applicationContext.getId());
applicationContext.start(); applicationContext.start();


postProcessContext(); initCamelContext();
} finally { } finally {
if (camelContexts != null && !camelContexts.isEmpty()) { // if we were veto started then mark as completed
// if we were veto started then mark as completed if (getCamelContext() != null && getCamelContext().isVetoStarted()) {
if (getCamelContexts().get(0).isVetoStarted()) { completed();
completed();
}
} }
} }
} }
Expand All @@ -192,10 +200,10 @@ protected ProducerTemplate findOrCreateCamelTemplate() {
if (names != null && names.length > 0) { if (names != null && names.length > 0) {
return getApplicationContext().getBean(names[0], ProducerTemplate.class); return getApplicationContext().getBean(names[0], ProducerTemplate.class);
} }
if (getCamelContexts().isEmpty()) { if (getCamelContext() == null) {
throw new IllegalArgumentException("No CamelContexts are available so cannot create a ProducerTemplate!"); throw new IllegalArgumentException("No CamelContext are available so cannot create a ProducerTemplate!");
} }
return getCamelContexts().get(0).createProducerTemplate(); return getCamelContext().createProducerTemplate();
} }


protected AbstractApplicationContext createDefaultApplicationContext() throws IOException { protected AbstractApplicationContext createDefaultApplicationContext() throws IOException {
Expand All @@ -221,18 +229,6 @@ protected AbstractApplicationContext createDefaultApplicationContext() throws IO
} }
} }


protected Map<String, CamelContext> getCamelContextMap() {
Map<String, SpringCamelContext> map = applicationContext.getBeansOfType(SpringCamelContext.class);
Set<Map.Entry<String, SpringCamelContext>> entries = map.entrySet();
Map<String, CamelContext> answer = new HashMap<>();
for (Map.Entry<String, SpringCamelContext> entry : entries) {
String name = entry.getKey();
CamelContext camelContext = entry.getValue();
answer.put(name, camelContext);
}
return answer;
}

protected AbstractApplicationContext createAdditionalLocationsFromClasspath() throws IOException { protected AbstractApplicationContext createAdditionalLocationsFromClasspath() throws IOException {
Set<String> locations = new LinkedHashSet<>(); Set<String> locations = new LinkedHashSet<>();
findLocations(locations, Main.class.getClassLoader()); findLocations(locations, Main.class.getClassLoader());
Expand Down
Expand Up @@ -43,10 +43,7 @@ public void configure() throws Exception {
}); });
main.start(); main.start();


List<CamelContext> contextList = main.getCamelContexts(); CamelContext camelContext = main.getCamelContext();
assertNotNull(contextList);
assertEquals("size", 1, contextList.size());
CamelContext camelContext = contextList.get(0);


MockEndpoint endpoint = camelContext.getEndpoint("mock:results", MockEndpoint.class); MockEndpoint endpoint = camelContext.getEndpoint("mock:results", MockEndpoint.class);
// in case we add more files in src/test/data // in case we add more files in src/test/data
Expand Down

0 comments on commit bfe9fc4

Please sign in to comment.