【问题标题】:Camel, Amazon SQS - No type converter available to convert from type: java.lang.String to the required type: com.amazonaws.services.sqs.AmazonSQSCamel,Amazon SQS - 没有可用于将类型:java.lang.String 转换为所需类型:com.amazonaws.services.sqs.AmazonSQS 的类型转换器
【发布时间】:2017-04-18 00:54:04
【问题描述】:

我目前正在使用 Camel 开发一个 Spring 应用程序,它将轮询 SQS 作为应用程序的入口点(第一条路线)。 我可以使用 Spring 的基于 XML 的方法成功地实现这一目标。

我的 AmazonSQSClient Bean:

<bean id="sqsClient" class="com.amazonaws.services.sqs.AmazonSQSClient">
    <constructor-arg ref="sqsCredentialsProvider" />
    <property name="endpoint" value="${aws.sqs.endpoint}" />
</bean>

我的骆驼之路:

<route id="pollMessages">
    <from id="sqsEndpoint" uri="aws-sqs://{{queue.name}}?amazonSQSClient=#sqsClient&amp;deleteAfterRead=false" />
    <to uri="direct:readSQSMessage" />
</route>

在这一点上,使用上述方法一切正常。

现在我正在尝试将我所有的 bean 和 Camel 配置迁移到基于 Java 的方法。

我创建了我的 Amazon SQS 客户端 Bean,如下所示:

@Bean
public AmazonSQS sqsClient(){
    ClientConfiguration clientConfiguration = new ClientConfiguration();
    AmazonSQSClient client = new AmazonSQSClient(sqsCredentialsProvider(), clientConfiguration);
    client.setEndpoint(sqsEndpoint);

    return client;
}

而且,我正在创建的骆驼路线(sn-p)看起来像:

@Bean
public CamelContext camelContext() throws Exception{
    CamelContext camelContext = new DefaultCamelContext();

    camelContext.addRoutes(new RouteBuilder() {
        @Override
        public void configure() {

            from("aws-sqs://"+fulfillmentQueueName+"?amazonSQSClient=#sqsClient&amp;delay="+fulfillmentQueuePollInterval)
            .to("direct:parseSQSMessage");

            }
    });

    camelContext.start();
    return camelContext;
}

但是,我在使用这种方法时遇到了错误:

java.lang.IllegalArgumentException:找不到合适的属性设置器:amazonSQSClient,因为没有相同类型的设置器方法:java.lang.String,也无法进行类型转换:没有可用于从类型转换的类型转换器: java.lang.String 到所需类型:com.amazonaws.services.sqs.AmazonSQS,值为#sqsClient

我阅读了here如何构建Java风格的骆驼路线

我读到 here 我需要将 AWS 客户端绑定到注册表 (registry.bind),但我无法在除 JNDI 之外的任何注册表上找到绑定方法

我也试过了:

SimpleRegistry registry = new SimpleRegistry();
registry.put("sqsClient", sqsClient());

CamelContext camelContext = new DefaultCamelContext(registry);

但遇到同样的错误。

我进行了很多搜索并尝试阅读,计划继续做更多但我无法找到任何完整的示例来做我需要做的事情。片段在这里很有帮助。

非常感谢任何帮助。谢谢

【问题讨论】:

    标签: java spring amazon-web-services apache-camel amazon-sqs


    【解决方案1】:

    对于 amazonSQSClient=#sqsClient 参数 Camel 尝试在注册表 (ApplicationContextRegistry) 中查找键为 sqsClient 的条目。

    在您的第一个解决方案(在 xml 中指定 bean)中,您设置了 bean 的 id,Camel 在 ApplicationContextRegistry 中使用 id=sqsClient 注册了 bean。但是当您在代码中配置 bean 时,您还没有设置它的 id/name,所以 Camel 没有找到它。要使其工作,您必须设置 Bean 的名称!

    解决方案:

    @Bean(name = "sqsClient")
    public AmazonSQS sqsClient(){
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        AmazonSQSClient client = new AmazonSQSClient(sqsCredentialsProvider(), clientConfiguration);
        client.setEndpoint(sqsEndpoint);
        return client;
    }
    

    【讨论】:

      【解决方案2】:

      尝试使用SpringCamelContext(它将spring的ApplicationContext作为构造函数参数)-它将在应用程序上下文中通过bean名称“sqsClient”查找您的sqs客户端。

      如果您使用的是 spring boot 和 camel-spring,则默认创建 SpringCamelContext

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-02
        • 2020-07-02
        • 2017-01-28
        • 2020-07-23
        • 2019-08-23
        • 2019-11-05
        • 2020-09-07
        相关资源
        最近更新 更多