Skip to content

Commit

Permalink
CAMEL-21353: camel-core - Add possibility to set some condition for C…
Browse files Browse the repository at this point in the history
…amel to wait during startup before continuing
  • Loading branch information
davsclaus committed Oct 19, 2024
1 parent f239050 commit c827cba
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ public interface StartupConditionStrategy extends StaticService {
*/
void addStartupCondition(StartupCondition startupCondition);

/**
* A list of custom class names (FQN) for {@link StartupCondition} classes. Multiple classes can be separated by
* comma.
*/
void addStartupConditions(String classNames);

/**
* Lists all the {@link StartupCondition}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
import org.apache.camel.support.DefaultUuidGenerator;
import org.apache.camel.support.PluginHelper;
import org.apache.camel.support.ResolverHelper;
import org.apache.camel.support.startup.DefaultStartupConditionStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ protected void autoConfigurationStartupConditions(
}
String file = mainConfigurationProperties.startupCondition().getFileExists();
if (file != null) {
scs.addStartupCondition(new FileStartupCondition(env));
scs.addStartupCondition(new FileStartupCondition(file));
}
String classes = mainConfigurationProperties.startupCondition().getCustomClassNames();
if (classes != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.impl.engine;
package org.apache.camel.support.startup;

import java.util.ArrayList;
import java.util.List;

import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.StartupStep;
import org.apache.camel.VetoCamelContextStartException;
import org.apache.camel.spi.StartupCondition;
import org.apache.camel.spi.StartupConditionStrategy;
import org.apache.camel.spi.StartupStepRecorder;
import org.apache.camel.support.OrderedComparator;
import org.apache.camel.support.service.ServiceSupport;
import org.apache.camel.util.StopWatch;
Expand All @@ -40,6 +43,7 @@ public class DefaultStartupConditionStrategy extends ServiceSupport implements S

private CamelContext camelContext;
private final List<StartupCondition> conditions = new ArrayList<>();
private String classNames;
private boolean enabled;
private int interval = 500;
private int timeout = 20000;
Expand Down Expand Up @@ -98,6 +102,11 @@ public void addStartupCondition(StartupCondition startupCondition) {
conditions.add(startupCondition);
}

@Override
public void addStartupConditions(String classNames) {
this.classNames = classNames;
}

@Override
public List<StartupCondition> getStartupConditions() {
return conditions;
Expand All @@ -109,8 +118,26 @@ public void checkStartupConditions() throws VetoCamelContextStartException {
try {
var list = new ArrayList<>(conditions);
list.addAll(camelContext.getRegistry().findByType(StartupCondition.class));
if (classNames != null) {
for (String fqn : classNames.split(",")) {
fqn = fqn.trim();
Class<? extends StartupCondition> clazz
= camelContext.getClassResolver().resolveMandatoryClass(fqn, StartupCondition.class);
list.add(camelContext.getInjector().newInstance(clazz));
}
}
list.sort(OrderedComparator.get());
doCheckConditions(list);

if (!list.isEmpty()) {
StartupStepRecorder recorder = camelContext.getCamelContextExtension().getStartupStepRecorder();
StartupStep step = recorder.beginStep(CamelContext.class, camelContext.getCamelContextExtension().getName(),
"Check Startup Conditions");
doCheckConditions(list);
recorder.endStep(step);
}

} catch (ClassNotFoundException e) {
throw RuntimeCamelException.wrapRuntimeCamelException(e);
} finally {
checkDone = true;
}
Expand Down

0 comments on commit c827cba

Please sign in to comment.