1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
package com.gjcloud;
import okhttp3.*;
import java.io.IOException; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger;
public class Alert { private static final int cpuNum = Runtime.getRuntime().availableProcessors(); private static final AtomicInteger increment = new AtomicInteger(); private static volatile ThreadPoolExecutor executor = null;
private static ThreadPoolExecutor getThreadPool() { if (executor == null) { synchronized (Alert.class) { if (executor == null) { int coreNum = cpuNum <= 0 ? 8 : cpuNum; executor = new ThreadPoolExecutor(coreNum, coreNum, 2000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(5000), new ThreadFactory() { @Override public Thread newThread(Runnable r) { return new Thread(Thread.currentThread().getThreadGroup(), r, "Alert-ThreadPool-" + increment.getAndIncrement()); } }, new ThreadPoolExecutor.CallerRunsPolicy()); } } } return executor; }
private static void sendPost(String webworkUrl, String content) { OkHttpClient client = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS) .readTimeout(20, TimeUnit.SECONDS) .build(); MediaType contentType = MediaType.parse("application/json; charset=utf-8"); RequestBody body = RequestBody.create(contentType, content); Request request = new Request.Builder().url(webworkUrl).post(body).addHeader("cache-control", "no-cache").build(); try { Response r = client.newCall(request).execute(); System.err.println(r.toString()); } catch (IOException e) { e.printStackTrace(); } }
public static void ding(String webworkUrl, final String msg, Throwable throwable) { String format = "{\n" + "\t\"msgtype\":\"markdown\",\n" + "\t\"markdown\":{\n" + "\t\t\"title\":\"%s\",\n" + "\t\t\"text\":\"%s\"\n" + "\t}\n" + "}"; final StringBuilder content = new StringBuilder(String.format(format, "uc", msg)); if (throwable != null) { content.append(", e = ").append(throwable.getLocalizedMessage()); }
getThreadPool().execute(() -> sendPost(webworkUrl, content.toString())); }
public static void wechat(String webworkUrl, final String msg, Throwable throwable) { String format = "{\r\n" + " \"msgtype\":\"markdown\",\r\n" + " \"markdown\":{\r\n" + " \"content\":\"%s\r\n\"" + " }\r\n" + "}}"; final StringBuilder content = new StringBuilder(String.format(format, msg)); if (throwable != null) { content.append(", e = ").append(throwable.getLocalizedMessage()); } getThreadPool().execute(() -> sendPost(webworkUrl, content.toString())); }
public static void main(String[] args) { wechat("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7378d8b1-24e9-44ab-a2a6-9e19f3187faf", "公费师范", null); } }
|