Skip to content

Instantly share code, notes, and snippets.

@garyhodgson
Created March 6, 2017 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garyhodgson/bb3fff743bdbe5ea6ad842895f62552b to your computer and use it in GitHub Desktop.
Save garyhodgson/bb3fff743bdbe5ea6ad842895f62552b to your computer and use it in GitHub Desktop.
Example Fluent Java DSL Builders
import static com.garyhodgson.cameljpatest.Startup.ActiveMQBuilder.activemq;
import static com.garyhodgson.cameljpatest.Startup.TimerBuilder.timer;
import org.apache.activemq.camel.component.ActiveMQComponent;
import static org.apache.activemq.camel.component.ActiveMQComponent.activeMQComponent;
import org.apache.activemq.camel.component.ActiveMQEndpoint;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.timer.TimerComponent;
import org.apache.camel.component.timer.TimerEndpoint;
import org.apache.camel.main.Main;
public class Startup {
public static void main(String[] args) throws Exception {
Main main = new Main();
main.bind("activemq", activeMQComponent("vm://localhost?broker.persistent=false"));
main.addRouteBuilder(new RouteBuilder() {
@Override
public void configure() throws Exception {
from(timer("abc").period(1000).build(getContext()))
.setBody(constant("abc"))
.to(activemq()
.queue("abc")
.timeToLive(2000)
.build(getContext()));
from(activemq()
.queue("abc")
.build(getContext()))
.to("log:output");
}
});
main.run();
}
static class TimerBuilder {
private final TimerEndpoint endpoint;
public TimerBuilder(String name) {
endpoint = new TimerEndpoint("timer:", new TimerComponent(), name);
}
public static TimerBuilder timer(String name) {
return new TimerBuilder(name);
}
public Endpoint build(CamelContext camelContext) {
endpoint.setCamelContext(camelContext);
return endpoint;
}
public TimerBuilder period(int period) {
endpoint.setPeriod(period);
return this;
}
}
static class ActiveMQBuilder {
private final ActiveMQEndpoint endpoint;
public static ActiveMQBuilder activemq() {
return new ActiveMQBuilder();
}
public ActiveMQBuilder() {
endpoint = new ActiveMQEndpoint();
}
public Endpoint build(CamelContext camelContext) throws Exception {
endpoint.setCamelContext(camelContext);
endpoint.setConnectionFactory(camelContext.getComponent("activemq", ActiveMQComponent.class).getConfiguration().getConnectionFactory());
return endpoint;
}
public ActiveMQBuilder timeToLive(long timeToLive) {
endpoint.setTimeToLive(timeToLive);
return this;
}
public ActiveMQBuilder queue(String name) {
endpoint.setDestination(new ActiveMQQueue(name));
return this;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment