异步推送新闻,可扩展Three****Push

原创
2018/12/05 18:14
阅读数 16

定时任务

    // 每30分钟
    @Scheduled(cron = "0 0/30 * * * ?")
    public void pushNewsInfo() {
        log.info("开始执行自动推送:begin");
        List<NewsPushDO> list = pushNewsService.selectReadyPush(NewsStatusEnum.INITIALIZE.getKey());
        if(list!=null&& list.size()>0){
            int size = list.size();
            try {
                log.info("自动推送数量:{}",size);
                for (int i = 0; i < size; i++) {
                    NewsPushDO push=list.get(i);
                    NewsInfoDO info = newsInfoService.selectById(push.getNewsId());
                    pushNewsService.pushNewInfo(info);
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("===自动推送新闻资讯失败===" + e.getMessage());
            }
        }
        log.info("结束执行自动推送:end" );

    }

service

   public void pushNewInfo(NewsInfoDO info){
        BaseNewsPush three = new ThreeOclockPush(info);
        three.setNewsPushMapper(newsPushMapper);
        three.setAsyncNewsInfoCall(asyncNewsInfoCall);
        three.update();
    }

Base

@Component
@Data
@Transactional
public abstract class BaseNewsPush {
    protected String url;
    protected String entity;
    protected NewsInfoDO info;
    protected NewsPushMapper newsPushMapper;
    protected AsyncNewsInfoCall asyncNewsInfoCall;

    protected final CountDownLatch countDownLatch = new CountDownLatch(1);

    public abstract void callback(String result);

    public abstract Object convertJSONArray(NewsInfoDO info);

    public void update() {
        asyncNewsInfoCall.update(this);
    }


}

执行httpclient

@Component
@Slf4j
public class AsyncNewsInfoCall {

    public void update(BaseNewsPush push) {
        new Thread(() -> {
            log.info("开始请求begin");
            String result = HttpClientUtil.httpPost(push.getUrl(), push.getEntity());
            push.callback(result);
            log.info("开始请求end");
        }).start();
    }
}

新闻推送 回调成功失败

@Component
@Slf4j
public class ThreeOclockPush extends BaseNewsPush {
    @Override
    public void callback(String result) {
        try {
            log.info("----Result push newsInfo begin ----:{}",result);
            JSONObject rej = JSONObject.parseObject(result);
            if (rej != null && "success".equals(rej.get("retmessage")) && 0 == Integer.valueOf(rej.get("retcode").toString())) {
                newsPushMapper.updateSendStatus((long) info.getId(), NewsStatusEnum.SEND_SUCCESS.getKey(), NewsStatusEnum.SEND_SUCCESS.getValue());
            } else {
                newsPushMapper.updateSendStatus((long) info.getId(), NewsStatusEnum.SEND_FAIL.getKey(), NewsStatusEnum.SEND_FAIL.getValue());
                if (result != null) {
                    log.error(result);
                }
            }
        } catch (SqlSessionException e) {
            e.printStackTrace();
            log.error("====SqlSessionException:{}", e.getMessage().getBytes());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("====Exception:{}", e.getMessage());
        } finally {
            countDownLatch.countDown();
        }

        log.info("----Result push newsInfo end ----:{}");
    }

    public ThreeOclockPush() {
    }

    public ThreeOclockPush(NewsInfoDO info) {
        this.info = info;
    }

    @Override
    public String getEntity() {
        Long times = System.currentTimeMillis();
        StringBuilder str = new StringBuilder();
        str.append(BopsConstant.DEV_APPID).append(times).append("0").append(BopsConstant.DEV_KEY);
        String sign = MD5Util.getMD5String(str.toString());
        JSONObject json = new JSONObject();
        json.put("items", convertJSONArray(info));
        json.put("appid", BopsConstant.DEV_APPID);
        json.put("cts", times);
        json.put("newstype ", "0");
        json.put("sign", sign);
        return json.toJSONString();
    }

    @Override
    public Object convertJSONArray(NewsInfoDO info) {
        long pubDate = System.currentTimeMillis() / 1000;
        if (!StringUtils.isEmpty(info.getCreateDate())) {
            pubDate = info.getCreateDate().getTime() / 1000;
        }
        JSONArray array = new JSONArray();
        JSONObject json0 = new JSONObject();
        json0.put("sourceid", String.valueOf(info.getId()));
        json0.put("title", String.valueOf(info.getNewsTitle()));
        json0.put("summary", info.getIntroduction());
        json0.put("content", info.getArticleContent());
        json0.put("pubDate", pubDate);
        json0.put("author", info.getAuthor());
        json0.put("img", info.getCoverImg());
        json0.put("link", info.getArticleUrl());
//        if(info.getCategory()==0){
//            json0.put("category", "");
//        }else{
//            json0.put("category", "");
//        }
        array.add(0, json0);
        return array;
    }

    @Override
    public String getUrl() {
        return BopsConstant.DEV_URL;
    }

    @Override
    public void update() {
        this.getAsyncNewsInfoCall().update(this);
    }

}

需要appId,密钥,url

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部