处理复合队列中的通配符 - ActiveMQ xml 配置文件



我想就ActiveMQ的一个主题征求意见。 😅

我正在使用 ActiveMQ 5.15。我正在尝试修改 xml 配置文件,以便使用转发到另一个队列/主题的复合队列添加虚拟目标。从此组件的 ActiveMQ 文档中,架构如下:

<compositeQueue name="IncomingOrders"> 
<forwardTo>
<topic physicalName="Notifications" />
</forwardTo>
</compositeQueue>

我已经能够转发来自现有队列的消息,这些队列的名称例如request.typeA.classC.但是,我有几个使用相同的前缀request.typeA的队列,因此我的目的是使用通配符,因为不必为每个具有该前缀的现有队列定义复合队列,并使其更易于维护。

我需要这样的东西:

<compositeQueue name="request.typeA.>"> 
<forwardTo>
<topic physicalName="Notifications" />
</forwardTo>
</compositeQueue>

但是,这段代码不起作用,我怀疑这是因为它根本不受支持(至少现在还不支持)。我已经成功地尝试在physicalName属性中使用通配符,但没有在名称中使用通配符。

  • 有一个先决条件是我必须保留使用相同前缀的不同队列(不能将它们合并为一个)。

  • 的另一个先决条件是我无法通过代码动态创建新的队列/主题(由于服务器权限)。这就是为什么我对修改 xml 配置文件感兴趣。

所以我想知道你们中是否有人知道是否可以在name属性中使用通配符(我没有在文档中阅读任何证据),如果是这样,我该怎么做。如果您确定使用当前的ActiveMQ版本无法做到这一点,我将感谢您确认它。

我也希望您出于与我相同的目的提出其他替代方案/建议,并满足我之前提到的先决条件。我也读过镜像队列,但是这是一个影响所有现有队列的设置(我只对其中的一小部分感兴趣),并且可能对性能产生相当大的影响。

提前非常感谢您的时间和最诚挚的问候。 😄

我正在使用 5.15.11,我看到通配符适用于复合队列。这是我的复合队列配置。

<compositeQueue name="email.>">
<forwardTo>
<queue physicalName="MY_MAIN_QUEUE" />
</forwardTo>
</compositeQueue>

最后,我找到了一种解决方法,允许我只为给定前缀的队列子集创建镜像队列。

我所做的是创建自己的 DestinationInterceptor,以便只为我感兴趣的那些队列创建一个镜像队列,并排除其余队列(因为默认的 MirroredQueue 实现镜像了在系统中创建的所有队列)。

我是怎么做到的。我将 MirroredQueue.java 类实现从库中复制到一个名为 CustomMirroredQueue 的新类中,并向该类添加了一个名为镜像的新属性。我从接口DestinationInterceptor修改了intercept(final Destination destination)实现,在 if 语句中考虑了这个新属性(我为它创建了一个名为isPrefixMirror的辅助方法):

/*
* This method is responsible for intercepting all the queues/topics that are created in the system.
* In this particular case we are interested only in the queues, in order we can mirror *some* of them and get
* a copy of the messages that are sent to them (With the topics this mirroring is not necessary since we would just
* subscribe to that topic for receiving the same message).
* */
public Destination intercept(final Destination destination) {
if (destination.getActiveMQDestination().isQueue()) {
if (isPrefixMirrored(destination) && (!destination.getActiveMQDestination().isTemporary() || brokerService.isUseTempMirroredQueues())) {
try {
//we create a mirrored queue for that destination
final Destination mirrorDestination = getMirrorDestination(destination);
if (mirrorDestination != null) {
return new DestinationFilter(destination) {
public void send(ProducerBrokerExchange context, Message message) throws Exception {
message.setDestination(mirrorDestination.getActiveMQDestination());
mirrorDestination.send(context, message);
if (isCopyMessage()) {
message = message.copy();
}
message.setDestination(destination.getActiveMQDestination());
message.setMemoryUsage(null); // set this to null so that it will use the queue memoryUsage instance instead of the topic.
super.send(context, message);
}
};
}
} catch (Exception e) {
LOG.error("Failed to lookup the mirror destination for: {}", destination, e);
}
}
}
return destination;
}
/*
* @returns true if the destination passed as parameter will be mirrored. If the value for the attribute "mirroring"
* is an empty string "" then all the queues will be mirrored by default.
**/
private boolean isPrefixMirrored(Destination destination) {
if (mirroring.equals("")) {
return true;
}
List<String> mirroredQueuesPrefixes = Arrays.asList(mirroring.split(","));
final String destinationPhysicalName = destination.getActiveMQDestination().getPhysicalName();
return mirroredQueuesPrefixes.stream().map(String::trim).anyMatch(destinationPhysicalName::contains);
}

我生成了一个仅包含此自定义类和依赖项的.jar(为此使用了 gradle),并将其添加到 ActimeMQ 代理安装中的 lib 文件夹中。然后,我能够将此标记用作 ActiveMQ 配置 XML 文件中的bean

<destinationInterceptors>
<bean xmlns="http://www.springframework.org/schema/beans" class="package.CustomMirroredQueue" id="CustomMirroredQueue">
<property name="copyMessage" value="true"/>
<property name="postfix" value=""/>
<property name="prefix" value="mirror."/>
<property name="mirroring" value="PREFIX_1, QUEUE2, QUEUE3"/>
</bean>
</destinationInterceptors>

该类必须包含 ActiveMQ 的库文件夹中该类的路径。属性 copyMessage、后缀和前缀来自默认的 MirroredQueue 实现。镜像属性将是一个列表,其中包含要镜像的所有特定队列/前缀(并且仅这些队列/前缀)。

最新更新