前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微博订阅评论

微博订阅评论

作者头像
week
发布2018-08-24 15:08:26
7010
发布2018-08-24 15:08:26
举报
文章被收录于专栏:用户画像

参考:http://open.weibo.com/wiki/%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81

微博开放平台给出的代码有很多bug,在此纠正

如乱码问题的解决: String message = new String(bytes,"UTF-8");

代码语言:javascript
复制
package com.thinkgem.jeesite.modules.util.weibo.request;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.thinkgem.jeesite.modules.util.weibo.post.WeiBoPostService;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ReceiveMessageService implements ServletContextListener {
    private MultiThreadedHttpConnectionManager httpConnManager;
    private HttpClient httpClient;
    private List<String> streamingUrlList=new ArrayList<String>();
    private int curStreamUrlIndex;
    public static long lastMsgLocation = -1L;
    private DataInputStream inputStream;
    private final int recBufSize = 256;
    private byte[] recBuf;
    private int recIndex;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        new ReceiveMessageService().init();
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }

    /**
     * 初始化httpclient,并启动获取数据线程
     */
    public void init() {
        this.streamingUrlList.add("http://c.api.weibo.com/datapush/comment?subid=10823");
        //this.streamingUrlList.add("http://c.api.weibo.com/datapush/status?subid=10718");
        this.httpConnManager = new MultiThreadedHttpConnectionManager();
        this.httpConnManager.getParams().setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, 10);
        this.httpConnManager.getParams().setMaxTotalConnections(10);
        this.httpConnManager.getParams().setSoTimeout(2147483647);
        this.httpConnManager.getParams().setConnectionTimeout(10000);
        this.httpConnManager.getParams().setReceiveBufferSize(655350);
        this.httpClient = new HttpClient(this.httpConnManager);
        new ReadTask().start();
    }



    /**
     * 获取数据线程
     *
     */
    class ReadTask extends Thread {

        /**
         * 启一个线程从服务器读取数据
         */
        public void run() {
            while (true) {
                GetMethod method = null;
                recIndex = 0;
                recBuf = new byte[recBufSize];
                try {
                    method = connectStreamServer();//建立连接
                    while (true) {
                        processLine();//读取并处理数据
                    }
                } catch (Exception e) {
                    // 当连接断开时,重新连接
                    lastMsgLocation = lastMsgLocation + 2;
                    curStreamUrlIndex = ++curStreamUrlIndex % streamingUrlList.size();
                } finally {
                    if (method != null)
                        method.releaseConnection();
                }
            }
        }

        /**
         * 建立http连接
         *
         * @return
         */
        private GetMethod connectStreamServer() throws IOException {
            String targetURL = (String) streamingUrlList.get(curStreamUrlIndex);
            // 从指定的since_id开始读取数据,保证读取数据的连续性,消息完整性
            if (lastMsgLocation > 0L) {
                targetURL = targetURL + "&since_id=" + lastMsgLocation;
            }
            //System.out.println("StreamingReceiver http get url=" + targetURL);
            GetMethod method = new GetMethod(targetURL);
            int statusCode = 0;
            statusCode = httpClient.executeMethod(method);

            if (statusCode != HttpStatus.SC_OK) {
                throw new RuntimeException(".. ");
            }

            try {
                inputStream = new DataInputStream(method.getResponseBodyAsStream());
            } catch (IOException e) {
                throw new RuntimeException("get stream input io exception", e);
            }

            return method;
        }

        /**
         * 读取并处理数据
         *
         * @throws IOException
         */
        private void processLine() throws IOException {
            byte[] bytes = readLineBytes();//读取数据
            if ((bytes != null) && (bytes.length > 0)) {
                String message = new String(bytes,"UTF-8");//编码格式设置
                handleMessage(message);
            }
        }

        /**
         * 可以重写此方法解析message,同时需将lastMsgLocation赋值为最近1条消息的id
         *
         * @param message
         */
        private void handleMessage(String message) throws UnsupportedEncodingException {
            JSONObject rs=JSON.parseObject(message);
            if(StringUtils.isBlank(rs.getString("errorCode"))){
                lastMsgLocation= rs.getLong("id");
                WeiBoPostService service=new WeiBoPostService();
                service.receiveMessage(message);
            }
        }

        /**
         * 读取数据
         *
         * @return
         * @throws IOException
         */
        public byte[] readLineBytes() throws IOException {
            byte[] result = null;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int readCount = 0;
            if ((recIndex > 0) && (read(bos))) {
                return bos.toByteArray();
            }
            while ((readCount = inputStream.read(recBuf, recIndex, recBuf.length - recIndex)) > 0) {
                recIndex = (recIndex + readCount);
                if (read(bos)) {
                    break;
                }
            }
            result = bos.toByteArray();
            if ((result == null) || ((result != null) && (result.length <= 0) && (recIndex <= 0))) {
                throw new IOException("++++ Stream appears to be dead, so closing it down");
            }
            return result;
        }

        /**
         * 读数据到bos
         *
         * @param bos
         * @return
         */
        private boolean read(ByteArrayOutputStream bos) {
            boolean result = false;
            int index = -1;
            for (int i = 0; i < recIndex - 1; i++) {
                // 13cr-回车 10lf-换行
                if ((recBuf[i] == 13) && (recBuf[(i + 1)] == 10)) {
                    index = i;
                    break;
                }
            }
            if (index >= 0) {
                bos.write(recBuf, 0, index);
                byte[] newBuf = new byte[recBufSize];
                if (recIndex > index + 2) {
                    System.arraycopy(recBuf, index + 2, newBuf, 0, recIndex - index - 2);
                }
                recBuf = newBuf;
                recIndex = (recIndex - index - 2);
                result = true;
            } else if (recBuf[(recIndex - 1)] == 13) {
                bos.write(recBuf, 0, recIndex - 1);
                Arrays.fill(recBuf, (byte) 0);
                recBuf[0] = 13;
                recIndex = 1;
            } else {
                bos.write(recBuf, 0, recIndex);
                Arrays.fill(recBuf, (byte) 0);
                recIndex = 0;
            }
            return result;
        }
    }

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年03月06日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档