本文主要研究一下artemis的BaseInterceptor

BaseInterceptor

activemq-artemis-master/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/BaseInterceptor.java

public interface BaseInterceptor<P> {

   /**
    * Intercepts a packet which is received before it is sent to the channel
    *
    * @param packet     the packet being received
    * @param connection the connection the packet was received on
    * @return {@code true} to process the next interceptor and handle the packet,
    * {@code false} to abort processing of the packet
    * @throws ActiveMQException
    */
   boolean intercept(P packet, RemotingConnection connection) throws ActiveMQException;

}
  • BaseInterceptor定义了intercept方法,该方法返回true表示可以继续下一个interceptor,返回false表示终止packet的处理

AbstractProtocolManager

activemq-artemis-master/artemis-server/src/main/java/org/apache/activemq/artemis/spi/core/protocol/AbstractProtocolManager.java

public abstract class AbstractProtocolManager<P, I extends BaseInterceptor<P>, C extends RemotingConnection> implements ProtocolManager<I> {

   private final Map<SimpleString, RoutingType> prefixes = new HashMap<>();

   protected void invokeInterceptors(final List<I> interceptors, final P message, final C connection) {
      if (interceptors != null && !interceptors.isEmpty()) {
         for (I interceptor : interceptors) {
            try {
               if (!interceptor.intercept(message, connection)) {
                  break;
               }
            } catch (Exception e) {
               ActiveMQServerLogger.LOGGER.failedToInvokeAninterceptor(e);
            }
         }
      }
   }

   @Override
   public void setAnycastPrefix(String anycastPrefix) {
      for (String prefix : anycastPrefix.split(",")) {
         prefixes.put(SimpleString.toSimpleString(prefix), RoutingType.ANYCAST);
      }
   }

   @Override
   public void setMulticastPrefix(String multicastPrefix) {
      for (String prefix : multicastPrefix.split(",")) {
         prefixes.put(SimpleString.toSimpleString(prefix), RoutingType.MULTICAST);
      }
   }

   @Override
   public Map<SimpleString, RoutingType> getPrefixes() {
      return prefixes;
   }
}
  • AbstractProtocolManager的invokeInterceptors方法接收interceptors,之后挨个遍历interceptors,执行interceptor.intercept(message, connection),若返回false则立马终止循环

小结

BaseInterceptor定义了intercept方法,该方法返回true表示可以继续下一个interceptor,返回false表示终止packet的处理

doc


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论