AVONnadozie

Timer Task stops after hibernation

Aug 7th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. public class Service {
  2. private static ScheduledFuture<?> mainTask;
  3. public static void startService() {
  4.     ScheduledExecutorService st = Executors.newSingleThreadScheduledExecutor();
  5.         //Start checking for new notification
  6.         mainTask = st.scheduleAtFixedRate(new NotificationTask(), 0, 5, TimeUnit.SECONDS);
  7. //        Timer t = new Timer();
  8. //        t.schedule(new NotificationTask(), 0, 5000);
  9. }
  10.  
  11. static class NotificationTask extends TimerTask implements Runnable {
  12.  
  13.         private JSONObject notifications;
  14.         private JSONObject old;
  15.  
  16.         public NotificationTask() {
  17.             old = new JSONObject();
  18.         }
  19.  
  20.         @Override
  21.         public void run() {
  22.             try {
  23.                 //Read new notification
  24.                 notifications = WebConnection.getInstance().readNotifications();
  25.                 if (!notifications.equals(old)) {
  26.                 //
  27.                     processOrderNotification();
  28.                 }
  29.  
  30.             } catch (IOException | ParseException ex) {
  31.                 //probably Network issues
  32. //                log for debugging purposes
  33.                 Utility.writeLog(ex.toString());
  34.             } catch (Exception e) {
  35. //                Every other error should fall in here
  36.                 Utility.writeLog(e);
  37.             }
  38.         }
  39.  
  40.     }
  41. }
Add Comment
Please, Sign In to add comment