2

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

这基本上就是我想要做的。我的问题在于我还是 Camel 的新手,我不确定应该如何/如何将路由中的信息传递到我的方法中,以便我可以使用由 JSON 值填充的 POJO .

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

EnrollResponse是 POJO 的名称。

任何帮助或指导/提示将不胜感激!

编辑/更新:

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

15:38:11,919 调试 [读取 #0 - JmsConsumer[Test.Central]] SendProcessor : >>>> Endpoint[bean://TriggeredSendBean?method=setup] Exchange[JmsMessage[JmsMessageID: ID:LT_John-51650-1363715888983- 3:2:1:1:1]]

15:38:12,396 INFO [读取 #0 - JmsConsumer[Test.Central]] ReflectionServiceFactoryBean:从 WSDL 创建服务 { http://testAPI.com/wsdl/partnerAPI }PartnerAPI:file:resources/META-INF/framework.wsdl

15:38:13,159 DEBUG [读取 #0 - JmsConsumer [Test.Central]] DefaultErrorHandler:(MessageId:topic_Test.Central_ID_LT_John-51650-1363715888983-3_2_1_1_1 在 ExchangeId 上的传递失败: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 错误 [读取 #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]]

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

}

4

1 回答 1

3

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

如果你有一本 Camel in Action 这本书,那么第 4 章将涵盖所有关于在 Camel 中使用 bean 的内容。

于 2013-03-20T08:24:55.623 回答