|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +# contributor license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright ownership. |
| 4 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +# (the "License"); you may not use this file except in compliance with |
| 6 | +# the License. You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +from rocketmq import ClientConfiguration, Credentials, Message, Producer |
| 17 | + |
| 18 | +if __name__ == '__main__': |
| 19 | + endpoints = "foobar.com:8080" |
| 20 | + credentials = Credentials() |
| 21 | + |
| 22 | + # if auth enable |
| 23 | + # credentials = Credentials("ak", "sk") |
| 24 | + config = ClientConfiguration(endpoints, credentials) |
| 25 | + # with namespace |
| 26 | + # config = ClientConfiguration(endpoints, credentials, "namespace") |
| 27 | + topic = "parent-topic" |
| 28 | + producer = Producer(config, (topic,)) |
| 29 | + |
| 30 | + try: |
| 31 | + producer.startup() |
| 32 | + try: |
| 33 | + msg = Message() |
| 34 | + # parent topic for the current message |
| 35 | + msg.topic = topic |
| 36 | + msg.body = "hello, rocketmq.".encode('utf-8') |
| 37 | + for i in range(0, 10): |
| 38 | + # set lite_topic |
| 39 | + msg.lite_topic("lite-test-" + str(i)) |
| 40 | + res = producer.send(msg) |
| 41 | + print(f"{producer} send message success. {res}") |
| 42 | + producer.shutdown() |
| 43 | + print(f"{producer} shutdown.") |
| 44 | + except Exception as e: |
| 45 | + print(f"{producer} raise exception: {e}") |
| 46 | + producer.shutdown() |
| 47 | + print(f"{producer} shutdown.") |
| 48 | + except Exception as e: |
| 49 | + print(f"{producer} startup raise exception: {e}") |
| 50 | + producer.shutdown() |
| 51 | + print(f"{producer} shutdown.") |
0 commit comments