SystemHandler.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package com.iamberry.wechat.handles.admin;
  2. import java.io.IOException;
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.concurrent.ExecutorService;
  7. import java.util.concurrent.Executors;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import com.iamberry.wechat.core.entity.mq.MQMessage;
  12. import com.iamberry.wechat.face.mq.EfastOrderService;
  13. import com.iamberry.wechat.tools.WeixinUtil;
  14. import org.apache.commons.lang3.StringUtils;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Controller;
  17. import org.springframework.web.bind.annotation.PathVariable;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import org.springframework.web.bind.annotation.RequestMethod;
  20. import org.springframework.web.bind.annotation.ResponseBody;
  21. import org.springframework.web.servlet.ModelAndView;
  22. import com.iamberry.wechat.tools.NameUtils;
  23. import com.iamberry.wechat.tools.ResponseJson;
  24. import com.iamberry.wechat.tools.loadResultUtil;
  25. import com.iamberry.wechat.utils.StaticCacheMemory;
  26. import com.iamberry.zk.HostInfo;
  27. import com.iamberry.zk.ZookeeperQueue;
  28. /**
  29. * @author 何秀刚
  30. * Class Description: 系统设置
  31. * Create Date:2016年4月25日
  32. * Update Date:2016年4月25日
  33. */
  34. @Controller
  35. @RequestMapping("/admin/system")
  36. public class SystemHandler {
  37. @RequestMapping("/set")
  38. public void set(HttpServletResponse response) {
  39. try {
  40. response.getWriter().write("清空配置成功!");
  41. } catch (IOException e) {
  42. // TODO Auto-generated catch block
  43. e.printStackTrace();
  44. }
  45. }
  46. @RequestMapping("/reSetWechatConfig")
  47. public void setWechatConfig(HttpServletResponse response) {
  48. StaticCacheMemory.setAccessToken(null);
  49. try {
  50. response.getWriter().write("清空微信配置成功!");
  51. } catch (IOException e) {
  52. // TODO Auto-generated catch block
  53. e.printStackTrace();
  54. }
  55. }
  56. @RequestMapping("/reload")
  57. public void reload(HttpServletResponse response) {
  58. try {
  59. loadResultUtil.load();
  60. response.getWriter().write("重新读取资源文件成功");
  61. } catch (IOException e) {
  62. // TODO Auto-generated catch block
  63. e.printStackTrace();
  64. }
  65. }
  66. @RequestMapping("/addMessageInfo")
  67. public ModelAndView addUI() {
  68. ModelAndView mv = new ModelAndView("admin/system/addUI");
  69. return mv;
  70. }
  71. @RequestMapping(value = "/loadMessageInfo", method = RequestMethod.POST)
  72. public void loadMessageInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
  73. // id
  74. final String tempMessageId = request.getParameter("tempMessageId");
  75. // 消息,分隔符位#s
  76. String tempMessageInfo = request.getParameter("tempMessageInfo");
  77. // url
  78. final String tempMessageURL = request.getParameter("tempMessageURL");
  79. // 用户
  80. String tempMessageUsers = request.getParameter("tempMessageUsers");
  81. final String[] users = StringUtils.split(tempMessageUsers, "\r\n");
  82. // format
  83. String tempMessageFormat = request.getParameter("tempMessageFormat");
  84. // 发送
  85. final String message = String.format(tempMessageFormat, StringUtils.split(tempMessageInfo, "#s"));
  86. if (users.length <= 100) {
  87. for (String string : users) {
  88. try {WeixinUtil.sendTemplateMessage(NameUtils.appId, NameUtils.appSecret, string, tempMessageId, message, tempMessageURL);} catch (Exception e) {}
  89. }
  90. response.getWriter().print("发送成功!");
  91. } else {
  92. int lengthTemp = users.length / 100;
  93. int count = users.length % 100 == 0 ? lengthTemp : lengthTemp + 1;
  94. ExecutorService threadPool = Executors.newSingleThreadExecutor();
  95. for (int i = 1; i < count; i++) {
  96. final int taskID = i;
  97. threadPool.execute(new Runnable() {
  98. public void run() {
  99. int startNum = taskID * 100; // 开始
  100. int stopNum = startNum + 100; // 结束
  101. for (;startNum <= stopNum; startNum++) {
  102. System.out.println("count:" + startNum);
  103. try {WeixinUtil.sendTemplateMessage(NameUtils.appId, NameUtils.appSecret, users[startNum], tempMessageId, message, tempMessageURL);} catch (Exception e) {}
  104. }
  105. }
  106. });
  107. }
  108. threadPool.shutdown();// 任务执行完毕,关闭线程池
  109. response.getWriter().print("发送成功!");
  110. }
  111. }
  112. /**
  113. * 系统监听
  114. * @throws InvocationTargetException
  115. * @throws IllegalAccessException
  116. * @throws IOException
  117. * @throws ClassNotFoundException
  118. */
  119. @RequestMapping("/monitor")
  120. public ModelAndView monitor(HttpServletRequest request) throws IllegalAccessException, InvocationTargetException, ClassNotFoundException, IOException {
  121. ModelAndView mv = new ModelAndView("admin/system/listUI");
  122. // mv.addObject("nowDate", DateTimeUtil.format(new Date()));
  123. //
  124. // Map<String, Integer> accessMap = new HashMap<String, Integer>();
  125. // for (Map<String, Integer> map : StaticCacheMemory.lists) {
  126. // for (Entry<String, Integer> entry : map.entrySet()) {
  127. // String key = entry.getKey();
  128. // if (accessMap.containsKey(key)) {
  129. // int value = accessMap.get(key) + entry.getValue();
  130. // accessMap.put(key, value);
  131. // } else {
  132. // accessMap.put(key, 1);
  133. // }
  134. // }
  135. // }
  136. //
  137. // WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
  138. // DataSourceManager dataSourceManager = context.getBean(DataSourceManager.class);
  139. // // 准备数据
  140. // mv.addObject("list", accessMap);
  141. // mv.addObject("dataBaseCount", dataSourceManager.getMaxPoolSize());
  142. return mv;
  143. }
  144. @RequestMapping("/host_list")
  145. public ModelAndView hostListUI() {
  146. ModelAndView mv = new ModelAndView("admin/system/host_list");
  147. List<String> hosts = ZookeeperQueue.listHostInfo();
  148. if (hosts == null || hosts.size() <= 0) {
  149. mv.addObject("list", null);
  150. return mv;
  151. }
  152. List<HostInfo> hostInfos = new ArrayList<HostInfo>();
  153. for (String string : hosts) {
  154. HostInfo hostInfo = ZookeeperQueue.readNodeData(string);
  155. hostInfo.setPath(string);
  156. hostInfos.add(hostInfo);
  157. }
  158. mv.addObject("list", hostInfos);
  159. return mv;
  160. }
  161. @RequestMapping("/cancal_service/{path}")
  162. @ResponseBody
  163. public ResponseJson cancelService(@PathVariable("path") String path) {
  164. ResponseJson json = new ResponseJson();
  165. HostInfo hostInfo = ZookeeperQueue.readNodeData(path);
  166. if (hostInfo == null || hostInfo.getState() == 1) {
  167. json.setReturnCode(200);
  168. json.setResultMsg("关闭中!");
  169. return json;
  170. }
  171. hostInfo.setState(1);
  172. ZookeeperQueue.writeNodeData(path, hostInfo);
  173. json.setReturnCode(200);
  174. return json;
  175. }
  176. }