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

时间:2019-01-03 16:18:32

标签: java apache-camel jms

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

在我最关心的部分中,用户将消息插入到数据库INPUT之类的DB表中,该表的列为DESTINATIONPRIORITYMESSAGE。优先级列不是必需的,其他两个都是必需的。

然后,应用程序从此表中的条目中获取信息,并创建带有JMSPriority = PRIORITY标头的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处理器中派生该程序,因为无论如何该信息都将在GeneralMessageProcessor中重写,并且setter处理器没有所有公开了必要的类和字段。

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

1 个答案:

答案 0 :(得分:2)

是的,值$a = unserialize(file_get_contents('http://www.geoplugin.net/php.gp? ip='.$_SERVER['REMOTE_ADDR'])); $countrycode= $a['geoplugin_countryCode']; 是默认的JMS优先级。因此,每条消息都有优先级,根本没有4优先级之类的东西,甚至根本没有优先级。

但是,一个不使数据库紧张的非常简单的解决方法是设置另一个消息标头,例如null或您喜欢的任何名称。然后,您可以使用此标头在默认优先级和prioritySetByApplication的“显式”优先级之间进行区分。