Skip to content

Instantly share code, notes, and snippets.

@gysel
Last active September 18, 2017 13:40
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 gysel/9fe8a2eeb2f8ab731256d3329fd7d289 to your computer and use it in GitHub Desktop.
Save gysel/9fe8a2eeb2f8ab731256d3329fd7d289 to your computer and use it in GitHub Desktop.
Camel onCompletion example
package com.example.oncompletiontest;
import org.apache.camel.spring.boot.CamelSpringBootApplicationController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class OncompletionTestApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(OncompletionTestApplication.class, args);
CamelSpringBootApplicationController controller = context.getBean(CamelSpringBootApplicationController.class);
controller.run();
}
}
package com.example.oncompletiontest;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.BindyType;
import org.springframework.stereotype.Component;
@Component
public class Route extends RouteBuilder {
@Override
public void configure() throws Exception {
from("file:in").setHeader("message", body())
.onCompletion().bean(TestProcessor.class, "testMethod").end()
.choice()
.when(header("camelfilenameonly").contains("test1.csv"))
.unmarshal().bindy(BindyType.Csv, Test1CsvDto.class)
.bean(TestProcessor.class, "processTest1Object").stop()
.otherwise()
.choice()
.when(header("camelfilenameonly").contains("test2.csv"))
//Unmarshal csv
.unmarshal().bindy(BindyType.Csv, Test2CsvDto.class)
.bean(TestProcessor.class, "processTest2")
.stop();
}
}
package com.example.oncompletiontest;
import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
@CsvRecord(separator = ",")
public class Test1CsvDto {
public String message;
}
package com.example.oncompletiontest;
import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
import org.apache.camel.dataformat.bindy.annotation.DataField;
@CsvRecord(separator = ",")
public class Test2CsvDto {
@DataField(pos = 1, position = 1)
public String message;
}
package com.example.oncompletiontest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestProcessor {
private Logger log = LoggerFactory.getLogger(TestProcessor.class);
public void testMethod(Object o) {
log.info("testmethod");
}
public void processTest1Object(Object o) {
log.info("processTest1Object");
}
public void processTest2(Object o) {
log.info("processTest2");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment