|
28 | 28 | import org.bukkit.plugin.RegisteredServiceProvider; |
29 | 29 | import org.bukkit.plugin.ServicePriority; |
30 | 30 | import org.maxgamer.quickshop.util.JsonUtil; |
31 | | -import org.maxgamer.quickshop.util.Util; |
32 | 31 |
|
33 | 32 | import javax.net.ssl.HttpsURLConnection; |
34 | | -import java.io.*; |
| 33 | +import java.io.BufferedReader; |
| 34 | +import java.io.ByteArrayOutputStream; |
| 35 | +import java.io.DataOutputStream; |
| 36 | +import java.io.File; |
| 37 | +import java.io.IOException; |
| 38 | +import java.io.InputStreamReader; |
35 | 39 | import java.lang.reflect.InvocationTargetException; |
36 | 40 | import java.lang.reflect.Method; |
37 | 41 | import java.net.URL; |
38 | 42 | import java.nio.charset.StandardCharsets; |
39 | | -import java.util.*; |
| 43 | +import java.util.ArrayList; |
| 44 | +import java.util.Collection; |
| 45 | +import java.util.List; |
| 46 | +import java.util.Map; |
| 47 | +import java.util.UUID; |
40 | 48 | import java.util.concurrent.Callable; |
| 49 | +import java.util.concurrent.Executors; |
| 50 | +import java.util.concurrent.ScheduledExecutorService; |
| 51 | +import java.util.concurrent.ThreadFactory; |
| 52 | +import java.util.concurrent.TimeUnit; |
41 | 53 | import java.util.logging.Level; |
42 | 54 | import java.util.zip.GZIPOutputStream; |
43 | 55 |
|
@@ -248,33 +260,35 @@ private static byte[] compress(final String str) throws IOException { |
248 | 260 | return outputStream.toByteArray(); |
249 | 261 | } |
250 | 262 |
|
| 263 | + // This ThreadFactory enforces the naming convention for our Threads |
| 264 | + private final ThreadFactory threadFactory = task -> new Thread(task, "bStats-Metrics"); |
| 265 | + |
| 266 | + // Executor service for requests |
| 267 | + // We use an executor service because the Bukkit scheduler is affected by server lags |
| 268 | + private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1, threadFactory); |
| 269 | + |
251 | 270 | /** |
252 | 271 | * Starts the Scheduler which submits our data every 30 minutes. |
253 | 272 | */ |
254 | 273 | private void startSubmitting() { |
255 | | - final Timer timer = |
256 | | - new Timer(true); // We use a timer cause the Bukkit scheduler is affected by server lags |
257 | | - timer.scheduleAtFixedRate( |
258 | | - new TimerTask() { |
259 | | - @Override |
260 | | - public void run() { |
261 | | - if (!plugin.isEnabled()) { // Plugin was disabled |
262 | | - timer.cancel(); |
263 | | - return; |
264 | | - } |
265 | | - // Nevertheless we want our code to run in the Bukkit main thread, so we have to use the |
266 | | - // Bukkit scheduler |
267 | | - // Don't be afraid! The connection to the bStats server is still async, only the stats |
268 | | - // collection is sync ;) |
269 | | - Util.mainThreadRun(() -> submitData()); |
270 | | - } |
271 | | - }, |
272 | | - 1000 * 60 * 5, |
273 | | - 1000 * 60 * 30); |
274 | | - // Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough |
275 | | - // time to start |
276 | | - // WARNING: Changing the frequency has no effect but your plugin WILL be blocked/deleted! |
277 | | - // WARNING: Just don't do it! |
| 274 | + final Runnable submitTask = () -> { |
| 275 | + if (!plugin.isEnabled()) { // Plugin was disabled |
| 276 | + scheduler.shutdown(); |
| 277 | + return; |
| 278 | + } |
| 279 | + // Nevertheless we want our code to run in the Bukkit main thread, so we have to use the Bukkit scheduler |
| 280 | + // Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;) |
| 281 | + plugin.getServer().getScheduler().runTask(plugin, this::submitData); |
| 282 | + }; |
| 283 | + |
| 284 | + // Many servers tend to restart at a fixed time at xx:00 which causes an uneven distribution of requests on the |
| 285 | + // bStats backend. To circumvent this problem, we introduce some randomness into the initial and second delay. |
| 286 | + // WARNING: You must not modify and part of this Metrics class, including the submit delay or frequency! |
| 287 | + // WARNING: Modifying this code will get your plugin banned on bStats. Just don't do it! |
| 288 | + long initialDelay = (long) (1000 * 60 * (3 + Math.random() * 3)); |
| 289 | + long secondDelay = (long) (1000 * 60 * (Math.random() * 30)); |
| 290 | + scheduler.schedule(submitTask, initialDelay, TimeUnit.MILLISECONDS); |
| 291 | + scheduler.scheduleAtFixedRate(submitTask, initialDelay + secondDelay, 1000 * 60 * 30, TimeUnit.MILLISECONDS); |
278 | 292 | } |
279 | 293 |
|
280 | 294 | /** |
|
0 commit comments