-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
950e216
commit ed7c599
Showing
4 changed files
with
76 additions
and
81 deletions.
There are no files selected for viewing
14 changes: 10 additions & 4 deletions
14
search/src/main/java/enit/ecomerce/search_product/repository/ProducteEntityRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
package enit.ecomerce.search_product.repository; | ||
|
||
import enit.ecomerce.search_product.product.ProductEntity; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
public interface ProducteEntityRepository extends CrudRepository<ProductEntity, String> { | ||
@Repository | ||
public interface ProducteEntityRepository extends JpaRepository<ProductEntity, String> { | ||
|
||
@Query("SELECT p FROM ProductEntity p WHERE p.isIndex = false") | ||
List<ProductEntity> findUnindexedProducts(); | ||
} | ||
@Query(value = "SELECT * FROM product_entity WHERE is_indexed = false LIMIT :batchSize", nativeQuery = true) | ||
List<ProductEntity> findUnindexedProducts(@Param("batchSize") int batchSize); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
search/src/test/java/enit/ecomerce/search_product/service/InboxProductCreationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package enit.ecomerce.search_product.service; | ||
|
||
import enit.ecomerce.search_product.product.Product; | ||
import enit.ecomerce.search_product.product.ProductEntity; | ||
import enit.ecomerce.search_product.repository.ProductRepository; | ||
import enit.ecomerce.search_product.repository.ProducteEntityRepository; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
public class InboxProductCreationService { | ||
@Autowired | ||
private ProducteEntityRepository productEntityRepository; | ||
@Autowired | ||
private ProductRepository productRepository; | ||
|
||
private static final int BATCH_SIZE = 50; // Define your batch size | ||
|
||
@Scheduled(fixedRate = 300000) | ||
@Transactional | ||
public void treatInbox() { | ||
List<ProductEntity> unindexedProducts; | ||
|
||
while (!(unindexedProducts = productEntityRepository.findUnindexedProducts(BATCH_SIZE)).isEmpty()) { | ||
for (ProductEntity product : unindexedProducts) { | ||
try { | ||
productRepository.save(new Product(product)); | ||
product.setIndex(true); | ||
productEntityRepository.save(product); | ||
} catch (Exception e) { | ||
System.err.println("Error processing product: " + product.getId() + " - " + e.getMessage()); | ||
throw e; // Rethrow the exception to trigger transaction rollback | ||
} | ||
} | ||
} | ||
} | ||
} |
67 changes: 0 additions & 67 deletions
67
...h/src/test/java/enit/ecomerce/search_product/service/InboxProductCreationServiceTest.java
This file was deleted.
Oops, something went wrong.