Send Messages with SDK

<p class="shortdesc">This chapter describes how to send messages with SDK by taking calling Java SDK in TCP protocol as an example.</p> <section class="section prereq" id="send_sdk__prereq_tdx_5zx_dmb"><div class="tasklabel"><h2 class="doc-tairway">Before you begin</h2></div> <div class="p"> <ul class="ul" id="send_sdk__ul_dkz_5zx_dmb"> <li class="li">You have successfully created a topic, and applied for publishing and subscription in the console.</li> <li class="li">You have successfully created an ECS cloud host. For more information, see <a class="xref" href="https://pinganyun.com/ssr/help/compute/ecs/manual.Instance.Create_ECS_Instance" target="_blank">Create an ECS Instance</a>.</li> <li class="li">You have successfully installed IntelliJ or other development tools on ECS.</li> </ul> </div> </section> <section class="section context"><div class="tasklabel"><h2 class="doc-tairway">About this task</h2></div> <p class="p">After completing the creation of topics, producers, consumers and other resources through the console, you need to integrate the SDK into the client application and complete the code of the business logic for sending messages.</p> </section> <section><div class="tasklabel"><h2 class="doc-tairway">Procedure</h2></div><ol class="ol steps"><li class="li step stepexpand"> <span class="ph cmd">Introduce dependent packages through Maven.</span> <div class="itemgroup info"> <pre class="pre codeblock"><code><dependency> <groupId>com.paic.fincloud</groupId> <artifactId>fmq-client</artifactId> <version>1.0.0</version> </dependency> <!—Other dependent packages --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.0.25.Final</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> </code></pre> </div> </li><li class="li step stepexpand"> <span class="ph cmd">Follow the instructions below to set the required parameters and run the sample code to send the message.</span> <div class="itemgroup info"> <pre class="pre codeblock"><code>public static Producer initProducer() throws FCException { // Prepare required parameters. Properties propertie = new Properties(); // The business system can take out the properties value from the configuration file and write it in the demo. propertie.setProperty(FCConstant.PRODUCER_ID, "PID-demo");// Must configure, and create in the console. propertie.setProperty(FCConstant.NAME_SERVER_ADDRESS, "NAME_SERVER_ADDR-demo");// Must configure, environment IP address. // Set ak/sk, get it from the console. propertie.setProperty(FCConstant.ACCESS_KEY, "ACCESSKEY-demo"); propertie.setProperty(FCConstant.SECRET_KEY, "SECRETKEY-demo"); // Get MQCPProducer instance, producer is single-instance。 Producer producer = FCFactory.createProducer(propertie); // Start service producer.start(); return producer; } public static void sendMsg(Producer producer) { try { String topic = Constant.TOPIC;// Send messages. If you need to send messages to different topics, just send them to different topics. FCMessage message = TestProducer.generateMsg(topic); // Send messages. // The status is returned on success, In case of failure, an exception will be thrown directly. FCSendResult sendResult = producer.sendMessage(message); // We recommend that you add some logs in the business side to record the status of sending messages, such as message ID, message tag, message key, and message sending status. System.out.println("####msg status:" + sendResult.getSendStatus() + " msgId:" + sendResult.getMessageId()); } catch (FCException ex) { // TODO: If failed, the business system can add some exception handling. But we do not recommend that you send messages again, as internal sendmsg of mqcp has a retry mechanism. // Retry for 3 times, and the polling broker is used to retry. The retry time does not exceed 10s by default. However, if it is a timeout exception, it will not retry. // It is recommended that the business system be stored in the database and retry periodically from the backend. ex.printStackTrace(); } } </code></pre> </div> </li><li class="li step stepexpand"> <span class="ph cmd">You can check the message sending status through Message Id in the console. If you can find the message, it means that the message was sent successfully. For more information, see <a class="xref" href="https://pinganyun.com/ssr/help/middleware/pamq/manual.message_search.id_search" target="_blank">Message Id</a>.</span> </li></ol></section>
Did the above content solve your problem? Yes No
Please complete information!

Call us

400-151-8800

Email us

cloud@pingan.com

Online customer service

Instant reply

Technical Support

cloud products