0

当我向 mule 服务发送请求时,我想将一些信息保存到数据库中。对于这个问题:我在 xml 文件中写了下面的配置:

    <mule xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:mule-ss="http://www.mulesoft.org/schema/mule/spring-security"
xmlns:ss="http://www.springframework.org/schema/security" xmlns:jdbc="http://www.mulesoft.org/schema/mule/jdbc"
xmlns:spring="http://www.springframework.org/schema/beans" version="CE-3.3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd 
    http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd 
    http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd 
    http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd 
    http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd 
    http://www.mulesoft.org/schema/mule/spring-security http://www.mulesoft.org/schema/mule/spring-security/3.3/mule-spring-security.xsd 
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd 
    http://www.mulesoft.org/schema/mule/jdbc http://www.mulesoft.org/schema/mule/jdbc/current/mule-jdbc.xsd ">



<spring:beans>
    <spring:bean id="Initializer" name="Initializer" class="org.mule.example.scripting.IpClient" doc:name="Bean"/>
</spring:beans>
<notifications>
    <notification event="COMPONENT-MESSAGE"/>
    <notification-listener ref="Initializer"/>
</notifications>



<configuration doc:name="Configuration">
    <expression-language>
        <global-functions>
            def parseIp(fullIp) {
            return
            fullIp.substring(fullIp.indexOf('/') + 1, fullIp.indexOf(':'))
            }
    </global-functions>
    </expression-language>
</configuration>


<http:connector name="httpConnector" doc:name="HTTP\HTTPS">
    <service-overrides sessionHandler="org.mule.session.NullSessionHandler" />
</http:connector>

<mule-ss:security-manager>
    <mule-ss:delegate-security-provider
        name="memory-dao" delegate-ref="authenticationManager" />
</mule-ss:security-manager>
<spring:beans>
    <ss:authentication-manager alias="authenticationManager">
        <ss:authentication-provider>
            <ss:user-service id="userService">
                <ss:user name="weather" password="weather" authorities="ROLE_ADMIN" />
            </ss:user-service>
        </ss:authentication-provider>
    </ss:authentication-manager>
</spring:beans>

<flow name="Serive_test" doc:name="Serive_test">
    <http:inbound-endpoint host="localhost" port="8089"
        path="service/local-weather" exchange-pattern="request-response"
        doc:name="HTTP">
        <mule-ss:http-security-filter realm="mule-realm" />
    </http:inbound-endpoint>

    <async doc:name="Async">
        <set-variable variableName="remoteClientAddress"
            value="#[parseIp(message.inboundProperties['MULE_REMOTE_CLIENT_ADDRESS'])]"
            doc:name="Variable" />

        <message-properties-transformer
            doc:name="myproperty" scope="session">
            <add-message-property key="message.payload.remoteClientAddress"
                value="#[parseIp(message.inboundProperties['MULE_REMOTE_CLIENT_ADDRESS'])]" />

        </message-properties-transformer>

        <component doc:name="classTest" class="org.mule.example.scripting.IpClient" />
    </async>
    <cxf:proxy-service service="Weather" doc:name="Weather_webservice"
        wsdlLocation="http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl" namespace="http://ws.cdyne.com/WeatherWS/"
        payload="envelope"></cxf:proxy-service>
    <copy-properties propertyName="SOAPAction" doc:name="Property"></copy-properties>
    <cxf:proxy-client doc:name="Weather_webservice"
        payload="envelope" />
    <outbound-endpoint address="http://wsf.cdyne.com/WeatherWS/Weather.asmx"
        exchange-pattern="request-response" doc:name="HTTP"></outbound-endpoint>

</flow>

和 IpClient 类:

public class IpClient  implements Callable,ModelNotificationListener<ModelNotification>  {


@Override
public void onNotification(ModelNotification notification) {
    // TODO Auto-generated method stub
    System.out.println("Notification order event: " + notification.getActionName() );

    if(notification.getAction() == ModelNotification.MODEL_DISPOSED || notification.getAction() == ModelNotification.MODEL_STOPPED){

        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();

        ConnectDB c = new ConnectDB("localhost:3306", "accounting", "root", "");

        String sql = " INSERT INTO weather (ip, date, CurrentState) VALUES (?,?,?) ";

        PreparedStatement ps = null;
        try {
            ps = c.getConnnection().prepareStatement(sql);
            ps.setString(1, "127.0.0.1");
            ps.setString(2, dateFormat.format(date).toString());                
            ps.setString(3, notification.getActionName());                                 
            ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            c.close();
        }           

    }
}


@Override
public Object onCall(MuleEventContext eventContext) throws Exception {


    MuleMessage msg = eventContext.getMessage();    

    String remClient = msg.getProperty("remoteClientAddress", PropertyScope.INVOCATION);


    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();

    ConnectDB c = new ConnectDB("localhost:3306", "accounting", "root", "");

    String sql = " INSERT INTO weather (ip, date, CurrentState) VALUES (?,?,?) ";

    PreparedStatement ps = c.getConnnection().prepareStatement(sql);

    ps.setString(1, remClient);

    ps.setString(2, dateFormat.format(date).toString());

    ps.setString(3, msg.getMuleContext().getLifecycleManager().getCurrentPhase());

    ps.executeUpdate();

    c.close();

    return msg.getPayload();

}

}

如果我开始运行服务,这个程序就可以正常工作。例如,我的服务在突然被处置之前运行(例如我的 wsdl(http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl不起作用))。我的日志程序将所有记录保存在启动状态,并且在处理后它的工作方式类似。如果我停止我的服务并再次运行它,它会正常工作并将所有记录保存在处置模式下。我不知道如何更改它在运行时正确保存的程序。

4

1 回答 1

0

(错误命名的)Initializerbean 不用作<component>配置中的任何地方,因此它的onCall方法永远不会被调用。

如果您的意图是:

<notification event="COMPONENT-MESSAGE"/>

是让Initializer每个组件调用都被调用,然后你必须让它实现ComponentMessageNotificationListener(就像你让它实现一样ModelNotificationListener)。

于 2013-05-08T16:42:16.017 回答