Skip to content

Commit

Permalink
kamelets: create a camel-kamelet component apache#375
Browse files Browse the repository at this point in the history
  • Loading branch information
davsclaus authored and lburgazzoli committed Aug 10, 2020
1 parent 19393c2 commit 43676dc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion camel-kamelet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-support</artifactId>
<artifactId>camel-core-engine</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected Endpoint createEndpoint(String uri, String remaining, Map<String, Obje

//
// The properties for the kamelets are determined by global properties
// and local endpoint parametes,
// and local endpoint parameters,
//
// Global parameters are loaded in the following order:
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.camel.component.kamelet;

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

import org.apache.camel.AsyncCallback;
Expand All @@ -25,12 +27,18 @@
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
import org.apache.camel.impl.event.CamelContextInitializedEvent;
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.spi.CamelEvent;
import org.apache.camel.spi.EventNotifier;
import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.UriEndpoint;
import org.apache.camel.spi.UriPath;
import org.apache.camel.support.DefaultAsyncProducer;
import org.apache.camel.support.DefaultConsumer;
import org.apache.camel.support.DefaultEndpoint;
import org.apache.camel.support.EventNotifierSupport;
import org.apache.camel.support.service.ServiceHelper;

@UriEndpoint(
Expand Down Expand Up @@ -87,16 +95,29 @@ public Consumer createConsumer(Processor processor) throws Exception {
}

@Override
protected void doStart() throws Exception {
try {
// Add a route to the camel context from the given template
// TODO: add validation (requires: https://issues.apache.org/jira/browse/CAMEL-15312)
getCamelContext().addRouteFromTemplate(routeId, templateId, kameletProperties);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
protected void doInit() throws Exception {
super.doInit();

// TODO: lets find a nicer way to do this
EventNotifier notifier = new EventNotifierSupport() {
@Override
public void notify(CamelEvent event) throws Exception {
String id = getCamelContext().addRouteFromTemplate(routeId, templateId, kameletProperties);
List<RouteDefinition> list = new ArrayList<>(1);
list.add(getCamelContext().adapt(ModelCamelContext.class).getRouteDefinition(id));
getCamelContext().adapt(ModelCamelContext.class).startRouteDefinitions(list);
// no longer needed so we can remove ourselves
getCamelContext().getManagementStrategy().removeEventNotifier(this);
}

@Override
public boolean isEnabled(CamelEvent event) {
return event instanceof CamelContextInitializedEvent;
}
};

super.doStart();
ServiceHelper.startService(notifier);
getCamelContext().getManagementStrategy().addEventNotifier(notifier);
}

// *********************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void configure() throws Exception {
public void configure() throws Exception {
// routes
from("direct:template")
.to("kamelet:setBody/test?bodyValue=bv")
.toF("kamelet:setBody/test?bodyValue=%s", body)
.to("log:1");
}
});
Expand Down

0 comments on commit 43676dc

Please sign in to comment.