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 实体框架的一些基础知识,以及构建 ...
随机推荐
- 五十七 POP3收取邮件
SMTP用于发送邮件,如果要收取邮件呢? 收取邮件就是编写一个MUA作为客户端,从MDA把邮件获取到用户的电脑或者手机上.收取邮件最常用的协议是POP协议,目前版本号是3,俗称POP3. Python ...
- 使用jsonp形式跨域访问实现电商平台的左侧导航栏
电商平台有个具备的左侧商品类目的导航栏的结构. 通过jsonp跨域访问电商平台的后台管理系统商品分类.(主要实现后台Java代码) 实现基本步骤: 1.在后台管理系统中准备相应的json数据. poj ...
- jdbc 回顾
JDBC实现基本的CRUD示例 private static void insertTest() throws SQLException { String dbURL = "jdbc:mys ...
- 分享Kali Linux 2017年第31周镜像文件
分享Kali Linux 2017年第31周镜像文件 Kali Linux官方于7月30日发布2017年的第31周镜像.这次维持了11个镜像文件的规模.默认的Gnome桌面的4个镜像,E17.KD ...
- codevs1033 蚯蚓的游戏问题 裸最小费用最大流,注意要拆点
因为蚯蚓走过的路径不能重合,所以把每个点拆成两个点,容量赋为1,保证不会走过相同的点,再加超级源点(程序中为1)和一个辅助点(程序中为2)容量赋为k来控制蚯蚓的数量,最后汇集到一个超级汇点上.做一遍最 ...
- Codeforces 500 E. New Year Domino
\(>Codeforces \space 500 E. New Year Domino<\) 题目大意 : 数轴上有序排列着 \(n\) 块多米诺骨牌, 第 \(i\) 块骨牌坐标为 \( ...
- JZYZOJ1539[haoi2015]T2 树链剖分
http://172.20.6.3/Problem_Show.asp?id=1539 在学校的OJ又写了一次,RE了好多次,原来haoi的时候这道题需要开栈+快读,裸数据结构30分,加上快读50分.o ...
- BZOJ 4247 挂饰(背包问题)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4247 [题目大意] JOI君有N个装在手机上的挂饰,编号为1...N. JOI君可以将 ...
- [CODE FESTIVAL 2017]Full Tournament
题意:$2^n$个编号为$1\cdots2^n$的人打比赛,这个比赛会给每个人一个唯一的排名,比赛规则递归地定义如下: $2^n$个人打的是级别为$n$的比赛,初始时他们按某种顺序站成一排 当$n=0 ...
- 【数论】【二次剩余】【map】hdu6128 Inverse of sum
部分引用自:http://blog.csdn.net/v5zsq/article/details/77255048 所以假设方程 x^2+x+1=0 在模p意义下的解为d,则答案就是满足(ai/aj) ...