123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package com.gct.tools.etlcamelhuge.routeconfig;
- import org.apache.camel.Message;
- import org.apache.camel.builder.RouteBuilder;
- import org.apache.camel.component.file.GenericFile;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStreamReader;
- import java.time.Duration;
- import java.time.LocalDateTime;
- import java.util.Date;
- import java.util.UUID;
- /**
- * class name: CamelFileConfiguration
- *
- * @author lloyd
- * @version 1.0
- * @since 2021/4/14 下午3:07
- */
- //@Configuration
- public class CamelFileConfiguration {
- @Bean
- RouteBuilder routeBuilderFile() {
- return new RouteBuilder() {
- private Object apply(GenericFile genericFile) {
- File actualFile = File.class.cast(genericFile.getFile());
- try {
- BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(actualFile)));
- StringBuilder s = new StringBuilder();
- while (true) {
- String s1 = in.readLine();
- s.append(s1);
- s.append("@MGT");
- if (s1 == null || s1.isEmpty()) break;
- // s.append("\r\n");
- }
- s.append("\n,OK by lloyd!\n");
- log.info("return s ::{}", s.toString());
- return s.toString();
- // FileWriter writer = new FileWriter(actualFile);
- // writer.append("your code perfect");
- // writer.close();
- } catch (Exception e) {
- throw new RuntimeException("OK!there is a error");
- }
- }
- Logger log = LoggerFactory.getLogger(this.getClass());
- @Override
- public void configure() throws Exception {
- // from("jms-queue:queue:my-queue")
- from("file:{{user.home}}/Desktop/cameltest/in")
- .routeId("file-to-file")
- .transform()
- .body(GenericFile.class, this::apply)
- .log("this is my test -camel")
- .setHeader("CamelFileName", () -> UUID.randomUUID().toString())
- .tracing()
- .process()
- .exchange(exchange -> {
- new Date().toInstant();
- LocalDateTime start = LocalDateTime.now();
- final Message in = exchange.getIn();
- String body = in.getBody(String.class);
- log.info("body is {}", body);
- Thread t = new Thread(() -> {
- while (true) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- log.warn("thread completed");
- break;
- }
- });
- t.start();
- try {
- t.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- final LocalDateTime end = LocalDateTime.now();
- final long l = Duration.between(start, end).toDays();
- log.warn("run with time : {}", l);
- })
- .to("file:{{user.home}}/Desktop/cameltest/out");
- }
- };
- }
- }
|