BaseRemoveRunner.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package com.zskk.dicom.monitor.runner;
  2. import com.zskk.dicom.monitor.config.Configs;
  3. import java.util.concurrent.TimeUnit;
  4. public abstract class BaseRemoveRunner extends AbstractRemove implements Runnable{
  5. @Override
  6. public void run() {
  7. while (true) {
  8. try {
  9. while (empty()) {
  10. printEmptyAndSleep();
  11. if(exit()) {
  12. return;
  13. }
  14. }
  15. if(remove()) {
  16. printSuccessAndSleep();
  17. } else {
  18. printFailAndSleep();
  19. }
  20. } catch (InterruptedException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. }
  25. private int MIN_FAIL_SLEEP_TIME = 1;
  26. private int MAX_FAIL_SLEEP_TIME = 5;
  27. private int failSleepTime = 0;
  28. private int MIN_EMPTY_SLEEP_TIME = 10;
  29. private int MAX_EMPTY_SLEEP_TIME = 60;
  30. private int emptySleepTime = 0;
  31. protected abstract boolean exit();
  32. protected void printFailAndSleep() throws InterruptedException {
  33. Configs.sysLog.warn(getRunnerName() + ": " + action() + " fail");
  34. if(failSleepTime < MAX_FAIL_SLEEP_TIME) {
  35. failSleepTime += MIN_FAIL_SLEEP_TIME;
  36. }
  37. TimeUnit.SECONDS.sleep(failSleepTime);
  38. }
  39. protected abstract String getRunnerName();
  40. private String action() {
  41. return "upload";
  42. }
  43. protected void printSuccessAndSleep() {
  44. Configs.sysLog.info(getRunnerName() + ": " + action() + " success!" + getQueueName() + " size :" + size());
  45. failSleepTime = 0;
  46. emptySleepTime = 0;
  47. }
  48. protected void printEmptyAndSleep() throws InterruptedException {
  49. Configs.sysLog.warn(getRunnerName() + ": " + getQueueName() + " is empty");
  50. if(emptySleepTime < MAX_EMPTY_SLEEP_TIME) {
  51. emptySleepTime += MIN_EMPTY_SLEEP_TIME;
  52. }
  53. TimeUnit.SECONDS.sleep(emptySleepTime);
  54. }
  55. protected abstract boolean empty();
  56. protected abstract int size();
  57. protected abstract String getQueueName();
  58. }