绑定aws sqs区域骆驼 [英] Bind aws sqs region camel

查看:33
本文介绍了绑定aws sqs区域骆驼的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试使用 apache camel 访问简单的队列服务.

So I'm trying to access to the simple queue service with apache camel.

Java DSL 方法工作正常,但我尝试使用 xml 配置.

Java DSL approach works fine, but I try to work with xml configuration.

private AmazonSQS sqs;
sqs = new AmazonSQSClient(credentials);
Region sqsRegion = Region.getRegion(Regions.US_WEST_2);
sqs.setRegion(sqsRegion);

上面的代码运行良好,但我决定构建 bean.

The code above works fine, but i decided to build bean.

<context:property-placeholder  location="classpath:/default.properties" />
    <bean name="sqsClient" class="com.amazonaws.services.sqs.AmazonSQSClient">
        <constructor-arg>
            <bean class="com.amazonaws.auth.BasicAWSCredentials">
                <constructor-arg value="${access.key}"/>
                <constructor-arg value="${secret.key}"/>
            </bean>
        </constructor-arg>
        <property name="region" value="com.amazonaws.regions.Region"/>
    </bean>

我有一个错误

无法将 [java.lang.String] 类型的属性值转换为属性区域"的必需类型 [com.amazonaws.regions.Region];嵌套异常是 java.lang.IllegalStateException:无法转换[java.lang.String] 类型的值到所需类型[com.amazonaws.regions.Region] 对于财产区域":没有匹配找到编辑器或转换策略

Failed to convert property value of type [java.lang.String] to required type [com.amazonaws.regions.Region] for property 'region'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.amazonaws.regions.Region] for property 'region': no matching editors or conversion strategy found

我没有通过 Spring xml 找到任何关于配置 sqs 的信息.有时我认为 apache camel 已经过时了,或者没有人将它与 sqs 一起使用.此外,下一步是连接在 Java DSL 实现中运行良好的扩展 SQS 库,但我不知道如何通过 xml 配置队列.

I haven't found anything about configuration sqs via Spring xml. And sometimes I think that apache camel is outdated or nobody uses it with sqs. Moreover the next step is to connect Extended SQS library that works fine in Java DSL implementation, but I have no idea how to configure queue via xml.

UPD:

感谢@jbird,我是这样解决的:

Thanks to @jbird, I solved the problem in this way:

<context:property-placeholder  location="classpath:/default.properties" />
    <bean id="awsRegion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass" value="com.amazonaws.regions.RegionUtils"/>
        <property name="targetMethod" value="getRegion"/>
        <property name="arguments">
            <list>
                <value>${aws.region}</value>
            </list>
        </property>
    </bean>

    <bean name="sqsClient" class="com.amazonaws.services.sqs.AmazonSQSClient">
        <constructor-arg>
            <bean class="com.amazonaws.auth.BasicAWSCredentials">
                <constructor-arg value="${access.key}"/>
                <constructor-arg value="${secret.key}"/>
            </bean>
        </constructor-arg>
        <property name="region" ref="awsRegion"/>
    </bean>

所以,我只是解析了包含 aws.key、aws.secret 和区域设置的 default.properties 文件.

So, I just parsed my default.properties file that contains aws.key, aws.secret and region settings.

我遇到了下一个问题.Apache camel 在加载路由器等后停止运行.

And I got the next problem. Apache camel stop running after loading routers and so on.

[                          main] SpringCamelContext             INFO  Route: route1 started and consuming from: Endpoint[aws-sqs://queue?amazonSQSClient=%23sqsClient]
[                          main] SpringCamelContext             INFO  Total 1 routes, of which 1 are started.
[                          main] SpringCamelContext             INFO  Apache Camel 2.17.2 (CamelContext: camel-1) started in 6.105 seconds

Process finished with exit code 0

路由器:

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class MyRouteBuilder extends RouteBuilder {

    public void configure() {
        from("aws-sqs://queue?amazonSQSClient=#sqsClient")
                .log("We have a message! ${body}")
                .to("file:target/output?fileName=login-message-${date:now:MMDDyy-HHmmss}.json");
    }
}

还有骆驼上下文.xml

And the camel-context.xml

    <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
       ">

    <context:property-placeholder  location="classpath:/default.properties" />

    <bean id="awsRegion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass" value="com.amazonaws.regions.RegionUtils"/>
        <property name="targetMethod" value="getRegion"/>
        <property name="arguments">
            <list>
                <value>${aws.region}</value>
            </list>
        </property>
    </bean>

    <bean name="sqsClient" class="com.amazonaws.services.sqs.AmazonSQSClient">
        <constructor-arg>
            <bean class="com.amazonaws.auth.BasicAWSCredentials">
                <constructor-arg value="${access.key}"/>
                <constructor-arg value="${secret.key}"/>
            </bean>
        </constructor-arg>
        <property name="region" ref="awsRegion"/>
    </bean>

    <!-- enable Spring @Component scan -->
    <context:component-scan base-package="com.test.router"/>

    <camelContext xmlns="http://camel.apache.org/schema/spring">

        <contextScan/>
    </camelContext>

</beans>

推荐答案

假设问题.

第一:

要向对象添加属性,我只需创建新 bean

To add property to your object I simply create new bean

 <bean id="awsRegion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass" value="com.amazonaws.regions.RegionUtils"/>
        <property name="targetMethod" value="getRegion"/>
        <property name="arguments">
            <list>
                <value>${aws.region}</value>
            </list>
        </property>
    </bean>

并创建引用

<property name="region" ref="awsRegion"/>

第二:

运行 Apache Camel

To run Apache Camel

Main main = new Main();
        main.setApplicationContextUri("/META-INF/spring/camel-contex.xml");
        main.run(args);

仅此而已!

这篇关于绑定aws sqs区域骆驼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