1

我正在使用 Apache Camel,并且我定义了一个从位于 2 个不同服务器上的 2 个队列接收输入的路由。理想情况下,我希望从两台服务器上消费,但我也希望能够在两个目的地之一关闭时运行应用程序。

这是我的路线:

    try {
        from("mccdsJmsRequest1:queue:{{mccds.queues.in}}").to("direct:apolloMccdsRoute");
    } catch (Exception e){
        // debug line
        System.out.println("Ms1 not reachable");
        e.printStackTrace();
    }
    try {
        from("mccdsJmsRequest2:queue:{{mccds.queues.in}}").to("direct:apolloMccdsRoute");
    } catch (Exception e){
        // debug line
        System.out.println("Ms2 not reachable");
        e.printStackTrace();
    }

    from("direct:apolloMccdsRoute").routeId("apolloMCCDSRoute")
   // Main route starts here...

我在这里声明我的豆子:

@Bean
public JndiObjectFactoryBean mccdsJmsConnectionFactory1() {
    JndiObjectFactoryBean cf = new JndiObjectFactoryBean();
    cf.setJndiEnvironment(prodMccdsJndiProperties.getJndi1());
    cf.setJndiName(jndiName1);
    return cf;
}

@Bean
public JndiObjectFactoryBean mccdsJmsConnectionFactory2(){
    JndiObjectFactoryBean cf = new JndiObjectFactoryBean();
    cf.setJndiEnvironment(prodMccdsJndiProperties.getJndi2());
    cf.setJndiName(jndiName2);  
    return cf;
}

@Inject
private CamelContext camelContext;

@Bean
public JmsComponent mccdsJmsRequest1() {
    JmsComponent ac = new JmsComponent(camelContext);
    ac.setConnectionFactory((ConnectionFactory) mccdsJmsConnectionFactory1().getObject());
    ac.setConcurrentConsumers(5);
    return ac;
}

@Bean
public JmsComponent mccdsJmsRequest2(){
    JmsComponent ac = new JmsComponent(camelContext);
    ac.setConnectionFactory((ConnectionFactory) mccdsJmsConnectionFactory2().getObject());
    ac.setConcurrentConsumers(5);
    return ac;
}

如果无法访问连接工厂之一,则应用程序不会启动。我想捕捉一个忽略异常:

o.s.b.f.s.DefaultListableBeanFactory     : Bean creation exception on non-lazy FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mccdsJmsConnectionFactory2' defined in class path resource [ca/bell/it/spa/uim/apollo/maximo/config/mccds/ProdMccdsJmsConfiguration.class]: Invocation of init method failed; nested exception is javax.naming.CommunicationException [Root exception is java.net.ConnectException: t3://someTestServerIP: Destination unreachable; nested exception is: 
java.net.ConnectException: Connection refused (Connection refused); No available router to destination]
4

2 回答 2

1

尝试设置lookupOnStartup为假。(添加cf.setLookupOnStartup(false)mccdsJmsConnectionFactory1mccdsJmsConnectionFactory2bean 定义。)

还要检查这个线程:http: //forum.spring.io/forum/spring-projects/batch/95620-jndi-lookup

于 2017-07-29T08:25:25.230 回答
0

这是行不通的,因为你的 try/catch 块在你的路由执行期间永远不会被执行。在您的代码中,您只会在 Camel 在应用程序启动时构建路由时首次执行此代码期间遇到异常,此时它并不关心您的队列是否存在。

相反,您需要确定路由在构建后执行时何时发生错误。有很多方法可以做到这一点,但一个好的开始可能是看看骆驼的例外条款

替代甚至更好的选择是使用 Camel 的负载均衡器。它允许您尝试任意数量的端点,并在一个因异常而失败时故障转移到下一个端点。

于 2017-07-28T18:51:09.347 回答