2323
2424/**
2525 * LogBackgroundServiceScheduler
26- *
26+ *
2727 * @author Eric Martin
2828 */
2929public class LogBackgroundServiceScheduler extends CustomScheduler {
30-
30+
3131 /**
3232 * One second (milliseconds)
3333 */
3434 private static final long ONE_SECOND = 1000 ;
35-
35+
3636 /**
3737 * Five seconds (milliseconds)
3838 */
3939 private static final long FIVE_SECONDS = 5000 ;
40-
40+
4141 /**
4242 * One minute (milliseconds)
4343 */
4444 private static final long ONE_MINUTE = 60000 ;
45-
45+
4646 /**
4747 * Five minutes (milliseconds)
4848 */
4949 private static final long FIVE_MINUTES = 300000 ;
50-
50+
5151 /**
5252 * Schedule delay (in milliseconds)
5353 */
5454 private long scheduleDelay = ONE_SECOND ;
55-
55+
5656 /**
5757 * UTC timestamp of the HTTP error
5858 */
5959 private long lastHttpError = 0 ;
60-
60+
6161 /**
6262 * Sets the next scheduled delay based on the number of messages sent
6363 * @param numSent The number of log messages sent
6464 */
6565 public void update (final int numSent ) {
66-
66+
6767 // Reset the last HTTP error
68-
68+
6969 lastHttpError = 0 ;
70-
70+
7171 // adjust the schedule delay based on the number of messages sent in the last iteration
72-
72+
7373 if (100 <= numSent ) {
74-
74+
7575 // messages are coming in quickly so decrease our delay
7676 // minimum delay is 1 second
77-
77+
7878 scheduleDelay = Math .max (Math .round (scheduleDelay / 2.0 ), ONE_SECOND );
79-
79+
8080 } else if (numSent < 10 ) {
81-
81+
8282 // messages are coming in rather slowly so increase our delay
8383 // maximum delay is 5 seconds
84-
84+
8585 scheduleDelay = Math .min (Math .round (1.25 * scheduleDelay ), FIVE_SECONDS );
86-
86+
8787 }
8888 }
8989
9090 /**
9191 * Sets the next scheduled delay based on the HTTP transmission status
9292 * @param t The exception
93- * @param status The HTTP transmission status
9493 */
9594 public void update (final Throwable t ) {
96-
95+
9796 // see if the exception indicates an authorization problem
98-
97+
9998 boolean isAuthorized = true ;
100-
99+
101100 if (t instanceof HttpException ) {
102101 HttpException httpException = (HttpException ) t ;
103-
102+
104103 if (httpException .getStatusCode () == HttpURLConnection .HTTP_UNAUTHORIZED ) {
105104 isAuthorized = false ;
106105 }
107106 }
108-
109- // adjust the schedule delay based on the type of error
110-
107+
108+ // adjust the schedule delay based on the type of error
109+
111110 if (isAuthorized ) {
112-
111+
113112 // Set the last HTTP error (if not already set)
114113
115114 if (lastHttpError == 0 ) {
@@ -121,19 +120,19 @@ public void update(final Throwable t) {
121120 // Max schedule delay = 1 minute
122121
123122 long sinceFirstError = System .currentTimeMillis () - lastHttpError ;
124-
123+
125124 scheduleDelay = Math .min (Math .max (sinceFirstError , ONE_SECOND ), ONE_MINUTE );
126-
125+
127126 } else {
128-
127+
129128 // Set the last HTTP error
130129 // Set the schedule delay to five minutes
131-
130+
132131 lastHttpError = System .currentTimeMillis ();
133132 scheduleDelay = FIVE_MINUTES ;
134133 }
135134 }
136-
135+
137136 /**
138137 * @return the scheduleDelay
139138 */
0 commit comments