package com.xxl.job.executor;

import com.alibaba.dubbo.common.utils.ConfigUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Arrays;
import java.util.Properties;

/**
 * @author xuxueli 2018-10-28 00:38:13
 */
@SpringBootApplication
public class XxlJobExecutorApplication {

	public static void main(String[] args) {
		initZookeeperAddres(args);
		setAdminAddres(args);
		SpringApplication.run(XxlJobExecutorApplication.class, args);
	}

	private static void setAdminAddres(String[] args){
		// xxl.job.admin.addresses=http://127.0.0.1:8181/xxl-job-admin
		String adminAddres = getAdminAddres(args);
		System.out.println(adminAddres);
		System.setProperty("xxl.job.admin.addresses", adminAddres);
	}

	private static String getAdminAddres(String[] args){
		if (args.length < 2){
			throw new RuntimeException("请输入调度中心的端口，如果是多个（集群）使用逗号(,)进行间隔");
		}
		String[] portArr = args[1].split(",");
		StringBuilder resultBuilder = new StringBuilder();
		for (String port : portArr) {
			resultBuilder.append("http://127.0.0.1:" + port + "/xxl-job-admin,");
		}
		String resultStr = resultBuilder.deleteCharAt(resultBuilder.length() - 1).toString();
		return resultStr;
	}

	/**
	 * 促使化dubbo注册中心的地址
	 * @param args
	 */
	private static void initZookeeperAddres(String[] args){
		if(args == null || args.length == 0){
			throw new RuntimeException("请传入zookeeper的地址");
		}
		String addres = "zookeeper://" + args[0];
		Properties properties = new Properties();
		properties.setProperty("dubbo.registry.address",addres);
		ConfigUtils.addProperties(properties);
	}

}