RemoveObsoleteProductProcessorImpl implements RemoveObsoleteProductProcessorInternal, JobStatusAware { @Override public void run() { final int minDays = getObsoleteMinDays(); final LocalDateTime time = now().plusDays(-minDays); final int batchSize = getObsoleteBatchSize(); listener.notifyMessage("Remove obsolete products unavailable before {} (min days {}), batch: {}", time, minDays, batchSize); final List<String> potentialObsoleteSku = skuWarehouseService.findProductSkuByUnavailableBefore(time); LOG.info("Potentially obsolete SKU count: {}", potentialObsoleteSku.size()); listener.count(UNAVAILABLE_COUNTER, potentialObsoleteSku.size()); for (final String skuCode : potentialObsoleteSku) { if (self().removeProductSkuIfInventoryDisabledSince(skuCode, time)) { int count = listener.count(REMOVED_COUNTER); if (count >= batchSize) { break; } } } listener.notifyCompleted(); listener.reset(); } RemoveObsoleteProductProcessorImpl(final ProductService productService,
                                              final ProductCategoryService productCategoryService,
                                              final GenericDAO<AttrValueProduct, Long> attrValueEntityProductDao,
                                              final ProductSkuService productSkuService,
                                              final GenericDAO<AttrValueProductSku, Long> attrValueEntityProductSkuDao,
                                              final GenericDAO<ProductAssociation, Long> productAssociationDao,
                                              final SkuWarehouseService skuWarehouseService,
                                              final SystemService systemService); @Override JobStatus getStatus(final String token); @Override void run(); @Override boolean removeProductSkuIfInventoryDisabledSince(final String skuCode, final LocalDateTime time); RemoveObsoleteProductProcessorInternal getSelf();  }
@Test public void testRun() throws Exception { final long _1999_01_01 = DateUtils.iParseSDT("1999-01-01").toEpochMilli(); final int days = (int) Math.ceil(((double) (java.lang.System.currentTimeMillis() - _1999_01_01)) / ((double) (1000*60*60*24))); final ProductService productService = (ProductService) ctx().getBean(ServiceSpringKeys.PRODUCT_SERVICE); final ProductSkuService productSkuService = (ProductSkuService) ctx().getBean(ServiceSpringKeys.PRODUCT_SKU_SERVICE); getTx().execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(final TransactionStatus status) { createProducts( new Pair<>("OBS-001", "1999-01-01"), new Pair<>("OBS-002", "1999-01-05"), new Pair<>("OBS-003", "1999-01-10"), new Pair<>("OBS-004", "1999-01-15") ); } }); assertNotNull(productService.getProductBySkuCode("OBS-001")); assertNotNull(productSkuService.getProductSkuBySkuCode("OBS-001")); assertNotNull(productService.getProductBySkuCode("OBS-002")); assertNotNull(productSkuService.getProductSkuBySkuCode("OBS-002")); assertNotNull(productService.getProductBySkuCode("OBS-003")); assertNotNull(productSkuService.getProductSkuBySkuCode("OBS-003")); assertNotNull(productService.getProductBySkuCode("OBS-004")); assertNotNull(productSkuService.getProductSkuBySkuCode("OBS-004")); getTx().execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(final TransactionStatus status) { getRemoveObsoleteProductProcessor(days - 5, 1).run(); } }); assertNull(productService.getProductBySkuCode("OBS-001")); assertNull(productSkuService.getProductSkuBySkuCode("OBS-001")); assertNotNull(productService.getProductBySkuCode("OBS-002")); assertNotNull(productSkuService.getProductSkuBySkuCode("OBS-002")); assertNotNull(productService.getProductBySkuCode("OBS-003")); assertNotNull(productSkuService.getProductSkuBySkuCode("OBS-003")); assertNotNull(productService.getProductBySkuCode("OBS-004")); assertNotNull(productSkuService.getProductSkuBySkuCode("OBS-004")); getTx().execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(final TransactionStatus status) { getRemoveObsoleteProductProcessor(days - 12, 10).run(); } }); assertNull(productService.getProductBySkuCode("OBS-001")); assertNull(productSkuService.getProductSkuBySkuCode("OBS-001")); assertNull(productService.getProductBySkuCode("OBS-002")); assertNull(productSkuService.getProductSkuBySkuCode("OBS-002")); assertNull(productService.getProductBySkuCode("OBS-003")); assertNull(productSkuService.getProductSkuBySkuCode("OBS-003")); assertNotNull(productService.getProductBySkuCode("OBS-004")); assertNotNull(productSkuService.getProductSkuBySkuCode("OBS-004")); }