mule数据库迭代数据和映射

时间:2015-06-17 21:51:19

标签: mule

我正在尝试从db中获取数据,为db中的每条记录进行映射并上传到云端。我做了以下方法:

从db中选择数据,从db中映射每条记录,然后上传。

骡子流:

<flow name="psi2sfdcFlow2" doc:name="psi2sfdcFlow2">
    <http:inbound-endpoint exchange-pattern="request-response" host="${hostname}" port="${port}" path="psi2sfdc" doc:name="HTTP"/>
    <flow-ref name="psi-update-Reading" doc:name="Flow Reference"/>
    <db:select config-ref="PostgreSQL" doc:name="PSI-Select">
        <db:parameterized-query><![CDATA[SELECT * FROM clients_int WHERE int_status = 'Reading']]></db:parameterized-query>
    </db:select>
    <object-to-string-transformer returnClass="java.lang.Object" doc:name="Object to String"/>
    <foreach doc:name="For Each">
        <invoke name="db2sfdcmapping" object-ref="createAccount" method="getPayloadData" methodArguments="#[message.payload]" doc:name="Invoke"/>
        <logger message="===Uplaod each row from db to cloud==== " level="INFO" doc:name="Logger"/>
    </foreach>
</flow>

错误:

ERROR 2015-06-17 14:24:56,735 [[psi2sfdc].connector.http.mule.default.receiver.02] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Object "java.lang.String" not of correct type. It must be of type "{interface java.lang.Iterable,interface java.util.Iterator,interface org.mule.routing.MessageSequence,interface java.util.Collection}" (java.lang.IllegalArgumentException). Message payload is of type: String
Type                  : org.mule.api.MessagingException
Code                  : MULE_ERROR--2
Payload               : [{phone=772-567-7461, pr_specialist_email=null, sic_code=5211, status=ACTIVE, address1=722 THIRD PLACE, fax=772-567-5054, naics_code=null, client_id=1630, bus_name=HBS INC, address2=null, contact=JANIE LUE, bus_name2=null, country=USA, termination_date=null, state=FL, zip=32962, client_fed_ids={59-1457513}, last_pay_date=2009-11-06, city=VERO BEACH, int_status=Reading}, {phone=713-668-9484, pr_specialist_email=null, sic_code=3714, status=DNE, address1=1140 AVE S, fax=null, naics_code=null, client_id=172, bus_name=FRANK'S SUPPLY OF DALLAS, address2=null, contact=PAM DEL BELLO, bus_name2=null, country=USA, termination_date=null, state=TX, zip=75050, client_fed_ids=null, last_pay_date=null, city=GRAND PRARIE, int_status=Reading}, {phone=(910)854-5000, pr_specialist_email=null, sic_code=null, status=DNE, address1=2720 IMMANUEL ROAD, fax=(910)854-0020, naics_code=null, client_id=2693, bus_name=SCIULLO INTERIOR SYSTEMS, address2=null, contact=Frank Sciullo, bus_name2=null, country=USA, te...
JavaDoc               : http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MessagingException.html
********************************************************************************
Exception stack is:
1. Object "java.lang.String" not of correct type. It must be of type "{interface java.lang.Iterable,interface java.util.Iterator,interface org.mule.routing.MessageSequence,interface java.util.Collection}" (java.lang.IllegalArgumentException)
  org.mule.util.collection.EventToMessageSequenceSplittingStrategy:57 (null)
2. Object "java.lang.String" not of correct type. It must be of type "{interface java.lang.Iterable,interface java.util.Iterator,interface org.mule.routing.MessageSequence,interface java.util.Collection}" (java.lang.IllegalArgumentException). Message payload is of type: String (org.mule.api.MessagingException)
  org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor:32 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MessagingException.html)
********************************************************************************
Root Exception stack trace:
java.lang.IllegalArgumentException: Object "java.lang.String" not of correct type. It must be of type "{interface java.lang.Iterable,interface java.util.Iterator,interface org.mule.routing.MessageSequence,interface java.util.Collection}"
    at org.mule.util.collection.EventToMessageSequenceSplittingStrategy.split(EventToMessageSequenceSplittingStrategy.java:57)
    at org.mule.util.collection.EventToMessageSequenceSplittingStrategy.split(EventToMessageSequenceSplittingStrategy.java:22)
    at org.mule.routing.CollectionSplitter.splitMessageIntoSequence(CollectionSplitter.java:29)
    at org.mule.routing.Foreach$CollectionMapSplitter.splitMessageIntoSequence(Foreach.java:245)
    at org.mule.routing.outbound.AbstractMessageSequenceSplitter.process(AbstractMessageSequenceSplitter.java:56)

Java文件:

public class Db2sfdc {

    public List<Map<String, Object>> getPayloadData(@Payload String src){
            System.out.println("Src-->"+src);
            Map<String, Object> sfdcFields = new HashMap<String, Object>();
            List<Map<String, Object>> accountList = new ArrayList<Map<String,Object>>();
            sfdcFields.put("ID", "001m000000In0p5AAB");
            sfdcFields.put("Current_WSE_Count__c", "20");
            accountList.add(sfdcFields);
            return accountList ;
    }
}

问题:

1)我是否需要使用object-to-string-transformer来从db获取数据并在java组件中进行映射?

