主要包括如下几个类:

文章标题: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类封装实现定时任务的更多相关文章

  1. Java中使用自定义类封装数组,添加类方法实现数据操作

    1.具体见注释 2.后续或有更新 public class MyArray { private long[] array; private int cnt; // 自定义数组类的元素个数 /** 使用 ...

  2. Java中的Unsafe类111

    1.Unsafe类介绍 Unsafe类是在sun.misc包下,不属于Java标准.但是很多Java的基础类库,包括一些被广泛使用的高性能开发库都是基于Unsafe类开发的,比如Netty.Hadoo ...

  3. Java中遍历实体类(处理MongoDB)

    在实际过程中,经常要将实体类进行封装,尤其是处理数据库的过程中:因此,对于遍历实体类能够与数据库中的一行数据对应起来. 我是使用的环境是Spring boot,访问的数据库时MongoDB 实体类遍历 ...

  4. 【Java并发】Java中的原子操作类

    综述 JDK从1.5开始提供了java.util.concurrent.atomic包. 通过包中的原子操作类能够线程安全地更新一个变量. 包含4种类型的原子更新方式:基本类型.数组.引用.对象中字段 ...

  5. 【Java】Java中的Collections类——Java中升级版的数据结构【转】

    一般来说课本上的数据结构包括数组.单链表.堆栈.树.图.我这里所指的数据结构,是一个怎么表示一个对象的问题,有时候,单单一个变量声明不堪大用,比如int,String,double甚至一维数组.二维数 ...

  6. java中普通的顶级类是不能使用static关键字修饰的。只有内部类可以使用static修饰,也可以不使用staitc关键字修饰。

    java中普通的顶级类是不能使用static关键字修饰的.只有内部类可以使用static修饰,也可以不使用staitc关键字修饰. java中的类可以是static吗?答案是可以.在java中我们可以 ...

  7. Java中的继承、封装、多态的理解

    Java中的继承.封装.多态 继承的理解: 1.继承是面向对象的三大特征之一,也是实现代码复用的重要手段.Java的继承具有单继承的特点,每个子类只有一个直接父类. 2.Java的继承通过extend ...

  8. Java中的实体类--Serializable接口、transient 关键字

    在java中,实体类是一个非常重要的概念,我们可以在实体类中封装对象.设置其属性和方法等.关于实体类,也经常涉及到适配器模式.装饰者模式等设计模式.那么在实际代码开发中,关于实体类的注意事项有哪些呢? ...

  9. Java中的Collections类

    转载:https://blog.csdn.net/yangxingpa/article/details/80515963 从[Java]Java中的Collections类——Java中升级版的数据结 ...

随机推荐

  1. iOS-点击视图,视图背景颜色随机更改

    一.效果图 二.代码 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the v ...

  2. [转载]C++中声明与定义的区别

    C++学了这么多年你知道为什么定义类时,类的定义放在.h文件中,而类的实现放在cpp文件中.它们为什么能够关联到一起呢?你知道什么东西可以放在.h文件中,什么不能.什么东西又可以放在cpp文件中.如果 ...

  3. CentOS6.5 下 Mysql5.7主从复制

    包下载http://url.cn/WrNg5S 主数据库:192.168.161.129 从数据库1:192.168.161.130 从数据库1:192.168.161.138 MySQL安装地址:/ ...

  4. 04_RHEL7.1忘记root密码

    在开机进入启动项时,选择需要重设密码的那个启动项 按e进入编辑模式,找到rhgb和quiet参数(几乎在最下面),替换为 init=/bin/sh 按ctrl+X不需密码进入shell 以rw的方式重 ...

  5. jQuery获取select、checkbox、radio的方法总结

    在进行网页的前后台交互的时候,经常需要获取单选框(radio).复选框(checkbox)以及下拉列表(select/dropdownlist)选中的值. 总结了一下,方便以后复习查看: 1.获取选中 ...

  6. fedora20 播放aiv视频

    环境:fedora20 64位 下载个教程是avi的格式,用FEDORA自带的视频播放提示少插件,搜索又没有.  到网上搜索后,参考下面的方式添加软件仓库后,再打开视频播放顺利装好插件.但视频画件一闪 ...

  7. python运维开发之第七天

    一.面向对象编程进阶 1.静态方法 @staticmethod 名义上归类管理,实际上跟类没什么关系 在静态方法里,访问不了类或实例中的任何属性 class Static_method(object) ...

  8. WEB兼容性之JS

    1. 获取iframe的window对象 //三种方法 window.iframeName window.frames[iframeName] document.getElementById(ifra ...

  9. sql server 调优----索引缺失

    SELECT mig.index_group_handle, mid.index_handle, CONVERT (decimal (28,1), migs.avg_total_user_cost * ...

  10. NSIndexSet-入门浅析

    NSIndexSet-入门浅析   记得上一次,用到,关于删除UITableView分组的方法 [tableView deleteSections:[NSIndexSet indexSetWithIn ...