Skip to content

Commit 505c690

Browse files
authored
[ISSUE #6321]🚀Add producer_simple example for RocketMQ (#6322)
1 parent eb12a5f commit 505c690

File tree

3 files changed

+64
-8
lines changed

3 files changed

+64
-8
lines changed

rocketmq-example/Cargo.lock

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rocketmq-example/Cargo.toml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ resolver = "3"
3232

3333
[dependencies]
3434

35-
[[example]]
36-
name = "pop-consumer"
37-
path = "examples/consumer/pop_consumer.rs"
38-
3935
[dev-dependencies]
4036
cheetah-string = { version = "1.0.1", features = ["serde", "bytes", "simd"] }
4137
rocketmq-client-rust = { path = "../rocketmq-client" }
@@ -45,3 +41,11 @@ rocketmq-rust = { path = "../rocketmq" }
4541
rocketmq-tools = { path = "../rocketmq-tools/rocketmq-admin/rocketmq-admin-core", package = "rocketmq-admin-core" }
4642
tokio = { version = "1.49", features = ["full"] }
4743
tracing = "0.1.44"
44+
45+
[[example]]
46+
name = "pop-consumer"
47+
path = "examples/consumer/pop_consumer.rs"
48+
49+
[[example]]
50+
name = "producer-simple"
51+
path = "examples/producer/producer_simple.rs"
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)