activenq之消息筛选

2022-03-29 14:25:17 浏览数 (1)

消息筛选器来在provider端过滤消息,只有当消息属性满足一定的条件,才将这些消息传送给对应的consumer!!!

注:该特性只能作用于消息头以及消息属性,不能用来筛选消息体

实例代码:

代码语言:javascript复制
String filter = "sign = 'golang'";      
MessageConsumer messageConsumer = session.createConsumer(destination,filter);

broker具体实现查看:

AbstractSubscription:matches方法

代码语言:javascript复制
    public boolean matches(MessageReference node, MessageEvaluationContext context) throws IOException {
         ConsumerId targetConsumerId = node.getTargetConsumerId();
         if (targetConsumerId != null) {
             if (!targetConsumerId.equals(info.getConsumerId())) {
                 return false;
             }
         }
         try {
             return (selectorExpression == null || 
selectorExpression.matches(context)) && this.context.isAllowedToConsume(node);
         } catch (JMSException e) {
             LOG.info("Selector failed to evaluate: "   e.getMessage(), e);
             return false;
         }
     }

注意上面加粗部分代码。

0 人点赞