Mi Galaxy EMQ Book

代码示例 (Java)

创建队列:

  CreateQueueRequest createQueueRequest = new CreateQueueRequest(name);
  CreateQueueResponse createQueueResponse = queueClient.createQueue(
      createQueueRequest);
  // 之后的操作都需要使用这里的queueName,而不是第一行中的name
  String queueName = createQueueResponse.getQueueName();

发送者:

  while(true) {
    SendMessageRequest sendMessageRequest =
        new SendMessageRequest(queueName, messageBody);
    SendMessageResponse sendMessageResponse =
        messageClient.sendMessage(sendMessageRequest);
  }

接收者:

  while(true) {
    ReceiveMessageRequest receiveMessageRequest =
        new ReceiveMessageRequest(queueName);
    List<ReceiveMessageResponse> receiveMessageResponse =
        messageClient.receiveMessage(receiveMessageRequest);

    if (!receiveMessageResponse.isEmpty()) {

      // process receiveMessageResponse

      DeleteMessageBatchRequest deleteMessageBatchRequest =
          new DeleteMessageBatchRequest();
      deleteMessageBatchRequest.setQueueName(queueName);
      for (ReceiveMessageResponse response : receiveMessageResponse) {
        deleteMessageBatchRequest.addToDeleteMessageBatchRequestEntryList(
            new DeleteMessageBatchRequestEntry(response.getReceiptHandle()));
      }
      messageClient.deleteMessageBatch(deleteMessageBatchRequest);
    }
  }

删除队列:

  DeleteQueueRequest deleteQueueRequest = new DeleteQueueRequest(queueName);
  queueClient.deleteQueue(deleteQueueRequest);

更多更详细的示例请参考各语言SDK下的Example