|
| 1 | +// Copyright 2023 The RocketMQ Rust Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use rocketmq_client_rust::producer::default_mq_producer::DefaultMQProducer; |
| 16 | +use rocketmq_client_rust::producer::mq_producer::MQProducer; |
| 17 | +use rocketmq_common::common::message::message_single::Message; |
| 18 | +use rocketmq_error::RocketMQResult; |
| 19 | +use rocketmq_rust::rocketmq; |
| 20 | + |
| 21 | +pub const MESSAGE_COUNT: usize = 1; |
| 22 | +pub const PRODUCER_GROUP: &str = "producer_simple"; |
| 23 | +pub const DEFAULT_NAMESRVADDR: &str = "127.0.0.1:9876"; |
| 24 | +pub const TOPIC: &str = "ProducerSimpleTest"; |
| 25 | +pub const TAG: &str = "TagA"; |
| 26 | + |
| 27 | +#[rocketmq::main] |
| 28 | +pub async fn main() -> RocketMQResult<()> { |
| 29 | + //init logger |
| 30 | + rocketmq_common::log::init_logger()?; |
| 31 | + |
| 32 | + // create a producer builder with default configuration |
| 33 | + let mut producer = DefaultMQProducer::builder() |
| 34 | + .producer_group(PRODUCER_GROUP) |
| 35 | + .name_server_addr(DEFAULT_NAMESRVADDR) |
| 36 | + .build(); |
| 37 | + |
| 38 | + producer.start().await?; |
| 39 | + |
| 40 | + for _ in 0..10 { |
| 41 | + let message = Message::builder() |
| 42 | + .topic(TOPIC) |
| 43 | + .tags(TAG) |
| 44 | + .body("Hello RocketMQ") |
| 45 | + .build()?; |
| 46 | + let send_result = producer.send(message).await?; |
| 47 | + println!("send result: {:?}", send_result); |
| 48 | + } |
| 49 | + producer.shutdown().await; |
| 50 | + Ok(()) |
| 51 | +} |
0 commit comments