rocketmq的线程服务基类
RocketMQ有很多的线程服务,这些服务都继承自抽象类ServiceThread。

这个抽象类可以单独抽出来用到我们其他的项目中来,仅仅需要修改下日志模块:
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ /**
* @author shijia.wxr
*/
public abstract class ServiceThread implements Runnable {
// 抽象类可以不用实现接口中的方法,但是继承改抽象类的类就必须实现了。
private static final long JoinTime = 90 * 1000; protected final Thread thread; protected volatile boolean hasNotified = false; protected volatile boolean stoped = false; public ServiceThread() {
this.thread = new Thread(this, this.getServiceName());
} public abstract String getServiceName(); public void start() {
this.thread.start();
} public void shutdown() {
this.shutdown(false);
} public void shutdown(final boolean interrupt) {
this.stoped = true;
System.out.println();
System.out.println("shutdown thread " + this.getServiceName() + " interrupt " + interrupt);
synchronized (this) {
if (!this.hasNotified) {
this.hasNotified = true;
this.notify();
}
} try {
if (interrupt) {
this.thread.interrupt();
} long beginTime = System.currentTimeMillis();
if (!this.thread.isDaemon()) {
this.thread.join(this.getJointime());
}
long eclipseTime = System.currentTimeMillis() - beginTime;
System.out.println("join thread " + this.getServiceName() + " eclipse time(ms) " + eclipseTime + " "
+ this.getJointime());
} catch (InterruptedException e) {
e.printStackTrace();
}
} public long getJointime() {
return JoinTime;
} public void stop() {
this.stop(false);
} public void stop(final boolean interrupt) {
this.stoped = true;
System.out.println("stop thread " + this.getServiceName() + " interrupt " + interrupt);
synchronized (this) {
if (!this.hasNotified) {
this.hasNotified = true;
this.notify();
}
} if (interrupt) {
this.thread.interrupt();
}
} public void makeStop() {
this.stoped = true;
System.out.println("makestop thread " + this.getServiceName());
} public void wakeup() {
synchronized (this) {
if (!this.hasNotified) {
this.hasNotified = true;
this.notify();
}
}
} protected void waitForRunning(long interval) {
synchronized (this) {
if (this.hasNotified) {
this.hasNotified = false;
this.onWaitEnd();
return;
} try {
this.wait(interval);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
this.hasNotified = false;
this.onWaitEnd();
}
}
} protected void onWaitEnd() {
} public boolean isStoped() {
return stoped;
}
}
使用方法:
继承这个类,需要实现两个方法,一个来自runnable接口的run方法,一个是来自ServiceThread 的getServiceName方法。
getServiceName方法主要是未来用来初始化线程。在上面标黄的位置可以看到。
构造完对象之后调用抽象父类ServiceThread的start方法就能启动线程了(上面标黄红字部分)。
你还可以通过这篇文章来观察一下,在rocketmq中一个完整的使用流程是怎样的:
http://www.cnblogs.com/guazi/p/6850988.html
rocketmq的线程服务基类的更多相关文章
- 请高手解释这个C#程序,其中ServiceBase是windows服务基类,SmsService是
请高手解释这个C#程序,其中ServiceBase是windows服务基类,SmsService是 ServiceBase的子类. static void Main() { ServiceBase[] ...
- ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现
ASP.NET MVC 学习笔记-2.Razor语法 1. 表达式 表达式必须跟在“@”符号之后, 2. 代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...
- 工作线程基类TaskSvc
工作线程基类TaskSvc 前端时间用ACE写代码,发ACE_Task确实好用.不但能提供数量一定的线程,还能够让这些继承的线程函数自由访问子类的private和protected变量.此外,ACE_ ...
- C#WinForm线程基类
在CS模式开发中一般我们需要用到大量的线程来处理比较耗时的操作,以防止界面假死带来不好的体验效果,下面我将我定义的线程基类给大家参考下,如有问题欢迎指正. 基类代码 #region 方法有返回值 // ...
- RTTI、虚函数和虚基类的实现方式、开销分析及使用指导(虚函数的开销很小,就2次操作而已)
白杨 http://baiy.cn “在正确的场合使用恰当的特性” 对称职的C++程序员来说是一个基本标准.想要做到这点,首先要了解语言中每个特性的实现方式及其开销.本文主要讨论相对于传统 C 而言, ...
- 【C# 线程】WaitHandle类
理论 Windows的线程同步方式可分为2种,用户模式构造和内核模式构造.内核模式构造:是由Windows系统本身使用,内核对象进行调度协助的.内核对象是系统地址空间中的一个内存块,由系统创建维护. ...
- 基于SqlSugar的开发框架循序渐进介绍(6)-- 在基类接口中注入用户身份信息接口
在基于SqlSugar的开发框架中,我们设计了一些系统服务层的基类,在基类中会有很多涉及到相关的数据处理操作的,如果需要跟踪具体是那个用户进行操作的,那么就需要获得当前用户的身份信息,包括在Web A ...
- 基于SqlSugar的开发框架循序渐进介绍(10)-- 利用axios组件的封装,实现对后端API数据的访问和基类的统一封装处理
在SqlSugar的开发框架的后端,我们基于Web API的封装了统一的返回结果,使得WebAPI的接口返回值更加简洁,而在前端,我们也需要统一对返回的结果进行解析,并获取和Web API接口对应的数 ...
- Entity Framework 实体框架的形成之旅--基类接口的统一和异步操作的实现(3)
在本系列的第一篇随笔<Entity Framework 实体框架的形成之旅--基于泛型的仓储模式的实体框架(1)>中介绍了Entity Framework 实体框架的一些基础知识,以及构建 ...
随机推荐
- [水煮 ASP.NET Web API2 方法论](1-7)CSRF-Cross-Site Request Forgery
问题 通过 CSRF(Cross-Site Request Forgery)防护,保护从 MVC 页面提交到ASP.NET Web API 的数据. 解决方案 ASP.NET 已经加入了 CSRF 防 ...
- SSH Secure File Transfer上传文件错误:encountered 1 errors during the transfer解决办法
在使用SSH 工具向Linux服务器上传文件时,弹出 encountered 1 errors during the transfer 错误. 解决方案: 1.准备上传的那个文件所在目录路径存在(), ...
- 转:使用IDA动态调试WanaCrypt0r中的tasksche.exe
逆向分析——使用IDA动态调试WanaCrypt0r中的tasksche.exe 转:http://www.4hou.com/technology/4832.html 2017年5月19日发布 导语: ...
- python初步入门
下载并安装python后,将安装路径加到path环境变量中,即可在命令行窗口使用 help(obj) --查看帮助 import 文件名 --导入一个文件 from 文件名 import 方法 ...
- 洛谷——P1276 校门外的树(增强版)
P1276 校门外的树(增强版) 题目描述 校门外马路上本来从编号0到L,每一编号的位置都有1棵树.有砍树者每次从编号A到B处连续砍掉每1棵树,就连树苗也不放过(记 0 A B ,含A和B):幸运的是 ...
- 安装zabbix监控系统
环境 操作系统 最小化安装CentOS Linux release 7.2.1511 IP 192.168.88.1 zabbix版本 zabbix-3.4.4.tar.gz zabbix依赖于LNM ...
- B/S架构和C/S架构
一.B/S架构 B/S结构(Browser/Server,浏览器/服务器模式),是WEB兴起后的一种网络结构模式,WEB浏览器是客户端最主要的应用软件.这种模式统一了客户端,将系统功能实现的核心部分集 ...
- 初见Python<6>:文件读写
1.open函数语法: python通过open函数打开文件,建立程序与文件之间的连接. open函数语法:open(filename[,mode[,buffering]]) 其中filename是指 ...
- BZOJ 2225 [Spoj 2371]Another Longest Increasing(CDQ分治)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2225 [题目大意] 给定N个数对(xi,yi),求最长上升子序列的长度. 上升序列定义 ...
- 【数论】bzoj1968 [Ahoi2005]COMMON 约数研究
对于i属于[1,n],i只能成为[1,n]中n/i个数的约数,易证. #include<stdio.h> int n,i; long long ans; int main() { scan ...