Mule-拦截所有流量

Mule-拦截所有流量,mule,Mule,对于Mule,是否有办法拦截所有流量?我的用例是查看流名称,如果流名称与http请求中的特定参数匹配,我需要采取一些措施。我不想为每个流编写拦截器配置。那太单调了,也不理想 我想要一个springaop横切应用程序中的所有流,然后编写一个java类,该类可以在流执行之前由Mule调用。我会在那里检查流名称,如果它与请求中的参数匹配,我可以采取行动,否则,我将不需要做任何事情 我对骡子不熟悉。有人能给我指出正确的方向吗?我想向您推荐一种满足要求的方法 定义一个以http作为入口点的公共流,然后定义

对于Mule,是否有办法拦截所有流量?我的用例是查看流名称,如果流名称与http请求中的特定参数匹配,我需要采取一些措施。我不想为每个流编写拦截器配置。那太单调了,也不理想

我想要一个springaop横切应用程序中的所有流,然后编写一个java类,该类可以在流执行之前由Mule调用。我会在那里检查流名称,如果它与请求中的参数匹配,我可以采取行动,否则,我将不需要做任何事情


我对骡子不熟悉。有人能给我指出正确的方向吗?

我想向您推荐一种满足要求的方法


定义一个以http作为入口点的公共流,然后定义一个选择路由器,该路由器评估所考虑的参数。因此,根据条件评估,使用flow-ref调用相应的流。

所建议的方法可能有效

但是,根据您对场景的描述,我觉得您最好使用APIkit

当然,它可能有很多您需要的特性,但简而言之,基于RESTAPI定义(这是http请求的一部分),它将自动将其路由到实现该部分API的流

最酷的是,你只需要编写RAML,工作室就会为你生成流


希望这有帮助;)

为什么不使用简单的“选择路由器”。根据您在输入端设置的请求参数,可以根据条件调用适当的流

