Search in sources :

Example 1 with Account

use of com.qcloud.cmq.Account in project cmq-java-sdk by tencentyun.

the class Consumer method main.

public static void main(String[] args) {
    String secretId = "";
    String secretKey = "";
    String endpoint = "https://cmq-queue-{$region}.api.qcloud.com";
    String queueName = "test";
    Account account = new Account(endpoint, secretId, secretKey);
    // 获得队列实例
    System.out.println("--------------- queue[qiyuan-test] ---------------");
    Queue queue = account.getQueue(queueName);
    try {
        // 接收单条消息
        System.out.println("---------------recv message ...---------------");
        Message msg = queue.receiveMessage();
        System.out.println("msgId:" + msg.msgId);
        System.out.println("msgBody:" + msg.msgBody);
        System.out.println("receiptHandle:" + msg.receiptHandle);
        System.out.println("enqueueTime:" + msg.enqueueTime);
        System.out.println("nextVisibleTime:" + msg.nextVisibleTime);
        System.out.println("firstDequeueTime:" + msg.firstDequeueTime);
        System.out.println("dequeueCount:" + msg.dequeueCount);
        // 删除消息
        // System.out.println("---------------delete message ...---------------");
        // queue.deleteMessage(msg.receiptHandle);
        // System.out.println("receiptHandle:" + msg.receiptHandle +" deleted");
        // 批量接收消息
        // 保存服务器返回的消息句柄,用于删除消息
        ArrayList<String> vtReceiptHandle = new ArrayList<String>();
        System.out.println("---------------batch recv message ...---------------");
        List<Message> msgList = queue.batchReceiveMessage(10);
        System.out.println("recv msg count:" + msgList.size());
        for (int i = 0; i < msgList.size(); i++) {
            Message msg1 = msgList.get(i);
            System.out.println("msgId:" + msg1.msgId);
            System.out.println("msgBody:" + msg1.msgBody);
            System.out.println("receiptHandle:" + msg1.receiptHandle);
            System.out.println("enqueueTime:" + msg1.enqueueTime);
            System.out.println("nextVisibleTime:" + msg1.nextVisibleTime);
            System.out.println("firstDequeueTime:" + msg1.firstDequeueTime);
            System.out.println("dequeueCount:" + msg1.dequeueCount);
            System.out.println();
            vtReceiptHandle.add(msg1.receiptHandle);
        }
    // 批量删除消息
    // System.out.println("---------------batch delete message ...---------------");
    // queue.batchDeleteMessage(vtReceiptHandle);
    // for(int i=0;i<vtReceiptHandle.size();i++)
    // System.out.println("receiptHandle:" + vtReceiptHandle.get(i) + " deleted");
    } catch (CMQClientException e2) {
        System.out.println("Client Exception, " + e2.toString());
    } catch (Exception e) {
        System.out.println("error..." + e.toString());
    }
}
Also used : Account(com.qcloud.cmq.Account) CMQClientException(com.qcloud.cmq.CMQClientException) Message(com.qcloud.cmq.Message) ArrayList(java.util.ArrayList) Queue(com.qcloud.cmq.Queue) CMQClientException(com.qcloud.cmq.CMQClientException)

Example 2 with Account

use of com.qcloud.cmq.Account in project jeesuite-libs by vakinge.

the class CMQManager method doInit.

private void doInit() {
    queueName = MQContext.getGroupName();
    CmqConfig config = ResourceUtils.getBean("jeesuite.amqp.cmq.", CmqConfig.class);
    Validate.notBlank(config.getEndpoint(), "config[mq.cmq.endpoint] not found");
    Validate.notBlank(config.getSecretId(), "config[mq.cmq.secretId] not found");
    Validate.notBlank(config.getSecretKey(), "config[mq.cmq.secretKey] not found");
    config.setAlwaysPrintResultLog(false);
    config.setPrintSlow(false);
    this.account = new Account(config);
    logger.info("init CMQ Account OK -> endpoint:{}", config.getEndpoint());
}
Also used : Account(com.qcloud.cmq.Account) CmqConfig(com.qcloud.cmq.entity.CmqConfig)

Example 3 with Account

use of com.qcloud.cmq.Account in project jeesuite-libs by vakinge.

the class CMQManager method doInit.

private void doInit() {
    queueName = MQContext.getGroupName();
    CmqConfig config = ResourceUtils.getBean("mendmix.amqp.cmq.", CmqConfig.class);
    Validate.notBlank(config.getEndpoint(), "config[mq.cmq.endpoint] not found");
    Validate.notBlank(config.getSecretId(), "config[mq.cmq.secretId] not found");
    Validate.notBlank(config.getSecretKey(), "config[mq.cmq.secretKey] not found");
    config.setAlwaysPrintResultLog(false);
    config.setPrintSlow(false);
    this.account = new Account(config);
    logger.info("init CMQ Account OK -> endpoint:{}", config.getEndpoint());
}
Also used : Account(com.qcloud.cmq.Account) CmqConfig(com.qcloud.cmq.entity.CmqConfig)

Example 4 with Account

