Commit f87165c0 by [zhangzhiwei]

推送添加log及重试3次

parent 7fe19a3c
...@@ -87,7 +87,7 @@ public class WeiboHotSearchDAO extends MongoDBTemplate{ ...@@ -87,7 +87,7 @@ public class WeiboHotSearchDAO extends MongoDBTemplate{
Date date = new Date((new Date().getTime()-60*60*1000)); Date date = new Date((new Date().getTime()-60*60*1000));
DBObject query = new BasicDBObject(); DBObject query = new BasicDBObject();
query.put("time", new BasicDBObject("$gte", date)); query.put("time", new BasicDBObject("$gte", date));
query.put("changeCount", 0); query.put("changeCount", 0);
try { try {
DBCursor cur = this.getReadColl().find(query); DBCursor cur = this.getReadColl().find(query);
......
...@@ -25,9 +25,9 @@ public class HotSearchRun { ...@@ -25,9 +25,9 @@ public class HotSearchRun {
} }
public static void main(String[] args) { public static void main(String[] args) {
new HotSearchRun().showTimer(); // new HotSearchRun().showTimer();
new CacheListener().startListen(); // new CacheListener().startListen();
// new SendWeiboHotSearchRun().start(); new SendWeiboHotSearchRun().start();
// new SendZhihuHotSearchRun().start(); // new SendZhihuHotSearchRun().start();
} }
} }
...@@ -16,49 +16,52 @@ import com.zhiwei.searchhotcrawler.util.WechatConstant; ...@@ -16,49 +16,52 @@ import com.zhiwei.searchhotcrawler.util.WechatConstant;
import com.zhiwei.zhiweiTools.timeParse.TimeParse; import com.zhiwei.zhiweiTools.timeParse.TimeParse;
import com.zhiwei.zhiweiTools.tools.ZhiWeiTools; import com.zhiwei.zhiweiTools.tools.ZhiWeiTools;
public class SendWeiboHotSearchRun extends Thread{ public class SendWeiboHotSearchRun extends Thread {
private WeiboHotSearchDAO weiboHotSearchDAO = new WeiboHotSearchDAO(); private WeiboHotSearchDAO weiboHotSearchDAO = new WeiboHotSearchDAO();
private static Logger logger = LoggerFactory.getLogger(SendWeiboHotSearchRun.class); private static Logger logger = LoggerFactory.getLogger(SendWeiboHotSearchRun.class);
@Override @Override
public void run() { public void run() {
while(true) { while (true) {
try { try {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY); int hour = calendar.get(Calendar.HOUR_OF_DAY);
logger.info("微博推送,当前系统时间为:" + hour); logger.info("微博推送,当前系统时间为:" + hour);
if(hour > 6 && hour < 23){ if (hour > 6 && hour < 23) {
List<DBObject> list = weiboHotSearchDAO.getWeiboHotOneHour(); List<DBObject> list = weiboHotSearchDAO.getWeiboHotOneHour();
if(list!=null && list.size()>0){ if (list != null && list.size() > 0) {
for(DBObject weibo : list){ for (DBObject weibo : list) {
String title = weibo.get("name").toString(); String title = weibo.get("name").toString();
String time = TimeParse.dateFormartString((Date)weibo.get("time"), "yyyy-MM-dd HH:mm:ss"); String time = TimeParse.dateFormartString((Date) weibo.get("time"), "yyyy-MM-dd HH:mm:ss");
String url = weibo.get("url").toString(); String url = weibo.get("url").toString();
sendTemplateByUserIds(title, time, url); sendTemplateByUserIds(title, time, url);
} }
}else{ } else {
sendTemplateByUserIds("最近一小时无数据", TimeParse.dateFormartString(new Date(), "yyyy-MM-dd HH:mm:ss"), null); logger.info("微博最近一小时无数据");
sendTemplateByUserIds("最近一小时无数据",
TimeParse.dateFormartString(new Date(), "yyyy-MM-dd HH:mm:ss"), null);
} }
} }
ZhiWeiTools.sleep(1*60*60*1000); ZhiWeiTools.sleep(1 * 60 * 60 * 1000);
} catch (Exception e) { } catch (Exception e) {
logger.debug("微博热搜推送出现问题,问题为:::{}",e.fillInStackTrace()); logger.debug("微博热搜推送出现问题,问题为:::{}", e.fillInStackTrace());
ZhiWeiTools.sleep(1*60*60*1000); ZhiWeiTools.sleep(1 * 60 * 60 * 1000);
continue; continue;
} }
} }
} }
/** /**
* @Title: sendTemplateByUserIds * @Title: sendTemplateByUserIds
* @author hero * @author hero
* @Description: 发送模版消息 * @Description: 发送模版消息
* @param @param microTouTiao * @param @param
* @param @param userList 设定文件 * microTouTiao
* @param @param
* userList 设定文件
* @return void 返回类型 * @return void 返回类型
*/ */
public static void sendTemplateByUserIds(String title,String time, String url) { public static void sendTemplateByUserIds(String title, String time, String url) {
Map<String, Object> dataMap = new HashMap<>(); Map<String, Object> dataMap = new HashMap<>();
JSONObject first = new JSONObject(); JSONObject first = new JSONObject();
first.put("value", "您好,有一条来自微博热搜榜的预警通知。"); first.put("value", "您好,有一条来自微博热搜榜的预警通知。");
...@@ -78,35 +81,44 @@ public class SendWeiboHotSearchRun extends Thread{ ...@@ -78,35 +81,44 @@ public class SendWeiboHotSearchRun extends Thread{
JSONObject remark = new JSONObject(); JSONObject remark = new JSONObject();
remark.put("value", "知微情报监测服务"); remark.put("value", "知微情报监测服务");
dataMap.put("remark", remark); dataMap.put("remark", remark);
List<String> userList = getUserList(); List<String> userList = getUserList();
if(userList!=null && userList.size()>0) { if (userList != null && userList.size() > 0) {
for (String openId : userList) { for (String openId : userList) {
Template template = new Template(); Template template = new Template();
template.setTouser(openId); template.setTouser(openId);
if(url!=null){ if (url != null) {
template.setUrl(url); template.setUrl(url);
} }
template.setTemplate_id(WechatConstant.WECHAT_TEMPLATEID_EARLY_IT); template.setTemplate_id(WechatConstant.WECHAT_TEMPLATEID_EARLY_IT);
template.setData(dataMap); template.setData(dataMap);
JSONObject templateJson = (JSONObject)JSONObject.toJSON(template); JSONObject templateJson = (JSONObject) JSONObject.toJSON(template);
WechatCodeUtil.sendDataJson(templateJson); WechatCodeUtil.sendDataJson(templateJson);
} }
} else {
logger.info("拉取微博用户列表失败");
} }
} }
/** /**
* @Title: getUserList * @Title: getUserList
* @author hero * @author hero
* @Description: 用户列表 * @Description: 用户列表
* @param @param projectName * @param @param
* @param @return 设定文件 * projectName
* @param @return
* 设定文件
* @return List<String> 返回类型 * @return List<String> 返回类型
*/ */
public static List<String> getUserList() public static List<String> getUserList() {
{ for (int i = 0; i < 3; i++) {
List<String> userList = WechatCodeUtil.getUserList("weibohot"); List<String> userList = WechatCodeUtil.getUserList("weibohot");
return userList; if (userList != null) {
return userList;
} else {
continue;
}
}
return null;
} }
} }
...@@ -42,6 +42,7 @@ public class SendZhihuHotSearchRun extends Thread{ ...@@ -42,6 +42,7 @@ public class SendZhihuHotSearchRun extends Thread{
} }
} }
}else{ }else{
logger.info("知乎最近一小时无数据");
sendTemplateByUserIds("最近一小时无数据", TimeParse.dateFormartString(new Date(), "yyyy-MM-dd HH:mm:ss"), null); sendTemplateByUserIds("最近一小时无数据", TimeParse.dateFormartString(new Date(), "yyyy-MM-dd HH:mm:ss"), null);
} }
} }
...@@ -115,8 +116,15 @@ public class SendZhihuHotSearchRun extends Thread{ ...@@ -115,8 +116,15 @@ public class SendZhihuHotSearchRun extends Thread{
private static List<String> getUserList() private static List<String> getUserList()
{ {
List<String> userList = new ArrayList<String>(); List<String> userList = new ArrayList<String>();
List<String> lpUserList = WechatCodeUtil.getUserList("LP组"); for(int i=0;i<3;i++){
userList.addAll(lpUserList); List<String> lpUserList = WechatCodeUtil.getUserList("LP组");
if(lpUserList!=null){
userList.addAll(lpUserList);
break;
}else{
continue;
}
}
return userList; return userList;
} }
......
...@@ -39,6 +39,7 @@ public class WechatCodeUtil { ...@@ -39,6 +39,7 @@ public class WechatCodeUtil {
} }
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace();
logger.error("获取微信公众号推送token失败,问题为:::{}", e.fillInStackTrace()); logger.error("获取微信公众号推送token失败,问题为:::{}", e.fillInStackTrace());
return null; return null;
} }
...@@ -87,17 +88,23 @@ public class WechatCodeUtil { ...@@ -87,17 +88,23 @@ public class WechatCodeUtil {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static List<String> getUserList(String groupName) { public static List<String> getUserList(String groupName) {
String url = "https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token=" + getToken();
JSONObject postData = new JSONObject();
postData.put("tagid", getGroupIp(groupName));
postData.put("next_openid", "");
try { try {
JSONObject jsonObject = HttpRequest.httpRequest(url, "GET", postData.toString()); String token = getToken();
if (null != jsonObject) { if(token!=null){
if(jsonObject.containsKey("data")) { String url = "https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token="+token;
return (List<String>) jsonObject.getJSONObject("data").getObject("openid", List.class); JSONObject postData = new JSONObject();
postData.put("tagid", getGroupIp(groupName));
postData.put("next_openid", "");
JSONObject jsonObject = HttpRequest.httpRequest(url, "GET", postData.toString());
if (null != jsonObject) {
if(jsonObject.containsKey("data")) {
return (List<String>) jsonObject.getJSONObject("data").getObject("openid", List.class);
}
} }
}else{
logger.info("token 获取失败");
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment