0

我想从数据库中选择记录并在mule.

我的方法是使用 Spring bean 获取记录并将其传递给hashmap然后返回hashmap并将其保存在会话变量中。

目前收到此错误:

Message               : Execution of the expression "message.payload.getServiceCodes()" failed. (org.mule.api.expression.ExpressionRuntimeException). Message payload is of type: HashMap
Code                  : MULE_ERROR--2

它有更好的方法吗?或者我做错了什么?

我的配置 XML

    <?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:quartz="http://www.mulesoft.org/schema/mule/quartz" xmlns:jdbc-ee="http://www.mulesoft.org/schema/mule/ee/jdbc" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:xm="http://www.mulesoft.org/schema/mule/xml"  xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:jms="http://www.mulesoft.org/schema/mule/jms" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/jdbc http://www.mulesoft.org/schema/mule/ee/jdbc/current/mule-jdbc-ee.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd
http://www.mulesoft.org/schema/mule/quartz http://www.mulesoft.org/schema/mule/quartz/current/mule-quartz.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
    <jms:activemq-connector name="Active_MQ" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"></jms:activemq-connector>
    <xm:jaxb-context name="myJaxb" doc:name="myJaxb" packageNames="com.test.jaxb"></xm:jaxb-context>
    <spring:beans> 
        <spring:bean id="springDataSource" name="Bean" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
            <spring:property name="url" value="jdbc:mysql://localhost:3306/muledb"></spring:property>  
            <spring:property name="maxActive" value="30"></spring:property>  
            <spring:property name="removeAbandoned" value="true"></spring:property>  
            <spring:property name="driverClassName" value="com.mysql.jdbc.Driver"></spring:property>  
            <spring:property name="initialSize" value="20"></spring:property>  
            <spring:property name="username" value="root"></spring:property>  
        </spring:bean>  
    </spring:beans>
    <quartz:connector name="quartzConnector" doc:name="Quartz"> 
        <quartz:factory-property key="org.quartz.scheduler.instanceName" value="MuleScheduler1"></quartz:factory-property>  
        <quartz:factory-property key="org.quartz.threadPool.class" value="org.quartz.simpl.SimpleThreadPool"></quartz:factory-property>  
        <quartz:factory-property key="org.quartz.threadPool.threadCount" value="3"></quartz:factory-property>  
        <quartz:factory-property key="org.quartz.scheduler.rmi.proxy" value="false"></quartz:factory-property>  
        <quartz:factory-property key="org.quartz.scheduler.rmi.export" value="false"></quartz:factory-property>  
        <quartz:factory-property key="org.quartz.jobStore.class" value="org.quartz.simpl.RAMJobStore"></quartz:factory-property>  
    </quartz:connector>
    <jdbc-ee:mysql-data-source name="MySQL_Data_Source" user="root" password="" url="jdbc:mysql://localhost:3306/muledb" transactionIsolation="UNSPECIFIED" doc:name="MySQL Data Source"></jdbc-ee:mysql-data-source>
    <jdbc-ee:connector name="Database" dataSource-ref="MySQL_Data_Source" validateConnections="true" transactionPerMessage="false" queryTimeout="-1" pollingFrequency="0" doc:name="Database"></jdbc-ee:connector>
    <!-- <flow name="JMSMessageFlow1" doc:name="JMSMessageFlow1" > 

        <jdbc-ee:inbound-endpoint queryKey="FirstUpdate" queryTimeout="-1" connector-ref="Database" doc:name="Database" pollingFrequency="0">
            <jdbc-ee:query key="FirstUpdate" value="SELECT * FROM gcp_txn_log WHERE PROCESSED= 'Y'"></jdbc-ee:query> 
            <jdbc-ee:query key="FirstUpdate.ack" value="update gcp_txn_log set PROCESSED='N' where service_id = #[map-payload:service_id]"></jdbc-ee:query>

        </jdbc-ee:inbound-endpoint>

        <logger level="INFO" doc:name="Logger"/>  
      </flow> -->
    <!-- <flow name="JMSMessageFlow2" doc:name="JMSMessageFlow2"> 
        <jdbc-ee:inbound-endpoint queryKey="SelectAll" queryTimeout="-1" connector-ref="Database" doc:name="Database" pollingFrequency="0"> 
            <jdbc-ee:transaction action="ALWAYS_BEGIN"></jdbc-ee:transaction>  

            <jdbc-ee:query key="SelectAll" value="SELECT * FROM gcp_txn_log WHERE PROCESSED= 'N'"></jdbc-ee:query>  
            <jdbc-ee:query key="SelectAll.ack" value="update gcp_txn_log set PROCESSED='Y'  where service_id = #[map-payload:service_id] "></jdbc-ee:query>  
        </jdbc-ee:inbound-endpoint>  
        <xm:object-to-xml-transformer doc:name="Object to XML"></xm:object-to-xml-transformer>  
        <file:outbound-endpoint path="C:\Users\FASYL\Downloads" outputPattern="test#[function:datestamp:dd-MM-yy]_#[function:systime].xml" responseTimeout="10000" doc:name="File"></file:outbound-endpoint>  
     <jdbc-ee:outbound-endpoint queryKey="FirstUpdate" queryTimeout="-1" connector-ref="Database" doc:name="Database">

            <jdbc-ee:query key="FirstUpdate" value="update gcp_txn_log set PROCESSED = 'N' WHERE PROCESSED = 'Y'"></jdbc-ee:query>

        </jdbc-ee:outbound-endpoint> 
    </flow> -->
    <flow name="JMSMessageFlow3" doc:name="JMSMessageFlow3"> 
        <quartz:inbound-endpoint jobName="job2" repeatInterval="1" repeatCount="0" responseTimeout="10000" doc:name="Quartz" connector-ref="quartzConnector">
            <quartz:event-generator-job>
                <quartz:payload>Setting Session Variable</quartz:payload>
            </quartz:event-generator-job>

        </quartz:inbound-endpoint>  
        <component class="com.test.SchedulerComponent" doc:name="Java"></component>  
        <component doc:name="Java">  
            <singleton-object class="com.test.ServiceDAO"> 
                <property key="dataSource" value-ref="springDataSource" value="null"/> 
            </singleton-object> 
        </component>
        <message-properties-transformer scope="session" doc:name="Message Properties">
            <add-message-property key="serviceDAO" value="#[message.payload]"/>
        </message-properties-transformer>
        <logger level="INFO" doc:name="Logger" message="#[sessionVars['serviceDAO']['1001']]"/>
    </flow>
    <!-- <flow name="JMSMessageFlow1" doc:name="JMSMessageFlow1">

    </flow> -->
    <flow name="JMSMessageFlow4" doc:name="JMSMessageFlow4">
        <jms:inbound-endpoint queue="StudioIns" connector-ref="Active_MQ" doc:name="JMS"/>
        <xm:jaxb-xml-to-object-transformer jaxbContext-ref="myJaxb" returnClass="com.test.jaxb.MsgContent"></xm:jaxb-xml-to-object-transformer>
        <choice doc:name="Choice">
            <when expression="payload.getMsgBody().getValue() == 'fundstransfer'" >
                <pooled-component doc:name="Java">  
                    <prototype-object class="com.test.PersonComponent"> 
                        <property key="dataSource" value-ref="springDataSource" value="null"/> 
                    </prototype-object> 
                </pooled-component>
            </when>
            <when expression="#[sessionVars['serviceDAO']['1001'] == 1] ">
                <pooled-component doc:name="Java">  
                    <prototype-object class="com.test.SecondPersonComponent"> 
                        <property key="dataSource" value-ref="springDataSource" value="null"/> 
                    </prototype-object> 
                </pooled-component>
            </when>
        </choice>
        <logger message="#[sessionVars['serviceDAO']]" level="INFO" doc:name="Logger"/>
        <file:outbound-endpoint path="D:\Documents\MuleStudio\workspace\jms_amq\bin\com\test" outputPattern="new.txt" responseTimeout="10000" doc:name="File"/>
    </flow>
</mule>
4

1 回答 1

1

该错误表明您的有效负载是 HashMap:“消息有效负载的类型为:HashMap”。所以表达式:message.payload.getServiceCodes() 将不起作用,因为 HashMap 没有名为“getServiceCodes()”的方法。如果您想要访问该 HashMap 的元素,您可以通过几种方式使用 MEL(Mule Expression Language - http://www.mulesoft.org/documentation/display/current/Mule+Expression+Language+MEL)。

通过标准的 java 方法调用,例如:

payload.get('mykey')

或者您可以使用 MEL 简写,例如:

payload['mykey']或者payload.mykey

关于会话变量的另一点,您可以通过“set-session-variable”消息处理器将其存储在会话变量中,并以类似的方式访问元素。您可以使用“sessionVars”而不是“payload”来引用它,这一次它将是一张地图,所以:

sessionVars.mymap.mykeysessionVars['mymap']['mykey']等等。

于 2013-08-21T11:07:17.557 回答