使用骡子,有没有办法拦截所有流量?我的用例是查看流名称,如果流名称与http请求中的特定参数匹配,我需要采取一些操作。我不想为每个流编写拦截器配置。这太繁琐而且不理想。
我喜欢像应用程序中的所有流的Spring AOP交叉,然后编写一个可以在流程执行之前由Mule调用的java类。我会检查那里的流程名称,如果它与请求中的参数匹配,我可以采取行动,否则,我不会被要求做任何事情。
我是骡子的新手。有人可以指出我正确的方向吗?
答案 0 :(得分:2)
您可以使用服务器通知执行此操作,我已经实现了一种类似的方法,可以侦听PRE / AFTER调用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());
}
}
}
}
从上面的代码中,您可以使用它来获取消息处理器或流的名称并拦截有效负载。
这有何帮助
答案 1 :(得分:1)
因此,如果您想基于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到对象变换器(包含JSON请求)并提取元素值,并根据元素值,您可以路由使用Choice路由器
答案 2 :(得分:0)
我想建议你一种方法来满足要求。
使用http作为入口点定义公共流,然后定义一个选择路由器,用于评估所考虑的参数。基于条件评估的SO使用flow-ref调用相应的流程。
答案 3 :(得分:0)
建议的内容可行!
但是,根据您对该场景的描述,我觉得您最好使用APIkit。
当然它可能有更多你需要的功能,但简而言之,基于REST API定义(这是http请求的一部分),它会自动将它路由到实现API部分的流程
很酷的是你只需要编写RAML,工作室就会为你生成流程。
希望这会有所帮助;)
答案 4 :(得分:0)
为什么不使用简单的&#34; Choice路由器&#34;。根据您在输入中设置的请求参数,可以调用与条件匹配的相应流。