gpt4 book ai didi

java - 如果发送 JMS 消息时没有 JMSPriority header ,会发生什么情况

转载 作者:行者123 更新时间:2023-12-02 01:43:47 26 4
gpt4 key购买 nike

我目前正在尝试对通过复杂应用程序的消息 (JMS) 进行优先级排序。

在我最关心的部分中,用户将消息插入数据库中的表中,例如带有列DESTINATIONPRIORITY的表INPUT消息。优先级列不是强制性的,其他都是强制性的。

然后,应用程序从此表中的条目中获取信息,并使用 JMSPriority = PRIORITY header 创建 JMS。正文填充为 BODY 列,然后 JMS 被发送到 DESTINATION 中指定的队列。

代码片段:

//pull requests from database and set headers
from(RouteConstants.READ_REQUESTS_FROM_DATABASE) //this is a route formed by SQL
.transacted("PROPAGATION_REQUIRED_JBOSS")
.process(setHeaderProperties)
.to("direct:jms");
<小时/>
//send JMS to destination
from("direct:jms").setBody(simple("${property.MESSAGE}"))
.convertBodyTo(String.class).recipientList(
simple("jms:queue:${property.DESTINATION}?
exchangePattern=InOnly&jmsMessageType=Text&preserveMessageQos=true
&disableReplyTo=true"));
<小时/>
public class SetHeaderProperties implements Processor {
public void process(Exchange exchange) throws Exception {
LinkedCaseInsensitiveMap body = (LinkedCaseInsensitiveMap) exchange.getIn().getBody();
exchange.setProperty("MESSAGE", body.get("MESSAGE").toString());
exchange.setProperty("DESTINATION", body.get("DESTINATION").toString());
Long priority = getPriorityQuery(); //DAO method that returns value of PRIORITY or null if empty
if(priority != null) exchange.setProperty("PRIORITY", priority);
}
<小时/>
//Receive the JMS. Consider this point to be the same that the message was sent to in the second snippet
from("jms:queue:input-msgs").
log(LoggingLevel.DEBUG, CAMEL_LOGGER_NAME, "Received JMSPriority: ${header.JMSPriority}"). //This getter is problematic, see below snippets
process(generalMessageProcessor);

只要 PRIORITY 列被填充,应用程序就会正常运行。当PRIORITY的值为null时,getter总是返回4。据我所知,优先级 4 是默认值,并且我可以接受这样处理的消息,但我需要能够区分优先级 4 何时在数据库表中设置为固定值因此请求,或者如果根本没有设置优先级,因此程序应该在后续处理器内以稍微不同的路线运行。

这有可能吗?我想避免更改数据库的 DDL,而且我也不能只在 SetHeaderProperties 处理器中 fork 程序,因为信息无论如何都会在 GeneralMessageProcessor 中被重写,并且 setter 处理器没有公开所有必要的类和字段。

我认为可行的天真的答案是每当我需要检查优先级时再次调用 DAO 查询,但这会给数据库带来压力,我想知道是否有更优雅的解决方案来解决该问题。

最佳答案

是的,值4是默认的JMS优先级。因此,每条消息都有一个优先级,并且不存在诸如 null 优先级或根本没有优先级之类的情况。

但是,一个不会对数据库造成压力的非常简单的解决方法是设置另一个消息 header ,例如 prioritySetByApplication 或任何您喜欢的名称。然后,您可以使用此 header 来区分默认优先级和“显式”优先级 4

关于java - 如果发送 JMS 消息时没有 JMSPriority header ,会发生什么情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54026064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com