// Path: mq-aliyun-core/src/main/java/com/github/mq/core/scan/constant/MqConstant.java
// public interface MqConstant {
//     String ACCESS_KEY = "aliyun.accessKey";
//     String SECRET_KEY = "aliyun.secretKey";
//     String MQ_SUFFIX = "aliyun.mq.suffix";
// 
// }
// 
// Path: mq-aliyun-producer-spring-boot-starter/src/main/java/com/github/mq/producer/ProducerFactory.java
// public interface ProducerFactory {
//     void sendAsync(final Enum address,final Object message);
//     void sendAsync(final Enum address,final Object message, final DeliveryOption deliveryOption);
//     void sendAsync(final Enum address,final Object message, final SendCallback sendCallback);
//     void sendAsync(final Enum address, final Object message, final DeliveryOption deliveryOption, final SendCallback sendCallback);
// 
//     SendResult orderSend(final Enum address, final Object message, final String shardingKey);
//     SendResult orderSend(final Enum address, final Object message, final String shardingKey,final DeliveryOption deliveryOption);
// 
//     Producer getProducer(String pid);
//     OrderProducer getOrderProducer(String pid);
//     void addProducer(String pid);
//     void addOrdereProducer(String pid);
// }
// 
// Path: mq-aliyun-producer-spring-boot-starter/src/main/java/com/github/mq/producer/ProducerSerialize.java
// public interface ProducerSerialize {
// 
//     byte[] objToBytes(Object object);
// }
// 
// Path: mq-aliyun-producer-spring-boot-starter/src/main/java/com/github/mq/producer/models/DeliveryOption.java
// public class DeliveryOption {
//     private String key;
//     private Long deliverTime;
//     public String getKey() {
//         return key;
//     }
// 
//     public DeliveryOption(){}
//     public DeliveryOption(String key){
//         this.key = key;
//     }
// 
//     public DeliveryOption setKey(String key) {
//         this.key = key;
//         return this;
//     }
// 
//     public Long getDeliverTime() {
//         return deliverTime;
//     }
// 
//     public DeliveryOption setDeliverTime(Long deliverTime) {
//         this.deliverTime = deliverTime;
//         return this;
//     }
// }
// 
// Path: mq-aliyun-producer-spring-boot-starter/src/main/java/com/github/mq/producer/models/PendingMsg.java
// public class PendingMsg {
//     private Enum anEnum;
//     private Object message;
//     private DeliveryOption deliveryOption;
//     private SendCallback sendCallback;
// 
//     public PendingMsg(){}
// 
//     public PendingMsg(Enum anEnum,Object message,DeliveryOption deliveryOption,SendCallback sendCallback){
//         this.anEnum =anEnum;
//         this.message = message;
//         this.deliveryOption = deliveryOption;
//         this.sendCallback = sendCallback;
//     }
// 
// 
//     public Enum getAnEnum() {
//         return anEnum;
//     }
// 
//     public void setAnEnum(Enum anEnum) {
//         this.anEnum = anEnum;
//     }
// 
//     public Object getMessage() {
//         return message;
//     }
// 
//     public void setMessage(Object message) {
//         this.message = message;
//     }
// 
//     public DeliveryOption getDeliveryOption() {
//         return deliveryOption;
//     }
// 
//     public void setDeliveryOption(DeliveryOption deliveryOption) {
//         this.deliveryOption = deliveryOption;
//     }
// 
//     public SendCallback getSendCallback() {
//         return sendCallback;
//     }
// 
//     public void setSendCallback(SendCallback sendCallback) {
//         this.sendCallback = sendCallback;
//     }
// }


