【问题标题】:How to enable Mule server notifications by default?如何默认启用 Mule 服务器通知?
【发布时间】:2016-08-16 10:49:36
【问题描述】:

在提供的服务器上从 Mule Studio 运行 Mule 应用程序时,默认情况下,我能够从实现接口 EndpointMessageNotificationListener 的类“监听”服务器通知。

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

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

如果我们需要在每个应用中启用通知。如何做到这一点?我试过(com.company.EndpointListener 是我的类实现 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

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

【问题讨论】:

    标签: mule


    【解决方案1】:

    好的,当我发现问题所在时,这是一个相当晦涩的解决方法。

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

    另一个通知侦听器的问题是它试图在初始化期间以这种方式读取属性文件(当部署到 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 
    

    在此之后,任何通知侦听器都无法收到任何通知。

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

    【讨论】:

      【解决方案2】:

      是一个错误: 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> {
      

      但是没有触发监听器。

      来源:Mule - Intercept all flows

      • 拦截端点消息。

      不干净且具有侵入性。

      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;
          }
      
      }
      

      在骡子配置中:

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

      这也适用于出站端点。

      我希望这对某人有帮助!

      【讨论】:

        【解决方案3】:

        给试图达到相同结果的访问者的小提示。 首先,您应该根据您想要通知的内容实现一个 Java 类,如下例所示:

        package stackoverflow.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="stackoverflow.mule.notifications.MyEndpointNotificationListener" 
                         id="MyEndpointNotificationListener" />
        </spring:beans>
        

        您必须在其下方注册您的通知,如下所示:

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

        然后就可以了。现在应该可以工作了。这已在 Mule ESB 3.8(CE 或 EE)中使用。

        干杯!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-04-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多