Skip to content

Instantly share code, notes, and snippets.

@doppiomacchiatto
Last active March 23, 2018 15:45
Show Gist options
  • Select an option

  • Save doppiomacchiatto/fd3f17a1dd431b344e856642eef87157 to your computer and use it in GitHub Desktop.

Select an option

Save doppiomacchiatto/fd3f17a1dd431b344e856642eef87157 to your computer and use it in GitHub Desktop.
Camel Sample Consumer that splits streaming data from Restful invocation
package io.santisi.fuel.routes;
import java.io.IOException;
import org.apache.camel.Exchange;
import org.apache.camel.LoggingLevel;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jackson.JacksonDataFormat;
import org.springframework.stereotype.Component;
import co.santisi.fuel.pojos.FuelStations;
import co.santisi.fuel.processors.JsonProcessor;
@Component
public class CityOfChicagoRoute extends RouteBuilder {
private static final String ENDPOINT_URL = "http4://data.cityofchicago.org/resource/alternative-fuel-locations.json?throwExceptionOnFailure=false&httpClient.soTimeout=5000";
private static final String route_id = "chicago-alt-fuel";
private int maximumRedeliveries = 2;
private long maximumRedeliveryDelay = 10L;
private static final String SPLITTER = "$.[*]";
JacksonDataFormat jacksonFormat = new JacksonDataFormat();
@Override
public void configure() throws Exception {
// enable Jackson json type converter
getContext().getProperties().put("CamelJacksonEnableTypeConverter", "true");
// allow Jackson json to convert to pojo types also (by default jackson
// only converts to String and other simple types)
getContext().getProperties().put("CamelJacksonTypeConverterToPojo", "true");
getContext().getProperties().put("throwExceptionOnFailure", "true");
getContext().getProperties().put(Exchange.LOG_DEBUG_BODY_STREAMS, "true");
getContext().setTracing(true);
// Error Handler
onException(IOException.class).maximumRedeliveries(maximumRedeliveries)
.maximumRedeliveryDelay(maximumRedeliveryDelay);
jacksonFormat.setUnmarshalType(FuelStations.class);
from("timer://neo?repeatCount=1")
.routeId(route_id)
.streamCaching()
.setHeader(Exchange.HTTP_METHOD, constant("GET"))
.to(ENDPOINT_URL)
.split().jsonpath(SPLITTER).streaming()
.convertBodyTo(String.class)
.log(LoggingLevel.DEBUG,"StringConvert >>>> ${body}")
.unmarshal(jacksonFormat)
.log(LoggingLevel.DEBUG,"Unmarshall >>>{$body}")
.process(new JsonProcessor())
.marshal(jacksonFormat)
.to("file:out");
// send documents to Elasticsearch
from("file:out?noop=true&moveFailed=out/.error")
.setHeader(Exchange.HTTP_METHOD, constant(HttpMethod.POST))
.marshal()
.mimeMultipart()
.to("http://localhost:9200/fuelstations/location/pretty&pretty");
/**
* For Testing Only
*/
//from("direct:test")
//.setHeader("CamelFileName", constant("report.json"))
//.to("file:out?noop=true&fileExist=Override");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment