CamelFileConfiguration.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package com.gct.tools.etlcamelhuge.routeconfig;
  2. import org.apache.camel.Message;
  3. import org.apache.camel.builder.RouteBuilder;
  4. import org.apache.camel.component.file.GenericFile;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import java.io.BufferedReader;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.InputStreamReader;
  13. import java.time.Duration;
  14. import java.time.LocalDateTime;
  15. import java.util.Date;
  16. import java.util.UUID;
  17. /**
  18. * class name: CamelFileConfiguration
  19. *
  20. * @author lloyd
  21. * @version 1.0
  22. * @since 2021/4/14 下午3:07
  23. */
  24. //@Configuration
  25. public class CamelFileConfiguration {
  26. @Bean
  27. RouteBuilder routeBuilderFile() {
  28. return new RouteBuilder() {
  29. private Object apply(GenericFile genericFile) {
  30. File actualFile = File.class.cast(genericFile.getFile());
  31. try {
  32. BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(actualFile)));
  33. StringBuilder s = new StringBuilder();
  34. while (true) {
  35. String s1 = in.readLine();
  36. s.append(s1);
  37. s.append("@MGT");
  38. if (s1 == null || s1.isEmpty()) break;
  39. // s.append("\r\n");
  40. }
  41. s.append("\n,OK by lloyd!\n");
  42. log.info("return s ::{}", s.toString());
  43. return s.toString();
  44. // FileWriter writer = new FileWriter(actualFile);
  45. // writer.append("your code perfect");
  46. // writer.close();
  47. } catch (Exception e) {
  48. throw new RuntimeException("OK!there is a error");
  49. }
  50. }
  51. Logger log = LoggerFactory.getLogger(this.getClass());
  52. @Override
  53. public void configure() throws Exception {
  54. // from("jms-queue:queue:my-queue")
  55. from("file:{{user.home}}/Desktop/cameltest/in")
  56. .routeId("file-to-file")
  57. .transform()
  58. .body(GenericFile.class, this::apply)
  59. .log("this is my test -camel")
  60. .setHeader("CamelFileName", () -> UUID.randomUUID().toString())
  61. .tracing()
  62. .process()
  63. .exchange(exchange -> {
  64. new Date().toInstant();
  65. LocalDateTime start = LocalDateTime.now();
  66. final Message in = exchange.getIn();
  67. String body = in.getBody(String.class);
  68. log.info("body is {}", body);
  69. Thread t = new Thread(() -> {
  70. while (true) {
  71. try {
  72. Thread.sleep(1000);
  73. } catch (InterruptedException e) {
  74. e.printStackTrace();
  75. }
  76. log.warn("thread completed");
  77. break;
  78. }
  79. });
  80. t.start();
  81. try {
  82. t.join();
  83. } catch (InterruptedException e) {
  84. e.printStackTrace();
  85. }
  86. final LocalDateTime end = LocalDateTime.now();
  87. final long l = Duration.between(start, end).toDays();
  88. log.warn("run with time : {}", l);
  89. })
  90. .to("file:{{user.home}}/Desktop/cameltest/out");
  91. }
  92. };
  93. }
  94. }