hz-zhhq-app-service/greenH5modul/.svn/pristine/4c/4c41199738fdd74328899eaa32f...

281 lines
8.3 KiB
Plaintext

package com.nationalelectirc.utils;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.nationalelectric.greenH5.MyException;
import com.sgcc.uap.persistence.IHibernateDao;
//import com.nationalelectirc.Constant.Constant;
import net.sf.json.JSONObject;
/*
* 消息推送工具类
*/
public class PushMessageUtil {
// 2017-9-29 开发调试请使用此地址
// public static final String getTokenUrl = "https://mam1.ft-power.com.cn:10030/bg_task/rest/loadAppToken.do";
// public static final String sendMessageUrl = "https://mam1.ft-power.com.cn:10030/bg_task/rest/sendMessage.do";
public ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(5);
private static PushMessageUtil pushMessageUtil = new PushMessageUtil();
private PushMessageUtil() {
}
public static PushMessageUtil getInstance() {
return pushMessageUtil;
}
public void pushMessage(String msgname, String tm, String users,IHibernateDao hibernateDao) throws IOException {
executor.schedule(new PushTask(msgname, tm, users,hibernateDao), 1, TimeUnit.SECONDS);
}
private class PushTask implements Runnable {
private String msgname;
private String tm;
private String users;
private IHibernateDao hibernateDao;
public PushTask(String msgname, String tm, String users,IHibernateDao hibernateDao) {
this.msgname = msgname;
this.tm = tm;
this.users = users;
this.hibernateDao = hibernateDao;
}
@Override
public void run() {
try {
pushMsg(msgname, tm, users,hibernateDao);
} catch (IOException e) {
} catch (MyException e) {
e.printStackTrace();
}
}
private void pushMsg(String msgname, String tm, String users,IHibernateDao hibernateDao) throws IOException, MyException {
System.out.println("msgname=="+msgname+"==tm=="+tm+"==users=="+users);
InputStream inputStream = null;
OutputStream outputStream = null;
DataOutputStream out = null;
BufferedReader reader = null;
try {
System.out.println("111");
List<Map<String, Object>> queryForListWithSql = hibernateDao.queryForListWithSql("SELECT * FROM green_dictionary_info WHERE data_type in('pushTokenUrl','sendMessageUrl','pushAppcode','pushUmsecret')");
if (queryForListWithSql.size() != 4) {
throw new MyException("推送数据有误!");
}
String getTokenUrl = queryForListWithSql.get(0).get("data_value").toString();
String sendMessageUrl = queryForListWithSql.get(1).get("data_value").toString();
String appcode = queryForListWithSql.get(2).get("data_value").toString();
String umsecret = queryForListWithSql.get(3).get("data_value").toString();
// String appcode = "SYS";
// String umsecret = "123456";
/*String getTokenUrl = "https://mam.sgcc.com.cn/bg_task/rest/loadAppToken.do";
String sendMessageUrl = "https://mam.sgcc.com.cn/bg_task/rest/sendMessage.do";
String appcode = "SGCCINTLSHQ";
String umsecret ="R65G708e5I4";*/
URL url = new URL(getTokenUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
connection.connect();
System.out.println("222");
// POST请求
outputStream = connection.getOutputStream();
System.out.println("333");
out = new DataOutputStream(outputStream);
com.alibaba.fastjson.JSONObject object = new com.alibaba.fastjson.JSONObject();
object.put("appcode", appcode);
object.put("umsecret", umsecret);
String objJson = object.toString();
// JSONObject obj = new JSONObject();
// // 应用编码
// obj.element("appcode", appcode);
// // 应用消息推送密码
// obj.element("umsecret", umsecret);
out.writeBytes("jsonData=" + objJson);
out.flush();
System.out.println("444");
// 读取响应
inputStream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sb.append(lines);
// 安全检测 DOS
if (sb.length() > 9999999) {
throw new IOException("input too long");
}
}
String parameter = sb.toString();
// JSONObject jsonObject = JSONObject.fromObject(parameter);
com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(parameter);
String token = jsonObject.getJSONObject("data").get("token").toString();
System.out.println("token5555"+token);
String usertype = "1";
sendmsg(appcode, token, msgname, tm, usertype, users,sendMessageUrl);
// 断开连接
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
}
try {
if (outputStream != null) {
outputStream.close();
}
} catch (Exception e) {
}
try {
if (out != null) {
out.close();
}
} catch (Exception e) {
}
try {
if (reader != null) {
reader.close();
}
} catch (Exception e) {
}
}
}
private void sendmsg(String appcode, String token, String msgname, String tm, String usertype, String users,String sendMessageUrl)
throws IOException {
InputStream inputStream = null;
OutputStream outputStream = null;
DataOutputStream out = null;
BufferedReader reader = null;
try {
System.out.println("sendmsg777");
// 创建连接
URL url = new URL(sendMessageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
connection.connect();
// POST请求
outputStream = connection.getOutputStream();
out = new DataOutputStream(outputStream);
// JSONObject obj = new JSONObject();
com.alibaba.fastjson.JSONObject obj = new com.alibaba.fastjson.JSONObject();
obj.put("msgname", msgname);
obj.put("subtitle", tm);
obj.put("suburl", "/view/pushInfo.html");// 一点通不适用
obj.put("content", tm);
obj.put("type", "2");
obj.put("msgflag", "1");
obj.put("pushtype", "2");
obj.put("appcode", appcode);
obj.put("token", token);
obj.put("version", "null");
obj.put("address", "");
obj.put("usertype", usertype);
obj.put("useridetype", "1");
obj.put("extend3", "3");
obj.put("priority", "1");
obj.put("users", users);
// 节点标示 此处默认 10 无需修改
obj.put("procode", "10");
out.writeBytes("jsonData=" + URLEncoder.encode(obj.toString(), "utf-8"));
out.flush();
// 读取响应
inputStream = connection.getInputStream();
System.out.println("sendmsg888");
reader = new BufferedReader(new InputStreamReader(inputStream));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "UTF-8");
sb.append(lines);
// 安全检测 DOS
if (sb.length() > 9999999) {
throw new IOException("input too long");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
}
try {
if (outputStream != null) {
outputStream.close();
}
} catch (Exception e) {
}
try {
if (out != null) {
out.close();
}
} catch (Exception e) {
}
try {
if (reader != null) {
reader.close();
}
} catch (Exception e) {
}
}
}
}
}