因此,如果您想基于http url控制流,您可以使用公共流并使用如下选项:-

 <http:listener-config name="HTTP_Listener_Configuration" protocol="HTTP" host="0.0.0.0" port="${port}" basePath="mainpath" doc:name="HTTP Listener Configuration"/>
   <flow  name="Entry_Point">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/*" doc:name="HTTP" parseRequest="false">
            <http:error-response-builder statusCode="404" reasonPhrase="Path Not Found"/>
        </http:listener>

        <set-variable variableName="requestUri" value="#[message.inboundProperties['http.request.uri'].replace('/mainpath/','')]" doc:name="Variable"/>
        <logger level="INFO" message="PATH :-> #[requestUri]" doc:name="Logger" />

        <choice doc:name="Route Message By Path">
            <when
                expression="#[regex('service/.+', requestUri, java.util.regex.Pattern.CASE_INSENSITIVE)]">
                <flow-ref name="flow1" doc:name="flow1" />
            </when>

            <when
                expression="#[regex('audit/.+', requestUri, java.util.regex.Pattern.CASE_INSENSITIVE)]">
                <flow-ref name="flow2" doc:name="flow2" />
            </when>
            //////////// so on

////////////诸如此类

但是,如果您想基于HTTP请求控制流,那么根据请求,您可以使用XPATH(对于XML请求)或JSON来对象transformer(对于JSON请求)并提取元素值,基于元素值,您可以使用Choice router将流路由到流,您可以使用服务器通知来执行此操作,我已经实现了一种类似的方法,可以在调用HTTP端点之前/之后侦听

代码如下:

    <spring:beans>
        <spring:bean name="MuleMessageProcessorNoticationBean" class="com.alexfrndz.MPLogger"></spring:bean>
    </spring:beans>

    package com.alexfrndz;

    import org.mule.api.MuleEvent;
    import org.mule.api.MuleMessage;
    import org.mule.api.context.notification.MessageProcessorNotificationListener;
    import org.mule.api.processor.LoggerMessageProcessor; 
    import org.mule.api.processor.MessageProcessor;
    import org.mule.context.notification.MessageProcessorNotification;
    import org.mule.endpoint.AbstractEndpoint;
    import org.mule.endpoint.DefaultOutboundEndpoint;
    import org.mule.processor.AsyncDelegateMessageProcessor;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.apache.log4j.Logger;

    public class MPLogger implements        MessageProcessorNotificationListener<MessageProcessorNotification> {
    private Logger log = Logger.getLogger(MPLogger.class);

    private  long startTime = System.currentTimeMillis();
    private  long endTime = System.currentTimeMillis();


    @Override
    public void onNotification(MessageProcessorNotification m) {
        MuleEvent ev = m.getSource();
        MuleMessage msg = ev.getMessage();
        Object payload = msg.getPayload();
        String ref = payload != null ? payload.toString() : "null";

        MessageProcessor proc = m.getProcessor();

        boolean inc = true;

        if (m.getAction() == MessageProcessorNotification.MESSAGE_PROCESSOR_PRE_INVOKE) {
               startTime = System.currentTimeMillis();
        }

        if (m.getAction() == MessageProcessorNotification.MESSAGE_PROCESSOR_POST_INVOKE) {
              long executionTime = System.currentTimeMillis() - startTime;
              AbstractEndpoint ep = (AbstractEndpoint) proc;
              log.info("Http call to : "+ ep.getName() + " took " + executionTime + "ms response time");
        }

        boolean outgoing = proc instanceof DefaultOutboundEndpoint;


        if (inc) {
          if (outgoing) {
            AbstractEndpoint ep = (AbstractEndpoint) proc;
            log.warn(msg.getMessageRootId() + " [OUTBOUND] Message " + ref + " -> " + ep.getEndpointURI());
            ep.getResponseTimeout();

          } else {
            log.warn(msg.getMessageRootId() + " [PROCESSING] Message " + ref + " <> " + proc.getClass().getSimpleName());
          }
        }
      }

}

包com.alexfrndz;
导入org.mule.api.MuleEvent;
导入org.mule.api.MuleMessage;
导入org.mule.api.context.notification.MessageProcessorNotificationListener;
导入org.mule.api.processor.LoggerMessageProcessor;
导入org.mule.api.processor.MessageProcessor;
导入org.mule.context.notification.MessageProcessorNotification;
导入org.mule.endpoint.AbstractEndpoint;
导入org.mule.endpoint.DefaultOutboundEndpoint;
导入org.mule.processor.AsyncDelegateMessageProcessor;
导入org.apache.commons.logging.Log;
导入org.apache.commons.logging.LogFactory;
导入org.apache.log4j.Logger;
公共类MPLogger实现MessageProcessorNotificationListener{
私有记录器log=Logger.getLogger(MPLogger.class);
private long startTime=System.currentTimeMillis();
private long-endTime=System.currentTimeMillis();
@凌驾
公共无效通知(MessageProcessorNotification m){
MuleEvent ev=m.getSource();
MuleMessage msg=ev.getMessage();
对象负载=msg.getPayload();
String ref=payload!=null?payload.toString():“null”;
MessageProcessor proc=m.getProcessor();
布尔inc=真;
if(m.getAction()==MessageProcessorNotification.MESSAGE\u PROCESSOR\u PRE\u INVOKE){
startTime=System.currentTimeMillis();
}
if(m.getAction()==MessageProcessorNotification.MESSAGE\u PROCESSOR\u POST\u INVOKE){
long executionTime=System.currentTimeMillis()-startTime;
AbstractEndpoint ep=(AbstractEndpoint)过程;
log.info(“Http调用:“+ep.getName()+”占用“+executionTime+”毫秒响应时间”);
}
布尔输出=DefaultOutboundEndpoint的proc instanceof;
if(公司){
如果(传出){
AbstractEndpoint ep=(AbstractEndpoint)过程;
log.warn(msg.getMessageRootId()+“[出站]消息”+ref+“->”+ep.getEndpointURI());
ep.getResponseTimeout();
}否则{
log.warn(msg.getMessageRootId()+“[处理]消息”+ref+“”+proc.getClass().getSimpleName());
}
}
}
}
从上面的代码中,您可以随意获得消息处理器或流的名称并截获有效负载

这有什么帮助