如果在没有 JMSPriority header 的情况下发送 JMS 消息会发生什么

What happens if JMS message is sent without JMSPriority header

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

在我最关心的部分,用户将消息插入到数据库中的 table 中,例如 table INPUTDESTINATION,PRIORITY, MESSAGE.优先级列不是强制性的,其他都是强制性的。

应用程序然后从 table 中的条目中获取信息并使用 JMSPriority = PRIORITY header 创建 JMS。 Body 填充 BODY 列,然后将 JMS 发送到 DESTINATION 中指定的 queue。

代码片段:

//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 总是 returns 4。我明白,优先级 4 是默认的,我可以接受这样处理的消息,但我需要能够区分优先级 4 何时被设置为数据库中的固定值 table 并因此请求,或者如果根本未设置优先级,则程序在后续处理器内的行为应该略有不同。

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

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

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

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