繼續上一篇隨筆:基于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, 
          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 閱讀(630) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 蚌埠市| 永昌县| 饶河县| 行唐县| 乌鲁木齐市| 孙吴县| 石河子市| 铅山县| 运城市| 天台县| 东兴市| 久治县| 西青区| 黑河市| 长丰县| 上虞市| 广水市| 滨州市| 富宁县| 元江| 高邑县| 德钦县| 东兴市| 宁阳县| 阿荣旗| 马关县| 南城县| 交城县| 万源市| 台山市| 赤峰市| 中山市| 乌兰察布市| 蓝田县| 怀柔区| 祁阳县| 威宁| 新乐市| 扬州市| 阳新县| 临潭县|