简体   繁体   English

Apache Camel:从消息正文中提取队列名称

[英]Apache Camel : extract to-queue name from message body

I have a requirement where I have to send message to Microsoft Teams.我有一个要求,我必须向 Microsoft Teams 发送消息。 I am trying to extract "to" channel name information from message I receive from queue and based on the channel name, I read it's url from properties file and send message.我正在尝试从从队列接收到的消息中提取“到”通道名称信息,并根据通道名称,我从属性文件中读取它的 url 并发送消息。 Below is the code for that.下面是代码。

    RouteDefinition from = from("jms:queue:teamsq?connectionFactory=artemis");
    from.setHeader("Exchange.CONTENT_TYPE", constant("application/json"));
    final StringBuffer channelName = new StringBuffer();
    from.process(exchange -> {
        String[] dataArray = exchange.getIn().getBody(String.class).split(",", 2);
        channelName.append(dataArray[0]);
        exchange.getIn().setBody("{\"text\" : \"" + dataArray[1].trim() + "\"}");
    })
    .log("Body is : " + channelName + " : ${body}");

When body is logged, value of channelName is null.记录正文时,channelName 的值为 null。

Any help how can I get value of channelName outside this process() method?有什么帮助我怎样才能在这个 process() 方法之外获得 channelName 的值?

Message received from queue is从队列收到的消息是

channel1, This is test a message 5

Thanks in advance.提前致谢。

You can set a message header or an Exchange property .您可以设置消息 header 或 Exchange 属性 Both are kind of message variables to use during route processing.两者都是在路由处理期间使用的消息变量。

.setHeader("channelName", channelName.toString())
.setProperty("channelName", channelName.toString())

The main difference is that Exchange properties are sitting on the Camel Exchange while message headers are part of the message itself.主要区别在于 Exchange 属性位于 Camel Exchange 上,而邮件标头是邮件本身的一部分。

The Camel Exchange is a Camel wrapper around the message . Camel Exchange 是消息的 Camel 包装器 It is created when the message enters the route and thrown away at the end of the route.它在消息进入路由时创建,并在路由结束时被丢弃。

Exchange Properties:交换属性:

  • are only available during Camel route processing仅在骆驼路线处理期间可用
  • are never sent to other systems永远不会发送到其他系统
  • are only in-memory仅在内存中

Message headers:消息头:

  • are converted to message headers for the target system whenever the route does a routing to another system每当路由路由到另一个系统时,都会转换为目标系统的消息头
  • are therefore sent to other systems因此被发送到其他系统
  • are serialized when sent to another system发送到另一个系统时被序列化

If you send a message from a Camel route to a JMS queue and consume it from another route, the Exchange properties are no more available while the message headers are still present.如果您将消息从 Camel 路由发送到 JMS 队列并从另一个路由使用它,则 Exchange 属性不再可用,而消息头仍然存在。

However, if you route to a direct endpoint (Camel in-memory endpoint), the whole Exchange is transferred and Exchange properties are still available .但是,如果您路由到直接端点(Camel 内存中端点),则整个 Exchange 都会被传输,并且Exchange 属性仍然可用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM