All Downloads are FREE. Search and download functionalities are using the official Maven repository.

demo.Send Maven / Gradle / Ivy

The newest version!
package demo;

import com.qcloud.cmq.client.common.ClientConfig;
import com.qcloud.cmq.client.common.ResponseCode;
import com.qcloud.cmq.client.producer.Producer;
import com.qcloud.cmq.client.producer.SendResult;

public class Send {
    public static void main(String args[]) {
        Producer producer = new Producer();
        // 设置 Name Server地址。必须设置 不同地域不同网络不同
        producer.setNameServerAddress("http://183.47.97.189:9990");
        // 设置SecretId,在控制台上获取,必须设置
        producer.setSecretId("AKIDrcs5AKw5mKiutg9gxGOK5eEdppfLw7D7");
        // 设置SecretKey,在控制台上获取,必须设置
        producer.setSecretKey("qV2NVSEPRGtMfqpcCaoqhMH14wc6TgiY");
        // 设置签名方式,可以不设置,默认为SHA1
        producer.setSignMethod(ClientConfig.SIGN_METHOD_SHA256);
        // 设置发送消息失败时,重试的次数,设置为0表示不重试,默认为2
        producer.setRetryTimesWhenSendFailed(3);
        // 设置请求超时时间, 默认3000ms
        producer.setRequestTimeoutMS(5000);

        // 消息发往的队列,在控制台创建
        String queue = "reserved_test-1";
        try {
            // 启动对象前必须设置好相关参数
            producer.start();
            final String msg = "this is a tcp message";
            // 同步单条发送消息
            SendResult result = producer.send(queue, msg);
            if (result.getReturnCode() == ResponseCode.SUCCESS) {
                System.out.println("==> send success! msg_id:" + result.getMsgId() + " request_id:" + result.getRequestId());
            } else {
                System.out.println("==> code:" + result.getReturnCode() + " error:" + result.getErrorMsg());
            }
            producer.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy