rocketmq_client_rust/utils/
message_util.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17use bytes::Bytes;
18use cheetah_string::CheetahString;
19use rocketmq_common::common::message::message_single::Message;
20use rocketmq_common::common::message::MessageConst;
21use rocketmq_common::common::message::MessageTrait;
22use rocketmq_common::common::mix_all;
23use rocketmq_common::MessageAccessor::MessageAccessor;
24use rocketmq_error::mq_client_err;
25
26use crate::common::client_error_code::ClientErrorCode;
27
28pub struct MessageUtil;
29
30impl MessageUtil {
31    pub fn create_reply_message(
32        request_message: &Message,
33        body: &[u8],
34    ) -> rocketmq_error::RocketMQResult<Message> {
35        let mut reply_message = Message::default();
36        let cluster = request_message.get_property(&CheetahString::from_static_str(
37            MessageConst::PROPERTY_CLUSTER,
38        ));
39        if let Some(cluster) = cluster {
40            reply_message.set_body(Bytes::copy_from_slice(body));
41            let reply_topic = mix_all::get_retry_topic(&cluster);
42            reply_message.set_topic(CheetahString::from_string(reply_topic));
43            MessageAccessor::put_property(
44                &mut reply_message,
45                CheetahString::from_static_str(MessageConst::PROPERTY_MESSAGE_TYPE),
46                CheetahString::from_static_str(mix_all::REPLY_MESSAGE_FLAG),
47            );
48            if let Some(reply_to) = request_message.get_property(&CheetahString::from_static_str(
49                MessageConst::PROPERTY_MESSAGE_REPLY_TO_CLIENT,
50            )) {
51                MessageAccessor::put_property(
52                    &mut reply_message,
53                    CheetahString::from_static_str(MessageConst::PROPERTY_MESSAGE_REPLY_TO_CLIENT),
54                    reply_to,
55                );
56            }
57            if let Some(correlation_id) = request_message.get_property(
58                &CheetahString::from_static_str(MessageConst::PROPERTY_CORRELATION_ID),
59            ) {
60                MessageAccessor::put_property(
61                    &mut reply_message,
62                    CheetahString::from_static_str(MessageConst::PROPERTY_CORRELATION_ID),
63                    correlation_id,
64                );
65            }
66            if let Some(ttl) = request_message.get_property(&CheetahString::from_static_str(
67                MessageConst::PROPERTY_MESSAGE_TTL,
68            )) {
69                MessageAccessor::put_property(
70                    &mut reply_message,
71                    CheetahString::from_static_str(MessageConst::PROPERTY_MESSAGE_TTL),
72                    ttl,
73                );
74            }
75            Ok(reply_message)
76        } else {
77            mq_client_err!(
78                ClientErrorCode::CREATE_REPLY_MESSAGE_EXCEPTION,
79                format!(
80                    "create reply message fail, requestMessage error, property[{}] is null.",
81                    MessageConst::PROPERTY_CLUSTER
82                )
83            )
84        }
85    }
86
87    pub fn get_reply_to_client(reply_message: &Message) -> Option<CheetahString> {
88        reply_message.get_property(&CheetahString::from_static_str(
89            MessageConst::PROPERTY_MESSAGE_REPLY_TO_CLIENT,
90        ))
91    }
92}