Java 绑定aws sqs区域

Java 绑定aws sqs区域,java,xml,amazon-web-services,apache-camel,Java,Xml,Amazon Web Services,Apache Camel,因此,我尝试使用ApacheCamel访问简单队列服务 JavaDSL方法工作得很好,但我尝试使用xml配置 private AmazonSQS sqs; sqs = new AmazonSQSClient(credentials); Region sqsRegion = Region.getRegion(Regions.US_WEST_2); sqs.setRegion(sqsRegion); 上面的代码运行良好,但我决定构建bean <context:property-placeho

因此,我尝试使用ApacheCamel访问简单队列服务

JavaDSL方法工作得很好,但我尝试使用xml配置

private AmazonSQS sqs;
sqs = new AmazonSQSClient(credentials);
Region sqsRegion = Region.getRegion(Regions.US_WEST_2);
sqs.setRegion(sqsRegion);
上面的代码运行良好,但我决定构建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>
路由器:

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");
    }
}
以及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>

${aws.region}

您的
region
属性的值是字符串值
“com.amazonaws.regions.region”
。它需要一个类型为
com.amazonaws.regions.Region
的对象。因此,您需要引用类型为
Region
的对象,而不是提供字符串值。

假设这个问题

第一:

要向对象添加属性,我只需创建新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>

就这些

谢谢重播。我解决了这些问题(查看upd部分),但遇到了另一个问题。我尝试这样做时遇到了一个异常:java.lang.exception:无法转换值org.springframework.beans.factory.config。MethodInvokingFactoryBean@521b30d2键入com.amazonaws.regions.Region
    <?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 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"/>
Main main = new Main();
        main.setApplicationContextUri("/META-INF/spring/camel-contex.xml");
        main.run(args);