【发布时间】:2020-07-15 03:20:44
【问题描述】:
我正在创建一个使用 Apache Camel 将消息从 AMQP 传输到 Kafka 的应用程序。代码也可以看这里——https://github.com/prashantbhardwaj/qpid-to-kafka-using-camel
我想将它创建为使用 spring、amqp 和 kafka 启动器的独立 SpringBoot 应用程序。创建了类似的路线
@Component
public class QpidToKafkaRoute extends RouteBuilder {
public void configure() throws Exception {
from("amqp:queue:destinationName")
.to("kafka:topic");
}
}
而SpringBoot应用配置是
@SpringBootApplication
public class CamelSpringJmsKafkaApplication {
public static void main(String[] args) {
SpringApplication.run(CamelSpringJmsKafkaApplication.class, args);
}
@Bean
public JmsConnectionFactory jmsConnectionFactory(@Value("${qpidUser}") String qpidUser, @Value("${qpidPassword}") String qpidPassword, @Value("${qpidBrokerUrl}") String qpidBrokerUrl) {
JmsConnectionFactory jmsConnectionFactory = new JmsConnectionFactory(qpidPassword, qpidPassword, qpidBrokerUrl);
return jmsConnectionFactory;
}
@Bean
@Primary
public CachingConnectionFactory jmsCachingConnectionFactory(JmsConnectionFactory jmsConnectionFactory) {
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(jmsConnectionFactory);
return cachingConnectionFactory;
}
使用 Spring Bean 注解创建的 jmsConnectionFactory bean 应该由 amqp starter 选择并注入到路由中。但它没有发生。当我启动这个应用程序时,我得到了以下异常 -
org.apache.camel.FailedToStartRouteException: Failed to start route route1 because of Route(route1)[From[amqp:queue:destinationName] -> [To[kafka:.
Caused by: java.lang.IllegalArgumentException: connectionFactory must be specified
如果我在 application.properties 文件中传递正确的属性,则应该自动创建 connectionFactory。
我的 application.properties 文件如下所示:
camel.springboot.main-run-controller = true
camel.component.amqp.enabled = true
camel.component.amqp.connection-factory = jmsCachingConnectionFactory
camel.component.amqp.async-consumer = true
camel.component.amqp.concurrent-consumers = 1
camel.component.amqp.map-jms-message = true
camel.component.amqp.test-connection-on-startup = true
camel.component.kafka.brokers = localhost:9092
qpidBrokerUrl = amqp://localhost:5672?jms.username=guest&jms.password=guest&jms.clientID=clientid2&amqp.vhost=default
qpidUser = guest
qpidPassword = guest
能否请您帮忙说明为什么在自动配置期间未使用 connectionFactory 对象?当我调试这段代码时,我可以清楚地看到正在创建 connectionFactory bean。
我什至可以再看到一条日志行 -
CamelContext has only been running for less than a second. If you intend to run Camel for a longer time then you can set the property camel.springboot.main-run-controller=true in application.properties or add spring-boot-starter-web JAR to the classpath.
但是,如果您看到我的 application.properties 文件,则必需的属性出现在第一行。
还有一条日志行,我可以在应用程序启动的开头看到-
[main] trationDelegate$BeanPostProcessorChecker : Bean 'org.apache.camel.spring.boot.CamelAutoConfiguration' of type [org.apache.camel.spring.boot.CamelAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
这条日志行有什么暗示吗?
注意 - 一个有趣的事实是,昨晚完全相同的代码运行良好,只是重新启动了我的桌面,甚至没有改变一个字,现在它正在抛出异常。
【问题讨论】:
-
你能发布完整的例外吗?
-
您有 2 个 jms 连接工厂 - 即使您在其中一个上指定了主要的,注册表中仍然有 2 个。因此,您需要配置要使用的 jms Camel 组件。或者将您的代码更改为只有 1 个 jms 连接工厂 @Bean
-
非常感谢您的回答。注释掉一个连接工厂解决了这个问题。但是,它提出了几个问题 - 1. 它不应该按照 camel.component.amqp.connection-factory = jmsCachingConnectionFactory 中提到的连接工厂 bean 名称来查找吗?似乎 amqp-starter 没有使用 application.properties 中提到的 amqp 相关属性值。 2. 为什么我们需要将AMQPComponent注册为一个bean,这不应该是camel-amqp-starter的工作,使用application.properties中提供的属性来创建一个。签入工作代码。
标签: apache-camel spring-amqp spring-camel