<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

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


    網站導航:
     
    主站蜘蛛池模板: 亚洲精品私拍国产福利在线| 亚洲av女电影网| 67194成是人免费无码| 夜夜爽免费888视频| 亚洲一级特黄特黄的大片| 午夜理伦剧场免费| 国产亚洲一区区二区在线| 亚洲一区动漫卡通在线播放| 亚洲狠狠色丁香婷婷综合| 亚洲AV无码资源在线观看| 视频一区二区三区免费观看| 国产亚洲女在线线精品| 久久久久国产免费| 亚洲熟妇自偷自拍另欧美| 亚洲成a人片在线观看久| 久久久精品免费国产四虎| 亚洲婷婷综合色高清在线| 深夜国产福利99亚洲视频| 18禁黄网站禁片免费观看不卡| 无码色偷偷亚洲国内自拍| 亚洲AV无码国产在丝袜线观看| 女人与禽交视频免费看| 日本不卡免费新一区二区三区| 亚洲国产AV一区二区三区四区| 亚洲第一AAAAA片| 国产在线观看www鲁啊鲁免费| 精品国产污污免费网站入口在线| 亚洲一级毛片在线观| 亚洲A∨精品一区二区三区| 国产精品成人免费福利| 免费夜色污私人影院网站| 亚洲色图.com| 伊人久久精品亚洲午夜| 青春禁区视频在线观看直播免费 | 大地资源在线观看免费高清| 毛片基地看看成人免费| 亚洲国产日韩精品| 久久亚洲综合色一区二区三区 | 亚洲高清中文字幕| 欧美日韩亚洲精品| 最近2019中文字幕免费大全5|