[英]Camel, Amazon SQS - No type converter available to convert from type: java.lang.String to the required type: com.amazonaws.services.sqs.AmazonSQS
I am currently working on a Spring Application using Camel which is going to poll SQS as an entry point into the application (1st route). 我目前正在使用Camel开发一个Spring应用程序,该应用程序将轮询SQS作为该应用程序的入口点(第一种途径)。 I am successfully able to Achieve this using Spring's XML based Approach. 我可以使用Spring的基于XML的方法成功实现这一目标。
My AmazonSQSClient Bean: 我的AmazonSQSClient Bean:
<bean id="sqsClient" class="com.amazonaws.services.sqs.AmazonSQSClient">
<constructor-arg ref="sqsCredentialsProvider" />
<property name="endpoint" value="${aws.sqs.endpoint}" />
</bean>
My Camel Route: 我的骆驼路线:
<route id="pollMessages">
<from id="sqsEndpoint" uri="aws-sqs://{{queue.name}}?amazonSQSClient=#sqsClient&deleteAfterRead=false" />
<to uri="direct:readSQSMessage" />
</route>
Everything works as I want at this point with the above approach. 通过上述方法,此时一切都按我的意愿进行。
Now I am trying to migrate all my beans and Camel Configuration to Java Based Approach. 现在,我正在尝试将所有bean和Camel配置迁移到基于Java的方法。
I have created my Amazon SQS Client Bean as following: 我已经创建了我的Amazon SQS客户端Bean,如下所示:
@Bean
public AmazonSQS sqsClient(){
ClientConfiguration clientConfiguration = new ClientConfiguration();
AmazonSQSClient client = new AmazonSQSClient(sqsCredentialsProvider(), clientConfiguration);
client.setEndpoint(sqsEndpoint);
return client;
}
And, I am creating Camel route (snippet) looks like: 而且,我正在创建骆驼路线(摘要),如下所示:
@Bean
public CamelContext camelContext() throws Exception{
CamelContext camelContext = new DefaultCamelContext();
camelContext.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from("aws-sqs://"+fulfillmentQueueName+"?amazonSQSClient=#sqsClient&delay="+fulfillmentQueuePollInterval)
.to("direct:parseSQSMessage");
}
});
camelContext.start();
return camelContext;
}
However, I am getting errors using this approach: 但是,使用这种方法时出现错误:
java.lang.IllegalArgumentException: Could not find a suitable setter for property: amazonSQSClient as there isn't a setter method with same type: java.lang.String nor type conversion possible: No type converter available to convert from type: java.lang.String to the required type: com.amazonaws.services.sqs.AmazonSQS with value #sqsClient java.lang.IllegalArgumentException:无法找到适合属性的setter:amazonSQSClient,因为没有具有相同类型的setter方法:java.lang.String或类型转换都没有:没有类型转换器可用于从类型转换:java.lang .String到所需类型:com.amazonaws.services.sqs.AmazonSQS,值为#sqsClient
I read here how to construct a Java style Camel Route 我在这里阅读了如何构建Java风格的Camel Route
I read here that I need to bind the AWS Client to Registry (registry.bind) but I am not able to find a bind method on any Registry except JNDI 我在这里阅读到我需要将AWS Client绑定到注册表(registry.bind),但是我无法在除JNDI之外的任何注册表上找到绑定方法
I tried this as well: 我也尝试过这个:
SimpleRegistry registry = new SimpleRegistry();
registry.put("sqsClient", sqsClient());
CamelContext camelContext = new DefaultCamelContext(registry);
But got same error. 但是有同样的错误。
I searched a lot and tried reading up, plan to keep doing more but am unable to find any complete example doing what I need to do. 我进行了很多搜索,然后尝试阅读,打算继续做更多的事情,但找不到任何完整的示例来完成我需要做的事情。 Snippets are helping much here. 片段在这里大有帮助。
Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thanks 谢谢
For the amazonSQSClient=#sqsClient
parameter Camel try to find an entry in the Registry (ApplicationContextRegistry) where the key is sqsClient
. 对于amazonSQSClient=#sqsClient
参数Camel,请尝试在注册表(ApplicationContextRegistry)中找到项为sqsClient
。
In your first solution (where you specified the bean in xml) you set the id
of the bean and Camel registered the bean with id=sqsClient
in the ApplicationContextRegistry. 在第一个解决方案(在xml中指定了bean的位置)中,设置bean的id
,然后Camel在ApplicationContextRegistry中使用id=sqsClient
注册了bean。 But when you configured the bean in code you haven't set the id/name of it, so Camel didn't find it. 但是,当您在代码中配置bean时,您没有设置它的id /名称,因此Camel找不到它。 To make it work you have to set the name of the Bean! 要使其工作,您必须设置Bean的名称!
Solution: 解:
@Bean(name = "sqsClient")
public AmazonSQS sqsClient(){
ClientConfiguration clientConfiguration = new ClientConfiguration();
AmazonSQSClient client = new AmazonSQSClient(sqsCredentialsProvider(), clientConfiguration);
client.setEndpoint(sqsEndpoint);
return client;
}
try using SpringCamelContext
(it takes spring's ApplicationContext
as a contructor parameter) - it will look up your sqs client by bean name "sqsClient" in the application context. 尝试使用SpringCamelContext
(它将spring的ApplicationContext
作为构造参数)-它会在应用程序上下文中按bean名称“ sqsClient”查找您的sqs客户端。
if you're using spring boot and camel-spring then SpringCamelContext
will be created by default. 如果您使用的是Spring Boot和camel-spring,则默认情况下将创建SpringCamelContext
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.