package com.xiaomi.infra.galaxy.emq.example;

import java.util.ArrayList;
import java.util.List;

import libthrift091.TException;

import com.xiaomi.infra.galaxy.emq.client.EMQClientFactory;
import com.xiaomi.infra.galaxy.emq.thrift.ChangeMessageVisibilityBatchRequest;
import com.xiaomi.infra.galaxy.emq.thrift.ChangeMessageVisibilityBatchRequestEntry;
import com.xiaomi.infra.galaxy.emq.thrift.CreateQueueRequest;
import com.xiaomi.infra.galaxy.emq.thrift.CreateQueueResponse;
import com.xiaomi.infra.galaxy.emq.thrift.CreateTagRequest;
import com.xiaomi.infra.galaxy.emq.thrift.DeleteMessageBatchRequest;
import com.xiaomi.infra.galaxy.emq.thrift.DeleteMessageBatchRequestEntry;
import com.xiaomi.infra.galaxy.emq.thrift.DeleteQueueRequest;
import com.xiaomi.infra.galaxy.emq.thrift.DeleteTagRequest;
import com.xiaomi.infra.galaxy.emq.thrift.GalaxyEmqServiceException;
import com.xiaomi.infra.galaxy.emq.thrift.MessageService;
import com.xiaomi.infra.galaxy.emq.thrift.QueueService;
import com.xiaomi.infra.galaxy.emq.thrift.ReceiveMessageRequest;
import com.xiaomi.infra.galaxy.emq.thrift.ReceiveMessageResponse;
import com.xiaomi.infra.galaxy.emq.thrift.SendMessageRequest;
import com.xiaomi.infra.galaxy.emq.thrift.SendMessageResponse;
import com.xiaomi.infra.galaxy.rpc.thrift.Credential;
import com.xiaomi.infra.galaxy.rpc.thrift.UserType;

/**
 * Copyright 2015, Xiaomi.
 * All rights reserved.
 * Author: shenyuannan@xiaomi.com
 */

/* This JAVA SDK support Java6 and Java7; Java8 is not included yet */

public class EMQExample {
  private static String secretKeyId = ""; // Set your AppKey, like "5521728135794"

  private static String secretKey = ""; // Set your AppSecret, like "K7czwCuHttwZD49DD/qKzg=="
  private static String name = "testClient";

