i've implemented an asynchronous job processing using SpringBoot and JobRunr. To store my data, i use Spring Data JPA and the @Transactional annotation. However, the status of my entities is not stored in the database DURING the job execution, just AFTER the job execution has been finished. Since the job needs about 5 minutes to finish, i want to update my entities during the job execution, so the user can inspect the changing progress via GUI.
My Code looks like the following (simplified job call):
@Job(name = "Translation job with id '%0'", retries = 2)
@Transactional
public void executeTranslationJob(String orderId, File translationOrderContent) throws IOException {
TranslationOrder translationOrder = this.translationOrderRepository.findById(orderId).get();
//I want to update the entity's status here
translationOrder.setTranslationOrderStatus(TranslationOrderStatus.PROCESSING);
Iterators.partition(this.translationSegmentRepository.findByTranslatedValueAndOrderId(null, orderId).iterator(), 100).forEachRemaining(segments -> {
machineTranslator.translateSegments(notTranslatedSegments);
//I want to update the entity here after each iteration of 100 segments processed
this.orderStatisticsService.updateTranslationProgress(translationOrder, notTranslatedSegments);
});
this.updateOrderStatus(translationOrder);
}
Entity:
@Entity
@Table(name = "translation_order")
@Data
public class TranslationOrder implements Persistable<String> {
@Id
@Column(name = "id")
private String id;
@Column(name = "order_status")
@Enumerated(EnumType.STRING)
private TranslationOrderStatus translationOrderStatus;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "statistic_id", referencedColumnName = "id", foreignKey=@ForeignKey(name="FK_order_statistic"))
private OrderStatistic orderStatistic;
}
And the repository:
@Repository
public interface TranslationOrderRepository extends JpaRepository<TranslationOrder, String> {
//custom findby-methods
}
How is it possible to update entities during job execution (and so during method execution)?
I tried to call save() or saveAndFlush() after each update statement, but that does not help.