Send Messages with SDK

This chapter describes how to send messages with SDK by taking calling Java SDK in TCP protocol as an example.

Before you begin

  • You have successfully created a topic, and applied for publishing and subscription in the console.
  • You have successfully created an ECS cloud host. For more information, see Create an ECS Instance.
  • You have successfully installed IntelliJ or other development tools on ECS.

About this task

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.

Procedure

  1. Introduce dependent packages through Maven.
    
              com.paic.fincloud
              fmq-client
              1.0.0
          
          
          
              com.alibaba
              fastjson
              1.2.3
          
          
              io.netty
              netty-all
              4.0.25.Final
          
          
              org.slf4j
              slf4j-api
              1.7.5
          
          
  2. Follow the instructions below to set the required parameters and run the sample code to send the message.
    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();
              }
          }
          
  3. 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 Message Id.
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