TsbAbstractJob.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package com.style24.batch.biz.job;
  2. import java.sql.Timestamp;
  3. import java.util.Collection;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import com.style24.batch.biz.service.TsbBatchService;
  7. import com.style24.persistence.domain.BatchLog;
  8. import lombok.extern.slf4j.Slf4j;
  9. /**
  10. * Batch Abastract Class
  11. *
  12. * @author gagamel
  13. * @since 2020. 11. 13
  14. */
  15. @Slf4j
  16. public abstract class TsbAbstractJob<I, O, R> {
  17. private long startTime = 0L;
  18. private long endTime = 0L;
  19. private String batchId;
  20. private String batchName;
  21. private I readItem = null;
  22. private O convertedItem = null;
  23. private R resultItem = null;
  24. public int totalCount = 0;
  25. @Autowired
  26. private TsbBatchService batchService;
  27. private Integer batchLogSq; // 배치로그일련번호
  28. /**
  29. * Batch Job 시작 로그를 출력한다.
  30. */
  31. protected void printStart() {
  32. startTime = System.currentTimeMillis();
  33. log.info("################################################################################");
  34. log.info("{} Start!!!\n", batchName);
  35. }
  36. /**
  37. * Batch Job 결과 건수 로그를 출력한다.
  38. */
  39. @SuppressWarnings("rawtypes")
  40. protected void printResult(int successCount, int failureCount) {
  41. if (readItem instanceof Collection) {
  42. totalCount = ((Collection)readItem).size();
  43. log.info("--------------------------------------------------------------------------------");
  44. log.info("배치: {}, 대상: {}건 중 성공: {}건, 실패: {}건", batchName, totalCount, successCount, failureCount);
  45. log.info("--------------------------------------------------------------------------------");
  46. }
  47. }
  48. /**
  49. * Batch Job 결과 건수 로그를 출력한다.
  50. */
  51. @SuppressWarnings("rawtypes")
  52. protected void printResult(int totalCount, int successCount, int failureCount) {
  53. log.info("--------------------------------------------------------------------------------");
  54. log.info("배치: {}, 대상: {}건 중 성공: {}건, 실패: {}건", batchName, totalCount, successCount, failureCount);
  55. log.info("--------------------------------------------------------------------------------");
  56. }
  57. /**
  58. * Batch Job 종료 로그를 출력한다.
  59. */
  60. protected void printEnd() {
  61. if (StringUtils.isNotBlank(batchId)) {
  62. // 배치로그 종료 처리
  63. batchService.updateBatchLog(batchLogSq, batchId);
  64. }
  65. endTime = System.currentTimeMillis();
  66. log.info("Start Time: {}", new Timestamp(startTime));
  67. log.info("End Time: {}", new Timestamp(endTime));
  68. log.info("Processing Time: {} seconds\n", ((endTime - startTime) / 1000.0));
  69. log.info("{} End!!!", batchName);
  70. log.info("################################################################################\n\n");
  71. }
  72. /**
  73. * 배치ID로 배치를 실행한다.
  74. * @param batchId - 배치ID
  75. * @throws Exception
  76. */
  77. public void runById(String batchId) throws Exception {
  78. this.batchId = batchId;
  79. if (StringUtils.isNotBlank(batchId)) {
  80. // 배치명 조회
  81. BatchLog batchLog = batchService.getBatchInfo(batchId);
  82. if (batchLog == null) {
  83. log.info("{} 배치는 미사용으로 종료합니다.(TB_BATCH 테이블 참조)", batchId);
  84. return;
  85. }
  86. if (batchLog.getBatchStat().equals("I")) {
  87. log.info("{} 배치는 현재 실행중입니다.(TB_BATCH 테이블 참조)", batchId);
  88. return;
  89. }
  90. batchName = batchLog.getBatchNm();
  91. // 배치로그 생성
  92. batchLogSq = batchService.createBatchLog(batchId);
  93. }
  94. this.printStart();
  95. readItem = this.read();
  96. if (readItem != null) {
  97. convertedItem = this.process(readItem);
  98. resultItem = this.write(convertedItem);
  99. this.notify(resultItem);
  100. } else { // 배치 처리할 대상 데이터가 없으면 종료
  101. log.info("처리할 데이터가 없습니다.\n");
  102. }
  103. this.printEnd();
  104. }
  105. /**
  106. * 배치명으로 배치를 실행한다.
  107. * @param batchId - 배치ID
  108. * @throws Exception
  109. */
  110. public void run(String batchName) throws Exception {
  111. this.batchName = batchName;
  112. this.printStart();
  113. readItem = this.read();
  114. if (readItem != null) {
  115. convertedItem = this.process(readItem);
  116. resultItem = this.write(convertedItem);
  117. this.notify(resultItem);
  118. } else { // 배치 처리할 대상 데이터가 없으면 종료
  119. log.info("처리할 데이터가 없습니다.\n");
  120. }
  121. this.printEnd();
  122. }
  123. // /**
  124. // * 데이터 로그를 출력한다.
  125. // * @param dataMap - 데이터
  126. // */
  127. // protected void printRowValue(GagaMap dataMap) {
  128. // Iterator<Entry<Object, Object>> itr = dataMap.entrySet().iterator();
  129. //
  130. // StringBuilder sb = new StringBuilder();
  131. // while (itr.hasNext()) {
  132. // Entry<Object, Object> entry = itr.next();
  133. // sb.append(" | ").append(entry.getValue());
  134. // }
  135. //
  136. // String value = "";
  137. // if (StringUtils.hasLength(sb.toString())) {
  138. // value += "[" + sb.toString().substring(2) + "]";
  139. // logger.info(value);
  140. // }
  141. // }
  142. /**
  143. * 배치 처리할 항목을 조회한다.
  144. * @return 배치 처리를 위해 조회한 항목
  145. * @throws Exception
  146. */
  147. public abstract I read() throws Exception;
  148. /**
  149. * 조회한 항목을 실제 데이터 처리를 위해 변환한다. 변환할 데이터가 없으면 아무 처리 하지 않아도 된다.
  150. * @param readItem - 조회한 항목
  151. * @return 변환된 항목
  152. * @throws Exception
  153. */
  154. public abstract O process(I readItem) throws Exception;
  155. /**
  156. * 변환된 항목으로 배치를 처리하고, 그 결과를 조회한다.
  157. * @param convertedItem - 변환된 항목
  158. * @return 처리결과 항목
  159. * @throws Exception
  160. */
  161. public abstract R write(O convertedItem) throws Exception;
  162. /**
  163. * 배치처리결과를 노티한다.
  164. * @param resultItem - 배치처리결과 항목
  165. * @throws Exception
  166. */
  167. public abstract void notify(R resultItem) throws Exception;
  168. }