2016-08-05 3 views
1

Итак, я пытаюсь получить доступ к простой службе очереди с верблюдом apache.Bind aws sqel region camel

Подход Java DSL работает отлично, но я стараюсь работать с конфигурацией xml.

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

Код выше работает отлично, но я решил построить бобы.

<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] для свойства '' региона ; nested exception is java.lang.IllegalStateException: Невозможно преобразовать значение значения типа [java.lang.String] в требуемый тип [com.amazonaws.regions.Region] для свойства 'region': не найдено Редакторы или стратегия конверсии найдены

Я ничего не нашел о конфигурации sqs через Spring xml. И иногда я думаю, что верблюд apache устарел или никто не использует его с sqs. Кроме того, следующий шаг - подключить расширенную библиотеку SQS, которая отлично работает в реализации Java DSL, но я понятия не имею, как настроить очередь через xml.

UPD:

Благодаря @jbird, я решил эту проблему следующим образом:

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

Итак, я просто разобран мой default.properties файл, содержащий aws.key, aws.secret и региона.

И у меня возникла следующая проблема. Apache верблюд останавливается после загрузки маршрутизаторов и так далее.

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

И верблюд-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> 

ответ

0

Предполагая, что вопрос.

Первое:

Чтобы добавить свойство к объекту Я просто создать новый компонент

<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

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

Thats all!

1

Значение вашего region свойства является строковое значение "com.amazonaws.regions.Region". Он ожидает объект типа com.amazonaws.regions.Region. Поэтому вам нужно ссылаться на объект типа Region вместо предоставления значения String.

+0

Спасибо за повтор. Я разрешаю проблемы (проверьте раздел обновления), но у вас есть другая проблема. – Ascelhem

Смежные вопросы