-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PS-1575: Daily cleanup for in-memory H2 DB to avoid OOM
- Loading branch information
Showing
6 changed files
with
91 additions
and
45 deletions.
There are no files selected for viewing
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
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
36 changes: 36 additions & 0 deletions
36
src/main/java/org/test/consumer/job/PurgeOldEventsJob.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,36 @@ | ||
package org.test.consumer.job; | ||
|
||
import java.time.Clock; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.criteria.CriteriaBuilder; | ||
import javax.persistence.criteria.CriteriaDelete; | ||
import javax.persistence.criteria.Root; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.test.consumer.domain.EventMessage; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class PurgeOldEventsJob { | ||
private final EntityManager entityManager; | ||
|
||
@Scheduled(cron = "0 0 * * *") | ||
@Transactional | ||
public void purge() { | ||
log.info("Starting the purging"); | ||
LocalDateTime todayStartOfDay = LocalDate.now(Clock.systemUTC()).atStartOfDay(); | ||
CriteriaBuilder cb = entityManager.getCriteriaBuilder(); | ||
CriteriaDelete<EventMessage> deleteQ = cb.createCriteriaDelete(EventMessage.class); | ||
Root<EventMessage> from = deleteQ.from(EventMessage.class); | ||
deleteQ.where(cb.lessThan(from.get("createdAt"), todayStartOfDay)); | ||
|
||
entityManager.createQuery(deleteQ).executeUpdate(); | ||
log.info("Purging finished"); | ||
} | ||
} |
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
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
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