java中基于TaskEngine类封装实现定时任务
主要包括如下几个类:
文章标题:java中基于TaskEngine类封装实现定时任务
文章地址: http://blog.csdn.net/5iasp/article/details/10950529
作者: javaboy2012
Email:yanek@163.com
qq: 1046011462
1. 核心工具类: TaskEngine
package com.yanek.task; import java.util.*;
import java.util.LinkedList; import org.apache.log4j.Logger; public class TaskEngine
{
static Logger logger = Logger.getLogger(TaskEngine.class.getName());
private static class PriorityQueue
{ public void enqueue(int priority, Object object)
{
if(priority > HIGH_PRIORITY)
priority = HIGH_PRIORITY;
else
if(priority < LOW_PRIORITY)
priority = LOW_PRIORITY;
switch(priority)
{
case HIGH_PRIORITY: // '\002'
high.addFirst(object);
break; case MEDIUM_PRIORITY: // '\001'
medium.addFirst(object);
break; case LOW_PRIORITY: // '\0'
low.addFirst(object);
break;
}
} public boolean isEmpty()
{
return high.isEmpty() && medium.isEmpty() && low.isEmpty();
} public int size()
{
return high.size() + medium.size() + low.size();
} public Object dequeue()
{
Object object;
if(!high.isEmpty())
object = high.removeLast();
else
if(!medium.isEmpty())
object = medium.removeLast();
else
if(!low.isEmpty())
object = low.removeLast();
else
throw new NoSuchElementException("Queue is empty.");
if(!low.isEmpty())
medium.addFirst(low.removeLast());
if(!medium.isEmpty())
high.addFirst(medium.removeLast());
return object;
} private LinkedList high;
private LinkedList medium;
private LinkedList low; private PriorityQueue()
{
high = new LinkedList();
medium = new LinkedList();
low = new LinkedList();
} } private static class ScheduledTask extends TimerTask
{ public void run()
{
TaskEngine.addTask(priority, task);
} private int priority;
private Runnable task; ScheduledTask(int priority, Runnable task)
{
this.priority = priority;
this.task = task;
}
} private static class TaskEngineWorker extends Thread
{ public void stopWorker()
{
done = true;
} public void run()
{
do
{
if(done)
break;
int currentThreadPriority = getPriority();
int newThreadPriority = currentThreadPriority;
try
{
TaskWrapper wrapper = TaskEngine.nextTask();
int desiredTaskPriority = wrapper.getPriority();
newThreadPriority = desiredTaskPriority != 2 ? ((int) (desiredTaskPriority != 1 ? 2 : 5)) : 9;
if(newThreadPriority != currentThreadPriority)
try
{
logger.debug("Running task engine worker (" + wrapper.getTask().getClass() + ") at thread priority " + newThreadPriority);
setPriority(newThreadPriority);
}
catch(Exception e)
{
logger.error(e);
}
logger.debug("Executing task (" + wrapper.getTask().getClass() + ")");
wrapper.getTask().run();
logger.debug("Completed execution task (" + wrapper.getTask().getClass() + ")");
if(newThreadPriority != currentThreadPriority)
try
{
logger.debug("Restoring task engine worker thread to thread priority - " + currentThreadPriority);
setPriority(currentThreadPriority);
}
catch(Exception e)
{
logger.error(e);
}
}
catch(Exception e)
{
logger.error(e);
if(newThreadPriority != currentThreadPriority)
try
{
logger.debug("Restoring task engine worker thread to thread priority - " + currentThreadPriority);
setPriority(currentThreadPriority);
}
catch(Exception e2)
{
logger.error(e2);
}
}
} while(true);
} private boolean done; TaskEngineWorker(String name)
{
super(TaskEngine.threadGroup, name);
done = false;
}
} private TaskEngine()
{
} public static void start()
{
synchronized(lock)
{
started = true;
lock.notifyAll();
}
} private static void initialize()
{
taskTimer = new Timer(true);
taskQueue = new PriorityQueue();
threadGroup = new ThreadGroup("Task Engine Workers");
workers = new TaskEngineWorker[5];
for(int i = 0; i < workers.length; i++)
{
workers[i] = new TaskEngineWorker("Task Engine Worker " + i);
workers[i].setDaemon(true);
workers[i].start();
} } public static int size()
{
synchronized(lock){
return taskQueue.size();
} } public static int getNumWorkers()
{
return workers.length;
} public static void addTask(Runnable task)
{
addTask(1, task);
} public static void addTask(int priority, Runnable task)
{
synchronized(lock)
{
if((double)taskQueue.size() > Math.ceil(workers.length / 2))
{
busyTimestamp = System.currentTimeMillis();
addWorker();
} else
if(workers.length > 3)
removeWorker();
TaskWrapper wrapper = new TaskWrapper(priority, task);
taskQueue.enqueue(priority, wrapper);
lock.notify();
}
} public static TimerTask scheduleTask(Runnable task, Date date)
{
return scheduleTask(1, task, date);
} public static TimerTask scheduleTask(int priority, Runnable task, Date date)
{
TimerTask timerTask = new ScheduledTask(priority, task);
taskTimer.schedule(timerTask, date);
return timerTask;
} //在1delay秒后执行此任务,每次间隔2秒period
public static TimerTask scheduleTask(Runnable task, long delay, long period)
{
return scheduleTask(1, task, delay, period);
} public static TimerTask scheduleTask(int priority, Runnable task, long delay, long period)
{
TimerTask timerTask = new ScheduledTask(priority, task);
taskTimer.scheduleAtFixedRate(timerTask, delay, period);
return timerTask;
} public static void shutdown()
{
taskTimer.cancel();
} public static void restart()
{
taskTimer.cancel();
initialize();
} private static TaskWrapper nextTask()
{
synchronized(lock){
while(taskQueue.isEmpty() || !started)
try
{
lock.wait();
}
catch(InterruptedException ie) { }
return (TaskWrapper)taskQueue.dequeue();
} } private static void addWorker()
{
if(workers.length < 30 && System.currentTimeMillis() > newWorkerTimestamp + 2000L)
{
int newSize = workers.length + 1;
int lastIndex = newSize - 1;
TaskEngineWorker newWorkers[] = new TaskEngineWorker[newSize];
System.arraycopy(workers, 0, newWorkers, 0, workers.length);
newWorkers[lastIndex] = new TaskEngineWorker("Task Engine Worker " + lastIndex);
newWorkers[lastIndex].setDaemon(true);
newWorkers[lastIndex].start();
workers = newWorkers;
newWorkerTimestamp = System.currentTimeMillis();
}
} private static void removeWorker()
{
if(workers.length > 3 && System.currentTimeMillis() > busyTimestamp + 5000L)
{
workers[workers.length - 1].stopWorker();
int newSize = workers.length - 1;
TaskEngineWorker newWorkers[] = new TaskEngineWorker[newSize];
System.arraycopy(workers, 0, newWorkers, 0, newSize);
workers = newWorkers;
busyTimestamp = System.currentTimeMillis();
}
} public static final int HIGH_PRIORITY = 2;
public static final int MEDIUM_PRIORITY = 1;
public static final int LOW_PRIORITY = 0;
private static PriorityQueue taskQueue = null;
private static ThreadGroup threadGroup;
private static TaskEngineWorker workers[] = null;
private static Timer taskTimer = null;
private static Object lock = new Object();
private static long newWorkerTimestamp = System.currentTimeMillis();
private static long busyTimestamp = System.currentTimeMillis();
private static boolean started = false; static
{
initialize();
} }
2. TaskWrapper 任务包装类
package com.yanek.task; public class TaskWrapper
{ public TaskWrapper(int priority, Runnable task)
{
this.priority = priority;
this.task = task;
} public Runnable getTask()
{
return task;
} public void setTask(Runnable task)
{
this.task = task;
} public int getPriority()
{
return priority;
} public void setPriority(int priority)
{
this.priority = priority;
} private Runnable task;
private int priority;
}
3. 测试任务类:
package com.yanek.task; import org.apache.log4j.Logger; //import com.aspboy.jxc.tcp.SocketClient; public class TestTask implements Runnable
{ static Logger Log = Logger.getLogger(TestTask.class.getName());
public void run()
{ System.out.println("time==="+System.currentTimeMillis());
} }
4. 监听器类: 启动时加载任务和启动任务
package com.yanek.task; import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; public class TaskListener implements ServletContextListener { public static final long SECOND = 1000L; private ServletContext context; public TaskListener() {
System.out.println("LifeCycleListener new ... ");
} public void contextInitialized(ServletContextEvent event) { System.out.println("ServletContext Initialized... ");
context = event.getServletContext(); String prefix = event.getServletContext().getRealPath("/");
System.out.println("root path===" + prefix); TestTask testtask = new TestTask();
TaskEngine.scheduleTask(testtask, SECOND * 1, SECOND * 2);
TaskEngine.start(); } public void contextDestroyed(ServletContextEvent event) {
System.out.println("ServletContext Destroyed... ");
TaskEngine.shutdown();
} }
最后在web.xml配置监听器类:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <listener>
<listener-class>com.yanek.task.TaskListener</listener-class>
</listener> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
启动web容器,即可开始执行任务。
java中基于TaskEngine类封装实现定时任务的更多相关文章
- Java中使用自定义类封装数组,添加类方法实现数据操作
1.具体见注释 2.后续或有更新 public class MyArray { private long[] array; private int cnt; // 自定义数组类的元素个数 /** 使用 ...
- Java中的Unsafe类111
1.Unsafe类介绍 Unsafe类是在sun.misc包下,不属于Java标准.但是很多Java的基础类库,包括一些被广泛使用的高性能开发库都是基于Unsafe类开发的,比如Netty.Hadoo ...
- Java中遍历实体类(处理MongoDB)
在实际过程中,经常要将实体类进行封装,尤其是处理数据库的过程中:因此,对于遍历实体类能够与数据库中的一行数据对应起来. 我是使用的环境是Spring boot,访问的数据库时MongoDB 实体类遍历 ...
- 【Java并发】Java中的原子操作类
综述 JDK从1.5开始提供了java.util.concurrent.atomic包. 通过包中的原子操作类能够线程安全地更新一个变量. 包含4种类型的原子更新方式:基本类型.数组.引用.对象中字段 ...
- 【Java】Java中的Collections类——Java中升级版的数据结构【转】
一般来说课本上的数据结构包括数组.单链表.堆栈.树.图.我这里所指的数据结构,是一个怎么表示一个对象的问题,有时候,单单一个变量声明不堪大用,比如int,String,double甚至一维数组.二维数 ...
- java中普通的顶级类是不能使用static关键字修饰的。只有内部类可以使用static修饰,也可以不使用staitc关键字修饰。
java中普通的顶级类是不能使用static关键字修饰的.只有内部类可以使用static修饰,也可以不使用staitc关键字修饰. java中的类可以是static吗?答案是可以.在java中我们可以 ...
- Java中的继承、封装、多态的理解
Java中的继承.封装.多态 继承的理解: 1.继承是面向对象的三大特征之一,也是实现代码复用的重要手段.Java的继承具有单继承的特点,每个子类只有一个直接父类. 2.Java的继承通过extend ...
- Java中的实体类--Serializable接口、transient 关键字
在java中,实体类是一个非常重要的概念,我们可以在实体类中封装对象.设置其属性和方法等.关于实体类,也经常涉及到适配器模式.装饰者模式等设计模式.那么在实际代码开发中,关于实体类的注意事项有哪些呢? ...
- Java中的Collections类
转载:https://blog.csdn.net/yangxingpa/article/details/80515963 从[Java]Java中的Collections类——Java中升级版的数据结 ...
随机推荐
- 用Global.asax实现伪静态.
在Global.asax文件里添加Application_BeginRequest事件处理.添加如下代码: 1 protected void Application_BeginRequest(Obje ...
- 安装SVN及实现nginx web同步更新需要在WDCP一键安装包的基础上
一.安装 1.查看是否安装cvs rpm -qa | grep subversion 2.安装 yum install subversion 3.测试是否安装成功 /usr/bin/svnserve ...
- 用webclient.DownloadFile下载exe文件时大小为0
用自己写的下载软件从服务器端下载文件,别的文件能下,但exe文件显示下载文件大小为0,连接超时,原因是服务上发布的下载文件夹的虚拟目录的属性有问题, 包含.exe 文件的虚拟目录已启用执行应用程序权限 ...
- dede 留言板访问的目录
D:\APMServ5.2.6\www\htdocs\xyhy\templets\plus guestbook.rar 文件 里面 DEDE留言簿的插件:
- day05
1.递归 利用递归实现阶乘(1*2*3*4*5*6*7) def func(num): if num == 1: return 1 return num * func(num-1) x = func( ...
- Python自动化运维之20、HTML
一.HTML相关概念 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这 ...
- Dapper快速学习
Dapper快速学习 我们都知道ORM全称叫做Object Relationship Mapper,也就是可以用object来map我们的db,而且市面上的orm框架有很多,其中有一个框架 叫做dap ...
- 前端面试题第二波,要offer的看过来~
快来测试测试自己掌握能力吧! 1. class.forname的作用?为什么要用? 1).获取Class对象的方式:类名.class.对象.getClass().Class.forName(" ...
- [Android] 输入系统(二)
在上一篇文章的最后,我们发现InputDispatcher是调用了InputChannel->sendMessage把键值发送出去,那么相应的,也有接收键值的地方.接收函数是InputChann ...
- HID燈是什么及其工作原理
HID為HighIntensityDischarge的縮寫,即高壓氣體放電燈,它發光的原理是將12V電壓增壓至23000V的超高電壓,激穿填充在石英管的氙氣,使它發光.然后再將電壓轉成85V左右,穩定 ...