2)如何从db上传/发送每一行的数据。因为我已经将数据转换为字符串转换器,所以当我使用每个时我都会遇到上述错误。我使用java组件来转换而不是Data Mapper。

修改

将对象移除到字符串变换器后,最后得到跟随错误,即在java映射之后:

ERROR 2015-06-17 15:02:09,954 [[psi2sfdc].connector.http.mule.default.receiver.02] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : java.io.NotSerializableException: org.postgresql.jdbc4.Jdbc4Array (org.apache.commons.lang.SerializationException). Message payload is of type: HttpResponse
Type                  : org.mule.execution.ResponseDispatchException
Code                  : MULE_ERROR--2
Payload               : org.mule.transport.http.HttpResponse@65b9ccef
JavaDoc               : http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/execution/ResponseDispatchException.html
********************************************************************************
Exception stack is:
1. org.postgresql.jdbc4.Jdbc4Array (java.io.NotSerializableException)
  java.io.ObjectOutputStream:1183 (null)
2. java.io.NotSerializableException: org.postgresql.jdbc4.Jdbc4Array (org.apache.commons.lang.SerializationException)
  org.apache.commons.lang.SerializationUtils:111 (null)
3. java.io.NotSerializableException: org.postgresql.jdbc4.Jdbc4Array (org.apache.commons.lang.SerializationException). Message payload is of type: HttpResponse (org.mule.execution.ResponseDispatchException)
  org.mule.transport.http.HttpMessageProcessTemplate:139 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/execution/ResponseDispatchException.html)
********************************************************************************
Root Exception stack trace:
java.io.NotSerializableException: org.postgresql.jdbc4.Jdbc4Array
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
    at org.apache.commons.collections.map.AbstractHashedMap.doWriteObject(AbstractHashedMap.java:1182)
    at org.mule.util.CaseInsensitiveHashMap.writeObject(CaseInsensitiveHashMap.java:142)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1495)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177)

**

修改:2

解决后,在以下更改之后:我在选择查询后转换为json对象:

<db:select config-ref="PostgreSQL" doc:name="PSI-Select">
   <db:parameterized-query><![CDATA[SELECT * FROM clients_int WHERE int_status = 'Reading']]></db:parameterized-query>
 </db:select>
 <foreach doc:name="For Each">
        <json:object-to-json-transformer returnClass="java.lang.Object" doc:name="Object to JSON"/>
        <logger message="===Main Flow #[payload]===" level="INFO" doc:name="Logger"/>
 </foreach>

1 个答案:

答案 0 :(得分:2)

删除object-to-string-transformer:它将行枚举转换为字符串,这会阻止for-each工作。

编辑:此外,由于入站HTTP端点为request-response,因此需要向客户端返回响应。 Mule将使用当前有效负载(Jdbc4Array实例)作为HTTP响应的有效负载,并且将失败。

您需要添加set-payload以将HTTP响应的正文设置为您想要返回给调用者的任何内容。使用http:response-builder设置响应头,缓存指令......

或者,您可以将HTTP入站端点切换为one-way。这将立即将OK返回给客户端,以及流程中发生的事情是成功还是失败。