-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathRollbarNotifier.java
More file actions
134 lines (105 loc) · 4.13 KB
/
Copy pathRollbarNotifier.java
File metadata and controls
134 lines (105 loc) · 4.13 KB
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
package com.muantech.rollbar.java;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.helpers.LogLog;
import org.json.JSONException;
import org.json.JSONObject;
public class RollbarNotifier {
public static final int MAX_RETRIES = 5;
private static NotifyBuilder BUILDER;
private static URL URL;
private static final ScheduledExecutorService EXECUTOR = Executors.newScheduledThreadPool(2, new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
// thread.setDaemon(true);
thread.setName("RollbarNotifier-" + new Random().nextInt(100));
return thread;
}
});
public enum Level {
DEBUG, INFO, WARNING, ERROR
}
public static void init(String urlString, String apiKey, String env) throws JSONException, UnknownHostException {
URL = getURL(urlString);
BUILDER = new NotifyBuilder(apiKey, env);
}
public static void notify(String message) {
notify(Level.INFO, message, null);
}
public static void notify(String message, Map<String, Object> context) {
notify(Level.INFO, message, context);
}
public static void notify(Level level, String message) {
notify(level, message, null, null);
}
public static void notify(Level level, String message, Map<String, Object> context) {
notify(level, message, null, context);
}
public static void notify(Throwable throwable) {
notify(Level.ERROR, throwable, null);
}
public static void notify(Throwable throwable, Map<String, Object> context) {
notify(Level.ERROR, throwable, context);
}
public static void notify(String message, Throwable throwable) {
notify(Level.ERROR, message, throwable, null);
}
public static void notify(String message, Throwable throwable, Map<String, Object> context) {
notify(Level.ERROR, message, throwable, context);
}
public static void notify(Level level, Throwable throwable) {
notify(level, null, throwable, null);
}
public static void notify(Level level, Throwable throwable, Map<String, Object> context) {
notify(level, null, throwable, context);
}
public static void notify(final Level level, final String message, final Throwable throwable, final Map<String, Object> context) {
EXECUTOR.execute(new Runnable() {
@Override
public void run() {
try {
JSONObject payload = BUILDER.build(level.toString(), message, throwable, context);
postJson(payload);
} catch (Throwable e) {
LogLog.error("There was an error notifying the error.", e);
}
}
});
}
private static void postJson(JSONObject json) {
HttpRequest request = new HttpRequest(URL, "POST");
request.setRequestProperty("Content-Type", "application/json");
request.setRequestProperty("Accept", "application/json");
request.setBody(json.toString());
boolean success = request.execute();
if (!success && request.getAttemptNumber() < MAX_RETRIES) {
retryRequest(request);
}
}
private static void retryRequest(final HttpRequest request) {
EXECUTOR.schedule(new Runnable() {
@Override
public void run() {
request.execute();
}
}, request.getAttemptNumber(), TimeUnit.SECONDS);
}
private static URL getURL(String urlString) {
URL url;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
LogLog.error("Error parsing the notifiying URL", e);
throw new IllegalArgumentException();
}
return url;
}
}