单线程service服务
1.@Service修饰类名,同时类继承Thread类
@Service
public class MasterSchedulerService extends Thread {
/**
* logger of MasterSchedulerService
*/
private static final Logger logger = LoggerFactory.getLogger(MasterSchedulerService.class);
@Autowired
private ServerConfig serverConfig;
/**
* dolphinscheduler database interface
*/
@Autowired
private ProcessService processService;
/**
* zookeeper master client
*/
@Autowired
private ZKClient zkClient;
/**
* master exec service
*/
private ThreadPoolExecutor masterExecService;
@Autowired
private FamaVmMapper vmMapper;
/**
* constructor of MasterSchedulerService
*/
@PostConstruct
public void init(){
this.masterExecService = (ThreadPoolExecutor) ThreadUtils.newDaemonFixedThreadExecutor("Server-Thread"
, serverConfig.getMasterExecThreads());
}
@Override
public synchronized void start(){
super.setName("MasterSchedulerService");
super.start();
}
public void close() {
masterExecService.shutdown();
boolean terminated = false;
try {
terminated = masterExecService.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException ignore) {
Thread.currentThread().interrupt();
}
if(!terminated){
logger.warn("masterExecService shutdown without terminated, increase await time");
}
logger.info("master schedule service stopped...");
}
/**
* run of MasterSchedulerService
*/
@Override
public void run() {
logger.info("master scheduler started");
while (Stopper.isRunning()){
try {
if (zkClient.getZkClient().getState() == CuratorFrameworkState.STARTED) {
scheduleProcess();
}
} catch (Exception e) {
logger.error("master scheduler thread error", e);
}
}
}
private void scheduleProcess() throws Exception {
InterProcessMutex mutex = null;
try {
mutex = zkClient.blockAcquireMutex();
// make sure to scan and delete command table in one transaction
Command command = processService.findOneCommand();
if (command != null) {
logger.info("find one command: {}", JsonUtils.toJSONString(command));
try {
String address = getLocalAddress();
ResourceInstance processInstance = processService.handleCommand(address,command);
logger.info("processInstance: {}", JsonUtils.toJSONString(processInstance));
if (processInstance != null) {
MapperContext context = new MapperContext();
context.vmMapper = vmMapper;
MasterExecThread execThread = new MasterExecThread(
processInstance
, processService
, serverConfig
, context);
masterExecService.execute(execThread);
}
} catch (Exception e) {
logger.error("Command consumer error ", e);
// processService.moveToErrorCommand(command, e.toString());
}
} else {
//indicate that no command ,sleep for 1s
Thread.sleep(Constants.SLEEP_TIME_MILLIS);
}
} finally {
zkClient.releaseMutex(mutex);
}
}
private String getLocalAddress() {
return NetUtils.getAddr(serverConfig.getListenPort());
}
}
2.执行时,直接执行start方法
@ComponentScan(value = {"fama.cost"})
public class FamaServerApplication implements IStoppable {
private static final Logger logger = LoggerFactory.getLogger(FamaServerApplication.class);
@Autowired
private MasterSchedulerService schedulerService;
@Autowired
private SpringApplicationContext springApplicationContext;
@Autowired
private BeanContext beanContext;
@Autowired
private ZKClient zkClient;
public static void main(String[] args) {
Thread.currentThread().setName("Schedule-Server");
new SpringApplicationBuilder(FamaServerApplication.class).web(WebApplicationType.NONE).run(args);
int currentProcessPid = getProcessID();
logger.info("Current Process Pid : {}",currentProcessPid);
}
@PostConstruct
public void run() {
this.zkClient.start(this);
this.schedulerService.start();
try {
logger.info("start Quartz server...");
QuartzExecutors.getInstance().start();
} catch (Exception e) {
try {
QuartzExecutors.getInstance().shutdown();
} catch (SchedulerException e1) {
logger.error("QuartzExecutors shutdown failed : " + e1.getMessage(), e1);
}
logger.error("start Quartz failed", e);
}
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
if (Stopper.isRunning()) {
close("shutdownHook");
}
}
}));
}
public void close(String cause) {
try {
//execute only once
if (Stopper.isStopped()) {
return;
}
logger.info("master server is stopping ..., cause : {}", cause);
// set stop signal is true
Stopper.stop();
try {
//thread sleep 3 seconds for thread quietly stop
Thread.sleep(3000L);
} catch (Exception e) {
logger.warn("thread sleep exception ", e);
}
//close
this.schedulerService.close();
this.zkClient.close();
//close quartz
try {
QuartzExecutors.getInstance().shutdown();
logger.info("Quartz service stopped");
} catch (Exception e) {
logger.warn("Quartz service stopped exception:{}", e.getMessage());
}
} catch (Exception e) {
logger.error("master server stop exception ", e);
} finally {
// System.exit(-1);
}
}
@Override
public void stop(String cause) {
close(cause);
}
public static final int getProcessID() {
try {
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
return Integer.valueOf(runtimeMXBean.getName().split("@")[0]).intValue();
}catch (Exception ex){
logger.error(ex.getMessage(),ex);
return 0;
}
}
}
单线程service服务的更多相关文章
- 解决VMWARE NAT SERVICE服务无法启动或服务消失的问题
解决VMWARE NAT SERVICE服务无法启动或服务消失的问题 2016-02-02 11:18 2012人阅读 评论(2) 收藏 举报 分类: 网络通信(3) 今日使用VMware中的Wi ...
- 使用axis开发web service服务端
一.axis环境搭建 1.安装环境 JDK.Tomcat或Resin.eclipse等. 2.到 http://www.apache.org/dyn/closer.cgi/ws/axis/1_4下载A ...
- win7提示“User Profile Service服务未能登录”
注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 最近,有个同事打电话告诉我说他的用户名无法登陆到系统,提示“User Profile Service服务未能登录,无法加载用户配置文 ...
- Android 通过JNI实现守护进程,使得Service服务不被杀死
来自: http://finalshares.com/read-7306 转载请注明出处: http://blog.csdn.net/yyh352091626/article/details/5054 ...
- Service服务
Android多线程: 定义线程的2种方式: 1.继承Thread类,重写run()方法,new一个实例,用start()方法启动:new MyThread().start(); 2.实现Runnab ...
- Centos6.5 设置Tomcat8 service服务实现自启动和服务管理
Centos6.5 设置Tomcat8 service服务实现自启动和服务管理 将tomcat设置成像apache,nginx一样. 用serviec xxxx start/stop/restart ...
- Android 综合揭秘 —— 全面剖释 Service 服务
引言 Service 服务是 Android 系统最常用的四大部件之一,Android 支持 Service 服务的原因主要目的有两个,一是简化后台任务的实现,二是实现在同一台设备当中跨进程的远程信息 ...
- win8.1 user profile service 服务登录失败
在Win 8.1 上新建个用户后,不能登录. 出现 user profile service 服务登录失败. 无法加载用户配置文件. 网上大部分相同提示的问题是有关已有账号不能再次登陆的. 解决方式是 ...
- Android中Service(服务)详解
http://blog.csdn.net/ryantang03/article/details/7770939 Android中Service(服务)详解 标签: serviceandroidappl ...
随机推荐
- 一篇博文让你学会HTML5
HTML 什么是HTML HTML(HyperText Markup Language,超文本标记语言)是用来描述网页的一种语言,它不是一种编程语言,而是一种标记语言. 认识HTML HTML5是构建 ...
- css3动画大全
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- WindowsPE 第七章 资源表
资源表 在程序设计中,总会设计一些数据.这些数据可能是源代码内部需要用到的常量,菜单选项.界面描述等:也可能是源代码外部的,比如程序的图标文件.北京音乐文件.配置文件等,以上这些数据统称为资源.按照程 ...
- 狂神说Elasticsearch7.X学习笔记整理
Elasticsearch概述 一.什么是Elasticsearch? Lucene简介 Lucene是一套用于全文检索和搜寻的开源程序库,由Apache软件基金会支持和提供 Lucene提供了一个简 ...
- web.xml常用配置详解
web.xml常用配置详解 context-param 指定 ServletContext(上下文) 配置文件路径,基本配置一般是Spring配置文件,或者是spring-security的配置文件. ...
- web&HTML
内容索引 1. web概念概述 2. HTML web概念概述 * JavaWeb: * 使用Java语言开发基于互联网的项目 * 软件架构: 1. C/S: Client/Server 客户端/服务 ...
- Linux Limit相关内容设置大全(值得收藏)
目录 一. /etc/security/limits.conf 详解 /etc/security/limits.conf 配置解析 /etc/security/limits.d/目录 二. ulimi ...
- C++ primer plus读书笔记——第8章 函数探幽
第8章 函数探幽 1. 对于内联函数,编译器将使用相应的函数代码替换函数调用,程序无需跳到一个位置执行代码,再调回来.因此,内联函数的运行速度比常规函数稍快,但代价是需要占用更多内存. 2. 要使用内 ...
- 三分钟了解B2B CRM系统的特点
最近很多朋友想了解什么是B2B CRM系统,说到这里小Z先来给大家说说什么是B2B--B2B原本写作B to B,是Business-to-Business的缩写.正常来说就是企业与企业之间的生意往来 ...
- 使用PuTTY连接Azure VM
使用PuTTY连接Azure VMhtml { -webkit-print-color-adjust: exact } * { box-sizing: border-box; -webkit-prin ...