使用Java配置时,Camel的BridgePropertyPlaceholderConfigurer无法正常工作

疯子

我正在使用Spring Java配置并编写带有一些Camel路由的控制台应用程序。我的应用程序中有多个属性源,因此我使用两个PropertyPlaceholderConfigurers:

@Configuration
@Import(CamelConfig.class)
@ComponentScan(basePackageClasses = {App.class})
public class Config
{
  final static String ENV = System.getProperty( "ENV" );

  @Bean
  public static BridgePropertyPlaceholderConfigurer properties()
  {
    final BridgePropertyPlaceholderConfigurer result = new BridgePropertyPlaceholderConfigurer();

    result.setOrder( 0 );
    result.setIgnoreUnresolvablePlaceholders( true );
    result.setLocations( new ClassPathResource( "a/b/c/environments/base.properties" ),
      new ClassPathResource( "a/b/c/environments/" + ENV + "/env.properties" ) );

    return result;
  }

  @Bean
  public static BridgePropertyPlaceholderConfigurer dlqAppProperties()
  {
    final YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
    final BridgePropertyPlaceholderConfigurer result = new BridgePropertyPlaceholderConfigurer();

    yaml.setResources( new ClassPathResource( "app.yaml" ) );
    result.setOrder( 1 );
    result.setIgnoreUnresolvablePlaceholders( true );
    result.setProperties( yaml.getObject() );

    return result;
  }
}

根据此文档,我正在使用BridgePropertyPlaceholderConfigurer类使Camel中的Spring属性可用。它的配置也很简单:

@Configuration
public class CamelConfig extends SingleRouteCamelConfiguration
{
  @Override
  protected CamelContext createCamelContext() throws Exception
  {
    final SpringCamelContext result = new SpringCamelContext( getApplicationContext() );

    return result;
  }

  @Override
  protected void setupCamelContext( CamelContext camelContext ) throws Exception
  {
  }

  @Bean
  @Override
  public RouteBuilder route()
  {
    return (new Routes()).builder();
  }
}

测试路由(Scala DSL)也很简单:

class Routes extends RouteBuilder {
  "timer://{{foo}}?period=2s" ==> {
    process((exchange) => {
      exchange.getIn.setBody("test")
    })
    to("log:test")
  }
}

但是上下文并非以以下异常开头:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'camelContext' defined in class path resource [a/b/c/config/CamelConfig.class]: Invocation of init method failed; nested exception is org.apache.camel.FailedToCreateRouteException: Failed to create route route1: Route(route1)[[From[timer://{{foo}}?period=2s]] -> [process[... because of Failed to resolve endpoint: timer://{{foo}}?period=2s due to: PropertiesComponent with name properties must be defined in CamelContext to support property placeholders.
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
  at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
  at a.b.c.App.main(App.java:13)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:606)
  at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: org.apache.camel.FailedToCreateRouteException: Failed to create route route1: Route(route1)[[From[timer://{{foo}}?period=2s]] -> [process[... because of Failed to resolve endpoint: timer://{{foo}}?period=2s due to: PropertiesComponent with name properties must be defined in CamelContext to support property placeholders.
  at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:182)
  at org.apache.camel.impl.DefaultCamelContext.startRoute(DefaultCamelContext.java:770)
  at org.apache.camel.impl.DefaultCamelContext.startRouteDefinitions(DefaultCamelContext.java:1914)
  at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:1670)
  at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:1544)
  at org.apache.camel.spring.SpringCamelContext.doStart(SpringCamelContext.java:179)
  at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
  at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:1512)
  at org.apache.camel.spring.SpringCamelContext.maybeStart(SpringCamelContext.java:228)
  at org.apache.camel.spring.SpringCamelContext.afterPropertiesSet(SpringCamelContext.java:104)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
  ... 16 more
Caused by: org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: timer://{{foo}}?period=2s due to: PropertiesComponent with name properties must be defined in CamelContext to support property placeholders.
  at org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:477)
  at org.apache.camel.util.CamelContextHelper.getMandatoryEndpoint(CamelContextHelper.java:63)
  at org.apache.camel.model.RouteDefinition.resolveEndpoint(RouteDefinition.java:192)
  at org.apache.camel.impl.DefaultRouteContext.resolveEndpoint(DefaultRouteContext.java:106)
  at org.apache.camel.impl.DefaultRouteContext.resolveEndpoint(DefaultRouteContext.java:112)
  at org.apache.camel.model.FromDefinition.resolveEndpoint(FromDefinition.java:72)
  at org.apache.camel.impl.DefaultRouteContext.getEndpoint(DefaultRouteContext.java:88)
  at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:890)
  at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:177)
  ... 27 more
Caused by: java.lang.IllegalArgumentException: PropertiesComponent with name properties must be defined in CamelContext to support property placeholders.
  at org.apache.camel.impl.DefaultCamelContext.resolvePropertyPlaceholders(DefaultCamelContext.java:1121)
  at org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:475)
  ... 35 more

看起来桥不起作用(但我绝对可以在Spring中使用占位符)。可能是什么问题?

疯子

看起来,如果要使用BridgePropertyPlaceholderConfigurer,则需要使用实例化Camel上下文CamelContextFactoryBean它具有initPropertyPlaceholder方法:

@Override
protected void initPropertyPlaceholder() throws Exception {
    super.initPropertyPlaceholder();

    Map<String, BridgePropertyPlaceholderConfigurer> beans = applicationContext.getBeansOfType(BridgePropertyPlaceholderConfigurer.class);
    if (beans.size() == 1) {
        // setup properties component that uses this beans
        BridgePropertyPlaceholderConfigurer configurer = beans.values().iterator().next();
        String id = beans.keySet().iterator().next();
        LOG.info("Bridging Camel and Spring property placeholder configurer with id: " + id);

        // get properties component
        PropertiesComponent pc = getContext().getComponent("properties", PropertiesComponent.class);
        // replace existing resolver with us
        configurer.setResolver(pc.getPropertiesResolver());
        configurer.setParser(pc.getPropertiesParser());
        String ref = "ref:" + id;
        // use the bridge to handle the resolve and parsing
        pc.setPropertiesResolver(configurer);
        pc.setPropertiesParser(configurer);
        // and update locations to have our as ref first
        String[] locations = pc.getLocations();
        String[] updatedLocations;
        if (locations != null && locations.length > 0) {
            updatedLocations = new String[locations.length + 1];
            updatedLocations[0] = ref;
            System.arraycopy(locations, 0, updatedLocations, 1, locations.length);
        } else {
            updatedLocations = new String[]{ref};
        }
        pc.setLocations(updatedLocations);
    } else if (beans.size() > 1) {
        LOG.warn("Cannot bridge Camel and Spring property placeholders, as exact only 1 bean of type BridgePropertyPlaceholderConfigurer"
                + " must be defined, was {} beans defined.", beans.size());
    }
}

好吧,现在的问题是要有两个桥梁,但这是另一回事了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Traefik:使用docker的配置无法正常工作

来自分类Dev

使用innerHTML时JavaScript无法正常工作

来自分类Dev

使用变量时,sed无法正常工作

来自分类Dev

使用Proguard时,gmailsender无法正常工作

来自分类Dev

使用innerHTML时JavaScript无法正常工作

来自分类Dev

配置有Camel + Spring空闲超时设置的ActiveMQ无法正常工作

来自分类Dev

Camel Spring DSL无法正常工作

来自分类Dev

使用Java的Mirth getMessageCount无法正常工作

来自分类Dev

在 Java 中使用 JDBC 创建数据库时,主要方法无法正常工作

来自分类Dev

Java Rect.intersects()有时无法正常工作

来自分类Dev

Chrome成为焦点时,Java Robot无法正常工作

来自分类Dev

当 32 位工作正常时无法安装 java 64 位

来自分类常见问题

使用grunt构建项目时Fontawesome无法正常工作

来自分类Dev

使用grunt构建项目时,Google字体无法正常工作

来自分类Dev

使用ng-repeat时Flexslider无法正常工作

来自分类Dev

使用角度继承时,$ injector无法正常工作

来自分类Dev

当我使用变量时,sed无法正常工作

来自分类Dev

使用CHAID包生成树时,Minbucket无法正常工作

来自分类Dev

使用FontAwesome时Flexbox大小调整无法正常工作

来自分类Dev

使用块内容时,Django CSS无法正常工作

来自分类Dev

使用grunt构建项目时Fontawesome无法正常工作

来自分类Dev

当我使用附加时,jQuery将无法正常工作

来自分类Dev

使用自定义指令时,ngShow无法正常工作

来自分类Dev

使用角度继承时,$ injector无法正常工作

来自分类Dev

使用basename时,无法使“ awk for loop”正常工作

来自分类Dev

使用双别名时MySQL无法正常工作

来自分类Dev

当“浮动”无法正常工作时,水平交换DIV(使用CSS)

来自分类Dev

$ .ajax在使用fullcalendar js时无法正常工作吗?

来自分类Dev

rsync:使用变量时排除无法正常工作

Related 相关文章

  1. 1

    Traefik:使用docker的配置无法正常工作

  2. 2

    使用innerHTML时JavaScript无法正常工作

  3. 3

    使用变量时,sed无法正常工作

  4. 4

    使用Proguard时,gmailsender无法正常工作

  5. 5

    使用innerHTML时JavaScript无法正常工作

  6. 6

    配置有Camel + Spring空闲超时设置的ActiveMQ无法正常工作

  7. 7

    Camel Spring DSL无法正常工作

  8. 8

    使用Java的Mirth getMessageCount无法正常工作

  9. 9

    在 Java 中使用 JDBC 创建数据库时,主要方法无法正常工作

  10. 10

    Java Rect.intersects()有时无法正常工作

  11. 11

    Chrome成为焦点时,Java Robot无法正常工作

  12. 12

    当 32 位工作正常时无法安装 java 64 位

  13. 13

    使用grunt构建项目时Fontawesome无法正常工作

  14. 14

    使用grunt构建项目时,Google字体无法正常工作

  15. 15

    使用ng-repeat时Flexslider无法正常工作

  16. 16

    使用角度继承时,$ injector无法正常工作

  17. 17

    当我使用变量时,sed无法正常工作

  18. 18

    使用CHAID包生成树时,Minbucket无法正常工作

  19. 19

    使用FontAwesome时Flexbox大小调整无法正常工作

  20. 20

    使用块内容时,Django CSS无法正常工作

  21. 21

    使用grunt构建项目时Fontawesome无法正常工作

  22. 22

    当我使用附加时,jQuery将无法正常工作

  23. 23

    使用自定义指令时,ngShow无法正常工作

  24. 24

    使用角度继承时,$ injector无法正常工作

  25. 25

    使用basename时,无法使“ awk for loop”正常工作

  26. 26

    使用双别名时MySQL无法正常工作

  27. 27

    当“浮动”无法正常工作时,水平交换DIV(使用CSS)

  28. 28

    $ .ajax在使用fullcalendar js时无法正常工作吗?

  29. 29

    rsync:使用变量时排除无法正常工作

热门标签

归档