import com.aliyun.openservices.ons.api.*;
import com.aliyun.openservices.ons.api.order.OrderProducer;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSONObject;
import com.github.mq.core.scan.constant.MqConstant;
import com.github.mq.producer.ProducerFactory;
import com.github.mq.producer.ProducerSerialize;
import com.github.mq.producer.models.DeliveryOption;
import com.github.mq.producer.models.PendingMsg;
import com.github.mq.producer.models.Pid;
import com.github.mq.producer.models.To;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import java.util.*;
import static java.lang.Thread.sleep;
        Message msg = new Message(
                to.topic() + suffix,
                // 可理解为Gmail中的标签，对消息进行再归类，方便Consumer指定过滤条件在ONS服务器过滤
                to.tag().trim(),
                // Message Body
                // 任何二进制形式的数据， ONS不做任何干预，
                // 需要Producer与Consumer协商好一致的序列化和反序列化方式
                producerSerialize.objToBytes(message));

        if (null != deliveryOption) {
            String key = deliveryOption.getKey();
            if (!Strings.isNullOrEmpty(key)) {
                msg.setKey(key);
            }

            Long deliverTime = deliveryOption.getDeliverTime();

            if (null != deliverTime && 0 != deliverTime)
                msg.setStartDeliverTime(deliverTime);
        }
        return new MessageBuild(pid.value(), msg);
    }

    public void init(Environment env) {
        String package_ = env.getProperty(MQ_PRODUCER_PACKAGES);
        if (null == package_) {
            throw new RuntimeException(String.format("mq生产者 启动失败, %s is require", MQ_PRODUCER_PACKAGES));
        }
        packages = package_.split(",");

// Path: mq-aliyun-core/src/main/java/com/github/mq/core/scan/constant/MqConstant.java
// public interface MqConstant {
//     String ACCESS_KEY = "aliyun.accessKey";
//     String SECRET_KEY = "aliyun.secretKey";
//     String MQ_SUFFIX = "aliyun.mq.suffix";
// 
// }
// 
// Path: mq-aliyun-producer-spring-boot-starter/src/main/java/com/github/mq/producer/ProducerFactory.java
// public interface ProducerFactory {
//     void sendAsync(final Enum address,final Object message);
//     void sendAsync(final Enum address,final Object message, final DeliveryOption deliveryOption);
//     void sendAsync(final Enum address,final Object message, final SendCallback sendCallback);
//     void sendAsync(final Enum address, final Object message, final DeliveryOption deliveryOption, final SendCallback sendCallback);
// 
//     SendResult orderSend(final Enum address, final Object message, final String shardingKey);
//     SendResult orderSend(final Enum address, final Object message, final String shardingKey,final DeliveryOption deliveryOption);
// 
//     Producer getProducer(String pid);
//     OrderProducer getOrderProducer(String pid);
//     void addProducer(String pid);
//     void addOrdereProducer(String pid);
// }
// 
// Path: mq-aliyun-producer-spring-boot-starter/src/main/java/com/github/mq/producer/ProducerSerialize.java
// public interface ProducerSerialize {
// 
//     byte[] objToBytes(Object object);
// }
// 
// Path: mq-aliyun-producer-spring-boot-starter/src/main/java/com/github/mq/producer/models/DeliveryOption.java
// public class DeliveryOption {
//     private String key;
//     private Long deliverTime;
//     public String getKey() {
//         return key;
//     }
// 
//     public DeliveryOption(){}
//     public DeliveryOption(String key){
//         this.key = key;
//     }
// 
//     public DeliveryOption setKey(String key) {
//         this.key = key;
//         return this;
//     }
// 
//     public Long getDeliverTime() {
//         return deliverTime;
//     }
// 
//     public DeliveryOption setDeliverTime(Long deliverTime) {
//         this.deliverTime = deliverTime;
//         return this;
//     }
// }
// 
// Path: mq-aliyun-producer-spring-boot-starter/src/main/java/com/github/mq/producer/models/PendingMsg.java
// public class PendingMsg {
//     private Enum anEnum;
//     private Object message;
//     private DeliveryOption deliveryOption;
//     private SendCallback sendCallback;
// 
//     public PendingMsg(){}
// 
//     public PendingMsg(Enum anEnum,Object message,DeliveryOption deliveryOption,SendCallback sendCallback){
//         this.anEnum =anEnum;
//         this.message = message;
//         this.deliveryOption = deliveryOption;
//         this.sendCallback = sendCallback;
//     }
// 
// 
//     public Enum getAnEnum() {
//         return anEnum;
//     }
// 
//     public void setAnEnum(Enum anEnum) {
//         this.anEnum = anEnum;
//     }
// 
//     public Object getMessage() {
//         return message;
//     }
// 
//     public void setMessage(Object message) {
//         this.message = message;
//     }
// 
//     public DeliveryOption getDeliveryOption() {
//         return deliveryOption;
//     }
// 
//     public void setDeliveryOption(DeliveryOption deliveryOption) {
//         this.deliveryOption = deliveryOption;
//     }
// 
//     public SendCallback getSendCallback() {
//         return sendCallback;
//     }
// 
//     public void setSendCallback(SendCallback sendCallback) {
//         this.sendCallback = sendCallback;
//     }
// }


// Path: mq-aliyun-producer-spring-boot-starter/src/main/java/com/github/mq/producer/impls/ProducerFactoryImpl.java
import com.aliyun.openservices.ons.api.*;
import com.aliyun.openservices.ons.api.order.OrderProducer;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSONObject;
import com.github.mq.core.scan.constant.MqConstant;
import com.github.mq.producer.ProducerFactory;
import com.github.mq.producer.ProducerSerialize;
import com.github.mq.producer.models.DeliveryOption;
import com.github.mq.producer.models.PendingMsg;
import com.github.mq.producer.models.Pid;
import com.github.mq.producer.models.To;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import java.util.*;
import static java.lang.Thread.sleep;

        Message msg = new Message(
                to.topic() + suffix,
                // 可理解为Gmail中的标签，对消息进行再归类，方便Consumer指定过滤条件在ONS服务器过滤
                to.tag().trim(),
                // Message Body
                // 任何二进制形式的数据， ONS不做任何干预，
                // 需要Producer与Consumer协商好一致的序列化和反序列化方式
                producerSerialize.objToBytes(message));

        if (null != deliveryOption) {
            String key = deliveryOption.getKey();
            if (!Strings.isNullOrEmpty(key)) {
                msg.setKey(key);
            }

            Long deliverTime = deliveryOption.getDeliverTime();

            if (null != deliverTime && 0 != deliverTime)
                msg.setStartDeliverTime(deliverTime);
        }
        return new MessageBuild(pid.value(), msg);
    }

    public void init(Environment env) {
        String package_ = env.getProperty(MQ_PRODUCER_PACKAGES);
        if (null == package_) {
            throw new RuntimeException(String.format("mq生产者 启动失败, %s is require", MQ_PRODUCER_PACKAGES));
        }
        packages = package_.split(",");

        accessKey = env.getProperty(MqConstant.ACCESS_KEY);