Bind aws sqs region camel

随声附和 提交于 2021-02-10 23:20:52

问题


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

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);

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>

And I got an error

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

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:

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>

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

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

Router:

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

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>

回答1:


The value of your region property is the String value "com.amazonaws.regions.Region". It is expecting an object of type com.amazonaws.regions.Region instead. So you need to reference an object of type Region instead of providing a String value.




回答2:


Assuming the question.

First:

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>

And create the reference

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

Second:

To run Apache Camel

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

Thats all!



来源:https://stackoverflow.com/questions/38798737/bind-aws-sqs-region-camel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!