如何默认启用 Mule 服务器通知?

How to enable Mule server notifications by default?

当 运行 来自 Mule Studio 的 Mule 应用程序在提供的服务器上时,默认情况下,我能够 "listen" 从实现接口 EndpointMessageNotificationListener 的 class 到服务器通知。

当我在集群中运行的独立环境(版本 3.6.1)中部署相同的代码时,我无法检索到相同的通知。默认情况下启用服务器通知需要什么?

我不想在每个应用程序中都进行配置 — 但也许我们需要这样做?启动 mule-servers 时是否有配置默认启用服务器通知? (这是在Mule Studio自带的Mule服务器上实现的?)

如果我们需要在每个应用中启用通知。如何做到这一点?我试过(其中 com.company.EndpointListener 是我的 class 实现 EndpointMessageNotificationListener 接口):

<spring:bean name="endpointListener" class="com.company.EndpointListener"/>

<notifications>
  <notification event="ENDPOINT-MESSAGE"/> 
  <notification-listener ref="endpointListener"/>
</notifications>

(如此处所述:http://www.mulesoft.org/documentation/display/current/Mule+Server+Notifications

但它什么也没做。我的 class 在我们的独立环境中仍然没有收到任何通知。任何让它工作的指示?

好的,当我发现问题所在时,结果证明这是一个相当晦涩的修复。

一个不同的通知侦听器没有正确启动,这会阻碍所有其他通知侦听器接收任何通知(这有点危险)。

另一个通知侦听器问题是它试图在初始化期间以这种方式读取属性文件(当部署到 Mule Studio 使用的服务器时有效):

MyClass.class.getClassLoader().getResourceAsStream("MyFile.file")

但我需要将其更改为:

Thread.currentThread().getContextClassLoader().getResourceAsStream("MyFile.file")

否则监听器引发异常:

org.mule.work.DefaultWorkListener: Work caused exception on 'workCompleted'. Work being executed was: org.mule.context.notification.ServerNotificationManager@5a66c9cf 

之后,所有通知监听器都无法收到任何通知。

我处理完这个案例后,所有其他听众都开始毫无问题地收到通知。

是一个错误: https://www.mulesoft.org/jira/browse/MULE-7183

我也有同样的问题。好几天了,我没法让它工作:(

根据 :

https://docs.mulesoft.com/mule-user-guide/v/3.7/mule-server-notifications#notification-interfaces

有几个通知接口:组件消息通知,端点消息通知,连接通知,自定义通知,ETC

这里有一些捕获组件和 enpoint 消息的方法:

  • 仅拦截组件消息

这个解决方案跟我一样。需要最少的努力和最少的配置。问题是它可以接收点消息:(

import org.apache.log4j.Logger;
import org.mule.api.MuleEvent;
import org.mule.api.MuleMessage;
import org.mule.api.context.notification.MessageProcessorNotificationListener;
import org.mule.api.processor.MessageProcessor;
import org.mule.context.notification.MessageProcessorNotification;

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

@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();
    log.info(String.format("\n\n\n[%s][%s][%s][%s]\n\n\n", ev.getFlowConstruct().getName(),m.getProcessorPath(), proc.getClass().getSimpleName(),ref));

  }

}

在 mule xml 配置中:

<spring:beans>
        <spring:bean name="componentMessageProcessor" class="com.mycompany.ComponentMessageProcessor"></spring:bean>
    </spring:beans>

或在spring配置中:

<bean name="componentMessageProcessor" class="com.mycompany.componentMessageProcessor"></bean>

我尝试使用以下方法捕获端点消息:

implements EndpointMessageNotificationListener<EndpointMessageNotification>{

instead of

implements MessageProcessorNotificationListener<MessageProcessorNotification> {

但是没有触发侦听器。

来源:

  • 拦截 Enpoint 消息。

不干净而且有侵扰性。

import org.mule.api.MuleEvent;
import org.mule.api.MuleException;
import org.mule.api.processor.MessageProcessor;

public class EndpointMessageProcesor  implements MessageProcessor{

    @Override
    public MuleEvent process(MuleEvent event) throws MuleException {
        System.out.println("\n\n\n\n\n"+event.getMessage().getPayload()+"\n\n\n\n");
        return event;
    }

}

并且在 mule 配置中:

<jms:inbound-endpoint queue="queue.req" ...>
            <custom-processor class="com.mycompany.EndpointMessageProcesor"></custom-processor>
        </jms:inbound-endpoint>

这也适用于出站端点。

我希望这对某人有所帮助!

给试图达到相同结果的访问者的小提示。 首先,您应该按照以下示例根据您希望收到通知的内容实施 Java class:

package Whosebug.mule.notifications

. . .

public class MyEndpointNotificationListener implements EndpointMessageNotificationListener<EndpointMessageNotification> {

    @Override
    public void onNotification(EndpointMessageNotification notification) {        

        // Whatever you have to do when notified goes here.
    }
}

然后是您的 Mule 配置。首先,您将通知侦听器注册为 Spring bean:

<spring:beans>
    <spring:bean name="MyEndpointNotificationListener" 
                 class="Whosebug.mule.notifications.MyEndpointNotificationListener" 
                 id="MyEndpointNotificationListener" />
</spring:beans>

在它的正下方,您必须按如下方式注册您的通知:

<notifications>
    <notification event="ENDPOINT-MESSAGE"/>
    <notification-listener ref="MyEndpointNotificationListener" />
</notifications>

好了。现在应该工作了。这已在 Mule ESB 3.8(CE 或 EE)中使用。

干杯!