use of com.qcloud.cmq.Account in project cmq-java-sdk by tencentyun.

the class Producer method main.

public static void main(String[] args) {
    // 从腾讯云官网查询的云API密钥信息
    String secretId = "";
    String secretKey = "";
    String endpoint = "https://cmq-queue-{$region}.api.qcloud.com";
    String queueName = "test";
    Account account = new Account(endpoint, secretId, secretKey);
    try {
        // 创建新队列
        // System.out.println("---------------create queue ...---------------");
        // QueueMeta meta = new QueueMeta();
        // meta.pollingWaitSeconds = 10;
        // meta.visibilityTimeout = 10;
        // meta.maxMsgSize = 1048576;
        // meta.msgRetentionSeconds = 345600;
        // Queue queue = account.createQueue(queueName,meta);
        // System.out.println(queueName + " created");
        // 列出当前帐号下所有队列名字
        // System.out.println("---------------list queue ...---------------");
        // ArrayList<String> vtQueue = new ArrayList<String>();
        // int totalCount = account.listQueue("",-1,-1,vtQueue);
        // System.out.println("totalCount:" + totalCount);
        // for(int i=0;i<vtQueue.size();i++)
        // {
        // System.out.println("queueName:" + vtQueue.get(i));
        // }
        // 删除队列
        // System.out.println("---------------delete queue ...---------------");
        // account.deleteQueue(queueName);
        // System.out.println(queueName + " deleted");
        // 获得队列实例(此处直接使用现有队列进行操作,也可按照上面的注释创建队列)
        System.out.println("--------------- queue[qiyuan-test] ---------------");
        Queue queue = account.getQueue(queueName);
        // 设置队列属性
        System.out.println("---------------set queue attributes ...---------------");
        QueueMeta meta1 = new QueueMeta();
        meta1.pollingWaitSeconds = 20;
        queue.setQueueAttributes(meta1);
        System.out.println("pollingWaitSeconds=20 set");
        // 获取队列属性
        System.out.println("---------------get queue attributes ...---------------");
        QueueMeta meta2 = queue.getQueueAttributes();
        System.out.println("maxMsgHeapNum:" + meta2.maxMsgHeapNum);
        System.out.println("pollingWaitSeconds:" + meta2.pollingWaitSeconds);
        System.out.println("visibilityTimeout:" + meta2.visibilityTimeout);
        System.out.println("maxMsgSize:" + meta2.maxMsgSize);
        System.out.println("createTime:" + meta2.createTime);
        System.out.println("lastModifyTime:" + meta2.lastModifyTime);
        System.out.println("activeMsgNum:" + meta2.activeMsgNum);
        System.out.println("inactiveMsgNum:" + meta2.inactiveMsgNum);
        // 发送单条信息
        System.out.println("---------------send message ...---------------");
        String msg = "hello!";
        CmqResponse cmqResponse = queue.send(msg);
        System.out.println("==> send success! msg_id:" + cmqResponse.getMsgId() + " requestId:" + cmqResponse.getRequestId());
        // 批量操作
        // 批量发送消息
        System.out.println("---------------batch send message ...---------------");
        ArrayList<String> vtMsgBody = new ArrayList<String>();
        String msgBody = "hello world,this is cmq sdk for java 1";
        vtMsgBody.add(msgBody);
        msgBody = "hello world,this is cmq sdk for java 2";
        vtMsgBody.add(msgBody);
        msgBody = "hello world,this is cmq sdk for java 3";
        vtMsgBody.add(msgBody);
        List<CmqResponse> cmqResponses = queue.batchSend(vtMsgBody);
        for (int i = 0; i < vtMsgBody.size(); i++) {
            System.out.println("[" + vtMsgBody.get(i) + "] sent");
        }
        for (int i = 0; i < cmqResponses.size(); i++) {
            CmqResponse response = cmqResponses.get(i);
            System.out.println("msgId:" + response.getMsgId() + " requestId:" + response.getRequestId());
        }
    } catch (CMQServerException e1) {
        System.out.println("Server Exception, " + e1.toString());
    } catch (Exception e) {
        System.out.println("error..." + e.toString());
    }
}
Also used : Account(com.qcloud.cmq.Account) QueueMeta(com.qcloud.cmq.QueueMeta) ArrayList(java.util.ArrayList) CMQServerException(com.qcloud.cmq.CMQServerException) Queue(com.qcloud.cmq.Queue) CmqResponse(com.qcloud.cmq.entity.CmqResponse) CMQServerException(com.qcloud.cmq.CMQServerException)

Aggregations

Account (com.qcloud.cmq.Account)4 Queue (com.qcloud.cmq.Queue)2 CmqConfig (com.qcloud.cmq.entity.CmqConfig)2 ArrayList (java.util.ArrayList)2 CMQClientException (com.qcloud.cmq.CMQClientException)1 CMQServerException (com.qcloud.cmq.CMQServerException)1 Message (com.qcloud.cmq.Message)1 QueueMeta (com.qcloud.cmq.QueueMeta)1 CmqResponse (com.qcloud.cmq.entity.CmqResponse)1