繼續(xù)上一篇隨筆:基于XMPP的GTalk機(jī)器人 關(guān)鍵詞:XMPP smack GTalk
          這篇隨筆將使用消息隊(duì)列機(jī)制,讓GTalk機(jī)器人一直在線
          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();

              
          // 線程池維護(hù)線程的最少數(shù)量
              private final static int CORE_POOL_SIZE = 2;

              
          // 線程池維護(hù)線程的最大數(shù)量
              private final static int MAX_POOL_SIZE = 4;

              
          // 線程池維護(hù)線程所允許的空閑時間
              private final static int KEEP_ALIVE_TIME = 0;

              
          // 線程池所使用的緩沖隊(duì)列大小
              private final static int WORK_QUEUE_SIZE = 4;

              
          // 消息緩沖隊(duì)列
              Queue msgQueue = new LinkedList();

              
          // 訪問消息緩存的調(diào)度線程
              final Runnable accessBufferThread = new Runnable() {
                  
          public void run() {
                      
          // 查看是否有待定請求,如果有,則創(chuàng)建一個新的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() 
          + "消息放入隊(duì)列中重新等待執(zhí)行");
                      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);

              
          // 調(diào)度線程池
              final ScheduledExecutorService scheduler = Executors
                      .newScheduledThreadPool(
          1);

              
          final ScheduledFuture taskHandler = scheduler.scheduleAtFixedRate(
                      accessBufferThread, 
          01, 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 閱讀(627) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 庐江县| 会昌县| 波密县| 邯郸市| 西乡县| 皮山县| 清河县| 丰镇市| 文水县| 循化| 广东省| 申扎县| 南澳县| 白山市| 岚皋县| 翁源县| 彩票| 偃师市| 高台县| 嘉定区| 车险| 安平县| 灵宝市| 德钦县| 西安市| 顺义区| 微博| 正蓝旗| 东丰县| 新沂市| 砀山县| 盐山县| 鄂托克旗| 金湖县| 金平| 庆城县| 花垣县| 石城县| 通州区| 洪雅县| 哈尔滨市|