Camel Route / ActiveMQ unmarshaling JSON并发送到方法

时间:2013-03-19 20:20:17

标签: json spring cxf activemq apache-camel

我有一个JSON响应被发布到我的activemq主题。我已经设置了我的路由来解组这个对POJO的响应。然后,我想逐步介绍3种不同的方法,这些方法利用了由JSON对象填充的POJO中的字段。

这基本上就是我想做的事情。我的问题在于我仍然是Camel的新手,我不确定应该将路径中的信息传递给我的方法是什么/如何使我可以使用已由JSON值填充的POJO

如果这有任何意义的话。我将发布我的Camel Route和下面的Java类,我将推迟pojo,因为它很简单,只有10个字段在int和字符串之间变化,有一些getter / setter方法。

EnrollResponse 是POJO的名称。

非常感谢任何帮助或指导/提示!

修改/更新:

在eclipse的控制台中,我从我的“设置”方法中获取了Sys,所以我知道它至少已经到了那一步。在我的日志中虽然这里是错误开始的地方:

15:38:11,919 DEBUG [read#0 - JmsConsumer [Test.Central]] SendProcessor:>>>>端点[bean:// TriggeredSendBean?method = setup] Exchange [JmsMessage [JmsMessageID:ID:LT_John-51650-1363715888983-3:2:1:1:1]]

15:38:12,396 INFO [read#0 - JmsConsumer [Test.Central]] ReflectionServiceFactoryBean:从WSDL创建服务{{{​​3}}} PartnerAPI:file:resources / META-INF / framework.wsdl

15:38:13,159 DEBUG [read#0 - JmsConsumer [Test.Central]] DefaultErrorHandler:ExchangeId上的消息传递失败(MessageId:topic_Test.Central_ID_LT_John-51650-1363715888983-3_2_1_1_1:ID-LT-John-52743-1363721881958 -0-1)。在传递尝试时:0捕获:org.apache.camel.CamelExecutionException:在交换机上执行期间发生异常:Exchange [JmsMessage [JmsMessageID:ID:LT_John-51650-1363715888983-3:2:1:1:1]]

15:38:13,160 ERROR [read#0 - JmsConsumer [Test.Central]] DefaultErrorHandler:ExchangeId上的消息传递失败(MessageId:topic_Test.Central_ID_LT_John-51650-1363715888983-3_2_1_1_1:ID-LT-John-52743-1363721881958 -0-1)。交付尝试后用尽:1捕获:org.apache.camel.CamelExecutionException:在交换机上执行期间发生异常:Exchange [JmsMessage [JmsMessageID:ID:LT_John-51650-1363715888983-3:2:1:1:1]] < / p>

org.apache.camel.CamelExecutionException:在交换机上执行期间发生异常:Exchange [JmsMessage [JmsMessageID:ID:LT_John-51650-1363715888983-3:2:1:1:1]]


<?xml version="1.0" encoding="UTF-8"?>
<!-- Configures the Camel Context-->

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:camel="http://camel.apache.org/schema/spring"
   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">

<!-- load properties --> 

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="file:backend.properties" /> 
</bean>
<bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
    <property name="location" value="file:backend.properties" /> 
</bean>

<bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="tcp://0.0.0.0:61616?useLocalHost=true" /> 
</bean>

<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
    <property name="maxConnections" value="8" /> 
    <property name="maximumActive" value="500" /> 
    <property name="connectionFactory" ref="jmsConnectionFactory" /> 
</bean>

<bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration">
    <property name="connectionFactory" ref="pooledConnectionFactory" /> 
    <property name="transacted" value="false" /> 
    <property name="concurrentConsumers" value="1" /> 
</bean>

<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
    <property name="configuration" ref="jmsConfig" /> 
</bean>

<!-- Custom Loaded Beans -->

<bean id="TriggeredSendBean" class="com.backend.trigger.ClientTest"/>
<bean id="EnrollResponse" class="com.testObjects.EnrollResponse" />

 <!-- camel configuration --> 

<camel:camelContext xmlns="http://camel.apache.org/schema/spring">
    <camel:dataFormats>
        <json id="UnmarshalToPOJO" library="Jackson" unmarshalTypeName="com.testObjects.EnrollResponse" />
     </camel:dataFormats>

    <camel:route id="genericMessageHandler" streamCache="true">
    <from uri="activemq:topic:Test.Central" /> 
    <unmarshal ref="UnmarshalToPOJO" /> 


    <to uri = "bean:TriggeredSendBean?method=setup" /> 
    <to uri = "bean:TriggeredSendBean?method=addSubscriberAllList" />
    <to uri = "bean:TriggeredSendBean?method=sendWelcomeEmail" />

    </camel:route>
</camel:camelContext>


public class ClientTest 
{
    static String user = null;
    static String password = null;
    static String customerKeyWelcomeEmailTest = null;
    static String validFromAddress = null;
    static String validFromName = null;

public static void setup(Exchange exchange) 
{   

    System.out.println("Exchange " + exchange.toString());

    //Retrieve settings from properties file
    Properties properties = getProperties();
    user = properties.getProperty("user");
    password = properties.getProperty("password");
    customerKeyWelcomeEmailTest = properties.getProperty("customerKeyWelcomeEmailTest");  
    validFromAddress = properties.getProperty("validFromAddress");
    validFromName = properties.getProperty("validFromName");


    System.out.println("user==> " + user);
    System.out.println("password==> " + password);
    System.out.println("customerKey==> " + customerKeyWelcomeEmailTest);
    System.out.println("validFromAddress==> " + validFromAddress);
    System.out.println("validFromName==> " + validFromName);

    //Create PartnerAPI stub.
    PartnerAPI service = new PartnerAPI();
    Soap stub = service.getSoap();
} 

/**
 * Adding specific subscriber to the "All subscribers" list in
 */

  private static void addSubscriberAllList(Soap stub, Exchange exchange, EnrollResponse enrollResponse) 
  {

      // Checking to see if enrollResponse is being populated
      System.out.println(enrollResponse.getEmail());
      System.out.println(enrollResponse.getFirstname());
      System.out.println(enrollResponse.getLastname());
      System.out.println(enrollResponse.getAcctid());
      System.out.println(enrollResponse.getCid());
      System.out.println(enrollResponse.getMyfridays());
      System.out.println(enrollResponse.getPhone());
      System.out.print(enrollResponse.getPoints());

      Subscriber subscriber = new Subscriber();

      subscriber.setEmailAddress(enrollResponse.getEmail());
      subscriber.setSubscriberKey(enrollResponse.getAcctid());  
      subscriber.setStatus(SubscriberStatus.ACTIVE);

      Attribute a1 = new Attribute();
      a1.setName("firstname");
      a1.setValue(enrollResponse.getFirstname());
      //Can add more attributes as needed

      Attribute[] AttributeLists = {a1};
      subscriber.getAttributes().addAll(Arrays.asList(AttributeLists));

      APIObject[] apiObjects = {subscriber};

      try
      {
          CreateRequest createRequest = new CreateRequest();
          createRequest.setOptions(new CreateOptions());
          createRequest.getObjects().addAll(Arrays.asList(apiObjects));

          CreateResponse createResponse = stub.create(createRequest);
          System.out.println("Subscriber created in all subscriber List: " + createResponse.getOverallStatus());
      } catch (Exception e)
      {
          e.printStackTrace();
      }
  } 

/**
 * Testing Triggered Send SPECIFIC SUBSCRIBER 
 */

private static void sendWelcomeEmail(Soap stub, Exchange exchange, EnrollResponse enrollResponse) 
{
    Subscriber[] testArray = new Subscriber[1];
    Owner ownerSubscriberValid = new Owner();


    System.out.println("****************** STARTING TRIGGERED SEND TEST ******************");

    //Specify TriggeredSendDefinition and initialize the TriggeredSend
    TriggeredSendDefinition triggeredSendDefinition = new TriggeredSendDefinition();
    triggeredSendDefinition.setCustomerKey(customerKeyWelcomeEmailTest); 
    TriggeredSend triggeredSend = new TriggeredSend();
    triggeredSend.setTriggeredSendDefinition(triggeredSendDefinition);

    //Create a valid Subscriber
    Subscriber subscriberValid = new Subscriber();
    subscriberValid.setEmailAddress(enrollResponse.getEmail()); 
    subscriberValid.setSubscriberKey(enrollResponse.getAcctid());
    ownerSubscriberValid.setFromAddress(validFromAddress);
    ownerSubscriberValid.setFromName(validFromName);
    subscriberValid.setOwner(ownerSubscriberValid);




    //Populate array of Subscribers
    testArray[0] = subscriberValid;
    java.util.List<Subscriber> list = Arrays.asList(testArray);        
    triggeredSend.getSubscribers().addAll( list ); 

    //Send the TriggeredSend using Create call
    try{
      CreateRequest createRequest = new CreateRequest();
      CreateOptions createOptions = new CreateOptions();
      createRequest.setOptions(createOptions);
      java.util.List<APIObject> listAPIObject = Arrays.asList(new APIObject[] {triggeredSend});
      createRequest.getObjects().addAll(listAPIObject);
      CreateResponse createResponse = stub.create(createRequest);

      System.out.println( "overall status message: " + createResponse.getOverallStatus() );
      java.util.List<CreateResult> listCreateResult = createResponse.getResults();
      CreateResult[] createResult = listCreateResult.toArray(new CreateResult[listCreateResult.size()]);

      for ( CreateResult status : createResult )
      {
          System.out.println("create status message: " + status.getStatusMessage());
          System.out.println("create status code: " + status.getStatusCode());
      }       

      //Validate the send and get failure information
      TriggeredSendCreateResult triggeredSendCreateResult = (TriggeredSendCreateResult)createResult[0];
      assert(triggeredSendCreateResult.getSubscriberFailures() != null);
      System.out.println("size of failures array: " + triggeredSendCreateResult.getSubscriberFailures().size());
      System.out.println("email address:     " + triggeredSendCreateResult.getSubscriberFailures().get(0).getSubscriber().getEmailAddress());
      System.out.println("error description: " + triggeredSendCreateResult.getSubscriberFailures().get(0).getErrorDescription());
      System.out.println("error code:        " + triggeredSendCreateResult.getSubscriberFailures().get(0).getErrorCode());
      assert( createResult != null );
      assert(createResult[0].getStatusMessage().equals("OK"));
      System.out.println( "****************** ENDING TRIGGERED SEND TEST ******************" );   
    } catch(Exception e) {
        e.printStackTrace();
    }
}    

/**
 * Retrieve properties file.
 * 
 */
public static Properties getProperties() 
{
    Properties properties = new Properties();
    try{
        FileInputStream fileInputStream = new FileInputStream("properties.xml");
        properties.loadFromXML(fileInputStream);
    } catch (Exception e){
        e.printStackTrace();
    }
    return properties;
} 

}

1 个答案:

答案 0 :(得分:3)

Camel具有强大的bean集成功能,在Camel网站上有很多关于此的文档,例如其中一些链接

如果你有一本Camel in Action书的副本,那么第4章将介绍有关使用Camel的bean的所有内容。