uw.task.api.TaskAPI Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of uw-task Show documentation
Show all versions of uw-task Show documentation
uw-task包是一个分布式任务框架,通过uw-task可以快速构建基于docker的分布式任务体系,同时支持基于influx的任务运维监控。
The newest version!
package uw.task.api;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import uw.task.TaskData;
import uw.task.conf.TaskProperties;
import uw.task.entity.TaskContact;
import uw.task.entity.TaskCronerConfig;
import uw.task.entity.TaskCronerLog;
import uw.task.entity.TaskRunnerConfig;
import uw.task.util.LeaderVote;
/**
* 对应服务器端的API接口实现。
*
* @author axeon
*
*/
@Component
public class TaskAPI {
private static final Logger log = LoggerFactory.getLogger(TaskAPI.class);
@Autowired
private TaskProperties taskProperties;
@Autowired
private RestTemplate restTemplate;
/**
* 专门给日志发送使用的线程池。
*/
private ExecutorService es = Executors
.newCachedThreadPool(new ThreadFactoryBuilder().setDaemon(true).setNameFormat("TaskAPI-%d").build());
/**
* Rest配置
*
* @param builder
* @return
*/
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
/**
* 本机的外网IP
*/
private String hostIp = "";
/**
* 主机配置名
*/
private List hostConfig = null;
/**
* @return the hostIp
*/
public String getHostIp() {
return hostIp;
}
/**
* @param hostConfig the hostConfig to set
*/
public void setHostConfig(List hostConfig) {
this.hostConfig = hostConfig;
}
/**
* 更新当前主机状态,返回主机IP地址。
*/
public String updateHostStatus() {
String ip = "";
MultiValueMap paramMap = new LinkedMultiValueMap<>();
paramMap.add("uuid", LeaderVote.UUID);
try {
ip = restTemplate.postForObject(taskProperties.getServer().getHost() + "/taskapi/host/status", paramMap,
String.class);
hostIp = ip;
} catch (Exception e) {
log.error("TaskAPI.updateHostStatus()服务端主机状态更新异常:{}", e.getMessage());
}
return ip;
}
/**
* 初始化CronerConfig。
*/
public void initTaskCronerConfig(TaskCronerConfig config){
try {
restTemplate.postForLocation(taskProperties.getServer().getHost() + "/taskapi/croner/config", config);
} catch (Exception e) {
log.error("TaskAPI.initTaskCronerConfig上传Croner配置到服务端异常:{}", e.getMessage());
}
}
/**
* 初始化RunnerConfig
*/
public void initTaskRunnerConfig(TaskRunnerConfig config){
try {
restTemplate.postForLocation(taskProperties.getServer().getHost() + "/taskapi/runner/config", config);
} catch (Exception e) {
log.error("TaskAPI.initTaskRunnerConfig上传Runner配置到服务端异常:{}", e.getMessage());
}
}
/**
* 初始化联系人信息。
*/
public void initTaskContact(TaskContact contact){
try {
restTemplate.postForLocation(taskProperties.getServer().getHost() + "/taskapi/contact", contact);
} catch (Exception e) {
log.error("TaskAPI.initTaskContact上传联系人信息到服务端异常:{}", e.getMessage());
}
}
/**
* 更新当前主机设置。
*/
public List getHostConfig() {
MultiValueMap paramMap = new LinkedMultiValueMap<>();
try {
String[] data = restTemplate.getForObject(taskProperties.getServer().getHost() + "/taskapi/host/config", String[].class, paramMap);
hostConfig = Arrays.asList(data);
} catch (Exception e) {
log.error("TaskAPI.getHostConfig()服务端主机配置拉取异常:{}", e.getMessage());
}
if (hostConfig==null) {
if (log.isWarnEnabled()) {
log.warn("获得主机服务端配置失败,启用默认配置项!");
}
hostConfig = new ArrayList<>();
hostConfig.add("");
}
return hostConfig;
}
/**
* 根据包名前缀获得TaskRunner配置列表。
*
* @param prefix
* @return
*/
public List getTaskRunnerConfigList(String taskPackage, long lastUpdateTime) {
HashMap paramMap = new HashMap<>();
paramMap.put("lastUpdateTime", String.valueOf(lastUpdateTime));
paramMap.put("taskPackage", taskPackage);
List list = null;
try {
TaskRunnerConfig[] data = restTemplate.getForObject(taskProperties.getServer().getHost() + "/taskapi/croner/config?taskPackage={taskPackage}&lastUpdateTime={lastUpdateTime}", TaskRunnerConfig[].class,paramMap);
list = Arrays.asList(data);
} catch (Exception e) {
log.error("TaskAPI.getTaskRunnerConfigList()服务端主机状态更新异常:{}", e.getMessage());
}
return list;
}
/**
* 根据包名前缀获得TaskCroner配置列表。
*
* @param prefix
* @return
*/
public List getTaskCronerConfigList(String taskPackage, long lastUpdateTime) {
HashMap paramMap = new HashMap<>();
paramMap.put("lastUpdateTime", String.valueOf(lastUpdateTime));
paramMap.put("taskPackage", taskPackage);
List list = null;
try {
TaskCronerConfig[] data = restTemplate.getForObject(taskProperties.getServer().getHost() + "/taskapi/croner/config?taskPackage={taskPackage}&lastUpdateTime={lastUpdateTime}", TaskCronerConfig[].class,paramMap);
list = Arrays.asList(data);
} catch (Exception e) {
log.error("TaskAPI.getTaskCronerConfigList()服务端主机状态更新异常:{}", e.getMessage());
}
return list;
}
/**
* 发送Runner任务日志。
*
* @param taskData
*/
public void sendTaskRunnerLog(TaskData, ?> taskData) {
es.submit(new Runnable() {
@Override
public void run() {
try {
restTemplate.postForLocation(taskProperties.getServer().getHost() + "/taskapi/runner/log", taskData);
} catch (Exception e) {
log.error("TaskAPI.sendTaskRunnerLog日志发送到服务端异常:{}", e.getMessage());
}
}
});
}
/**
* 发送CronLog日志。
*
* @param taskCronLog
*/
public void sendTaskCronerLog(TaskCronerLog taskCronerLog) {
es.submit(new Runnable() {
@Override
public void run() {
try {
restTemplate.postForLocation(taskProperties.getServer().getHost() + "/taskapi/croner/log", taskCronerLog);
} catch (Exception e) {
log.error("TaskAPI.sendTaskCronerLog日志发送到服务端异常:{}", e.getMessage());
}
}
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy