CamelContextAwareConfiguration.java 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package com.gct.tools.etlcamelhuge.camelconfig;
  2. import lombok.SneakyThrows;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.apache.camel.CamelContext;
  5. import org.apache.camel.CamelContextAware;
  6. import org.apache.camel.builder.RouteBuilder;
  7. import org.apache.camel.impl.engine.DefaultCamelContextNameStrategy;
  8. import org.springframework.beans.BeansException;
  9. import org.springframework.context.ApplicationContext;
  10. import org.springframework.context.ApplicationContextAware;
  11. import org.springframework.context.annotation.Configuration;
  12. import java.util.Map;
  13. /**
  14. * class name: CamelContextAwareConfiguration
  15. *
  16. * @author lloyd
  17. * @version 1.0
  18. * @since 2021/4/14 下午5:28
  19. */
  20. @Slf4j
  21. @Configuration
  22. public class CamelContextAwareConfiguration implements CamelContextAware, ApplicationContextAware {
  23. private CamelContext camelContext;
  24. private ApplicationContext applicationContext;
  25. @SneakyThrows
  26. @Override
  27. public void setCamelContext(CamelContext cc) {
  28. this.camelContext = cc;
  29. camelContext.setNameStrategy(new DefaultCamelContextNameStrategy("lloyd-camel-etl"));
  30. final Map<String, RouteBuilder> beans = applicationContext.getBeansOfType(RouteBuilder.class);
  31. log.info("[I say:] total {} defined route builders ", beans.size());
  32. for (RouteBuilder value : beans.values()) camelContext.addRoutes(value);
  33. }
  34. @Override
  35. public CamelContext getCamelContext() {
  36. return camelContext;
  37. }
  38. @Override
  39. public void setApplicationContext(ApplicationContext appContext) throws BeansException {
  40. applicationContext = appContext;
  41. }
  42. }