package com.zm.order.tool.server.config;

import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.order.OrderProducer;
import com.zm.order.tool.server.model.OnsProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * @Description ons 配置
 * @Author: ph
 * @Date: 2019/12/5 09:44
 */
@Configuration
@EnableConfigurationProperties(OnsProperty.class)
public class OnsConfig {

    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public Producer getProducer(OnsProperty onsProperty) {
        Properties properties = new Properties();
        properties.setProperty(PropertyKeyConst.GROUP_ID, onsProperty.getGroupId());
        // AccessKey 阿里云身份验证，在阿里云服务器管理控制台创建
        properties.put(PropertyKeyConst.AccessKey, onsProperty.getAccessKey());
        // SecretKey 阿里云身份验证，在阿里云服务器管理控制台创建
        properties.put(PropertyKeyConst.SecretKey, onsProperty.getSecretKey());
        properties.put(PropertyKeyConst.NAMESRV_ADDR, onsProperty.getNameSrvAddr());
        properties.put(PropertyKeyConst.SendMsgTimeoutMillis, onsProperty.getSendMsgTimeoutMillis());
        Producer producer = ONSFactory.createProducer(properties);
        return producer;
    }

    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public OrderProducer getOrderProducer(OnsProperty onsProperty) {
        Properties properties = new Properties();
        properties.setProperty(PropertyKeyConst.GROUP_ID, onsProperty.getOrderlyGroupId());
        // AccessKey 阿里云身份验证，在阿里云服务器管理控制台创建
        properties.put(PropertyKeyConst.AccessKey, onsProperty.getAccessKey());
        // SecretKey 阿里云身份验证，在阿里云服务器管理控制台创建
        properties.put(PropertyKeyConst.SecretKey, onsProperty.getSecretKey());
        properties.put(PropertyKeyConst.NAMESRV_ADDR, onsProperty.getNameSrvAddr());
        properties.put(PropertyKeyConst.SendMsgTimeoutMillis, onsProperty.getSendMsgTimeoutMillis());
        OrderProducer producer = ONSFactory.createOrderProducer(properties);
        return producer;
    }

}
