继续上一篇随笔:
基于XMPP的GTalk机器人 关键词:XMPP smack GTalk
这篇随笔将使用消息队列机制,让GTalk机器人一直在线
ThreadPoolManager.java:
package com.yinger.util.gtalkRobot;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadPoolManager {
private static ThreadPoolManager tpm = new ThreadPoolManager();
// 线程池维护线程的最少数量
private final static int CORE_POOL_SIZE = 2;
// 线程池维护线程的最大数量
private final static int MAX_POOL_SIZE = 4;
// 线程池维护线程所允许的空闲时间
private final static int KEEP_ALIVE_TIME = 0;
// 线程池所使用的缓冲队列大小
private final static int WORK_QUEUE_SIZE = 4;
// 消息缓冲队列
Queue msgQueue = new LinkedList();
// 访问消息缓存的调度线程
final Runnable accessBufferThread = new Runnable() {
public void run() {
// 查看是否有待定请求,如果有,则创建一个新的AccessThread,并添加到线程池中
if (hasMoreAcquire()) {
String msg = (String) msgQueue.poll();
Runnable task = new AccessThread(msg);
threadPool.execute(task);
}
}
};
final RejectedExecutionHandler handler = new RejectedExecutionHandler() {
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
System.out.println(((AccessThread) r).getMsg() + "消息放入队列中重新等待执行");
msgQueue.offer(((AccessThread) r).getMsg());
}
};
// 管理线程池
final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS,
new ArrayBlockingQueue(WORK_QUEUE_SIZE), this.handler);
// 调度线程池
final ScheduledExecutorService scheduler = Executors
.newScheduledThreadPool(1);
final ScheduledFuture taskHandler = scheduler.scheduleAtFixedRate(
accessBufferThread, 0, 1, TimeUnit.SECONDS);
public static ThreadPoolManager newInstance() {
return tpm;
}
private ThreadPoolManager() {
}
private boolean hasMoreAcquire() {
return !msgQueue.isEmpty();
}
public void login(String msg) {
Runnable task = new AccessThread(msg);
threadPool.execute(task);
}
}
AccessThread.java
package com.yinger.util.gtalkRobot;
public class AccessThread implements Runnable {
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public AccessThread() {
super();
}
public AccessThread(String msg) {
this.msg = msg;
}
public void run() {
if(GTalk.gTalk == null)
{
GTalk gTalk = new GTalk();
}
}
}
修改
GTalk.java
package com.yinger.util.gtalkRobot;
/**
* GTalk manager
*
* @author Ying-er
* @mail melody.crazycoding@gmail.com
* @time 2011/07/18 8:45:54
* @version 1.00
*/
public final class GTalk {
public static SmackToGtalk gTalk = SmackToGtalk
.getInstance(GTalkRobotConst.MODE);
public static ThreadPoolManager tpm = ThreadPoolManager.newInstance();
static {
tpm.login(null);
}
public static void sent(String str) {
try {
gTalk.sendMessage(str);
} catch (Exception e) {
}
}
}
哦了 =。=
posted on 2011-07-19 09:43
Ying-er 阅读(621)
评论(0) 编辑 收藏