  public static void main(String[] args) {
    Credential credential = new Credential().setSecretKeyId(secretKeyId).
        setSecretKey(secretKey).setType(UserType.APP_SECRET);
    EMQClientFactory clientFactory = new EMQClientFactory(credential);
    QueueService.Iface queueClient = clientFactory.newQueueClient(
        "http://awsbj0.emq.api.xiaomi.com");
    MessageService.Iface messageClient = clientFactory.newMessageClient(
        "http://awsbj0.emq.api.xiaomi.com");

    try {
      CreateQueueRequest createQueueRequest = new CreateQueueRequest(name);
      CreateQueueResponse createQueueResponse = queueClient.createQueue(
          createQueueRequest);
      String queueName = createQueueResponse.getQueueName();

      String tagName = "tagTest";
      CreateTagRequest createTagRequest = new CreateTagRequest(queueName, tagName);
      queueClient.createTag(createTagRequest);

      String messageBody = "EMQExample";
      SendMessageRequest sendMessageRequest =
          new SendMessageRequest(queueName, messageBody);
      SendMessageResponse sendMessageResponse =
          messageClient.sendMessage(sendMessageRequest);
      System.out.printf("Send:\n  MessageBody: %s  MessageId: %s\n\n",
          messageBody, sendMessageResponse.getMessageID());

      ReceiveMessageRequest receiveMessageRequest =
          new ReceiveMessageRequest(queueName);
      List<ReceiveMessageResponse> receiveMessageResponse =
          new ArrayList<ReceiveMessageResponse>();
      while (receiveMessageResponse.isEmpty()) {
        receiveMessageResponse =
            messageClient.receiveMessage(receiveMessageRequest);
      }
      DeleteMessageBatchRequest deleteMessageBatchRequest =
          new DeleteMessageBatchRequest();
      deleteMessageBatchRequest.setQueueName(queueName);
      for (ReceiveMessageResponse response : receiveMessageResponse) {
        System.out.printf(
            "Receive from default:\n  MessageBody: %s  MessageId: %s " +
                "ReceiptHandle: %s\n\n",
            response.getMessageBody(), response.getMessageID(),
            response.getReceiptHandle());
        deleteMessageBatchRequest.addToDeleteMessageBatchRequestEntryList(
            new DeleteMessageBatchRequestEntry(response.getReceiptHandle()));
      }
      messageClient.deleteMessageBatch(deleteMessageBatchRequest);

      receiveMessageRequest = new ReceiveMessageRequest(queueName);
      receiveMessageRequest.setTagName(tagName);
      receiveMessageResponse.clear();
      while (receiveMessageResponse.isEmpty()) {
        receiveMessageResponse =
            messageClient.receiveMessage(receiveMessageRequest);
      }
      ChangeMessageVisibilityBatchRequest changeRequest =
          new ChangeMessageVisibilityBatchRequest();
      changeRequest.setQueueName(queueName);
      for (ReceiveMessageResponse response : receiveMessageResponse) {
        System.out.printf(
            "Receive from tag:\n  MessageBody: %s  MessageId: %s " +
                "ReceiptHandle: %s\n\n",
            response.getMessageBody(), response.getMessageID(),
            response.getReceiptHandle());
        changeRequest.addToChangeMessageVisibilityRequestEntryList(
            new ChangeMessageVisibilityBatchRequestEntry(
                response.getReceiptHandle(), 0));
      }

      messageClient.changeMessageVisibilitySecondsBatch(changeRequest);
      System.out.printf("Change Visibility.\n\n");

      receiveMessageRequest.setMaxReceiveMessageWaitSeconds(5);
      receiveMessageResponse = messageClient.receiveMessage(receiveMessageRequest);
      for (ReceiveMessageResponse response : receiveMessageResponse) {
        System.out.printf(
            "Receive from tag:\n  MessageBody: %s  MessageId: %s ReceiptHandle: %s\n\n",
            response.getMessageBody(), response.getMessageID(),
            response.getReceiptHandle());
      }

      if (!receiveMessageResponse.isEmpty()) {
        deleteMessageBatchRequest = new DeleteMessageBatchRequest();
        deleteMessageBatchRequest.setQueueName(queueName);
        for (ReceiveMessageResponse response : receiveMessageResponse) {
          deleteMessageBatchRequest.addToDeleteMessageBatchRequestEntryList(
              new DeleteMessageBatchRequestEntry(response.getReceiptHandle()));
        }
        messageClient.deleteMessageBatch(deleteMessageBatchRequest);
        System.out.print("Delete Messages.\n\n");
      }

      DeleteTagRequest deleteTagRequest = new DeleteTagRequest(queueName, tagName);
      queueClient.deleteTag(deleteTagRequest);

      DeleteQueueRequest deleteQueueRequest = new DeleteQueueRequest(queueName);
      queueClient.deleteQueue(deleteQueueRequest);
    } catch (TException e) {
      if (e instanceof GalaxyEmqServiceException) {
        GalaxyEmqServiceException ex = (GalaxyEmqServiceException) e;
        System.out.printf("Failed. Reason:" + ex.getErrMsg() + "\n" +
            ex.getDetails() + " requestId=" + ex.getRequestId() + "\n\n");
      } else {
        System.out.printf("Failed." + e.getMessage() + "\n\n");
      }
    }
  }
}

@@ -1343,16 +1343,103 @@
 rType;%0A%0A
+import static com.xiaomi.infra.galaxy.emq.client.EMQClientFactory.generateHttpClient;%0A%0A
 /**%0A * C
@@ -1702,13 +1702,13 @@
 7281
+2
 3579
-4
 %22%0A%0A 
@@ -1786,16 +1786,17 @@
 zwCuHttw
+r
 ZD49DD/q
@@ -1797,17 +1797,16 @@
 49DD/qKz
-g
 ==%22%0A  pr
@@ -2094,16 +2094,52 @@
 edential
+,%0A        generateHttpClient(10, 10)
 );%0A    Q

Update SDK example about httpclient.