From 84ffc42faf6bd11b5b26f2055883b70ff5bc010f Mon Sep 17 00:00:00 2001 From: mxsm Date: Sat, 3 Aug 2024 18:36:31 +0800 Subject: [PATCH] [ISSUE #868]Add a Producer example --- rocketmq-client/Cargo.toml | 3 ++ .../examples/producer/simple_producer.rs | 43 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 rocketmq-client/examples/producer/simple_producer.rs diff --git a/rocketmq-client/Cargo.toml b/rocketmq-client/Cargo.toml index f9d5acfe..2e6aff4e 100644 --- a/rocketmq-client/Cargo.toml +++ b/rocketmq-client/Cargo.toml @@ -34,3 +34,6 @@ lazy_static = { workspace = true } tracing.workspace = true tracing-subscriber.workspace = true +[[example]] +name = "simple-producer" +path = "examples/producer/simple_producer.rs" \ No newline at end of file diff --git a/rocketmq-client/examples/producer/simple_producer.rs b/rocketmq-client/examples/producer/simple_producer.rs new file mode 100644 index 00000000..e3ecadb9 --- /dev/null +++ b/rocketmq-client/examples/producer/simple_producer.rs @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +use rocketmq_client::producer::default_mq_producer::DefaultMQProducer; +use rocketmq_client::producer::mq_producer::MQProducer; +use rocketmq_client::Result; +use rocketmq_rust::rocketmq; + +pub const MESSAGE_COUNT: usize = 1; +pub const PRODUCER_GROUP: &str = "please_rename_unique_group_name"; +pub const DEFAULT_NAMESRVADDR: &str = "127.0.0.1:9876"; +pub const TOPIC: &str = "TopicTest"; +pub const TAG: &str = "TagA"; + +#[rocketmq::main] +pub async fn main() -> Result<()> { + // create a producer builder with default configuration + let builder = DefaultMQProducer::builder(); + + let mut producer = builder + .producer_group(PRODUCER_GROUP.to_string()) + .name_server_addr(DEFAULT_NAMESRVADDR.to_string()) + .build(); + + producer.start().await?; + + producer.shutdown().await; + + Ok(()) +}