我是一个复杂交易场景的应用程序。
我做了很多交易的任务。我尝试简化它以向您展示这个概念:
我是一个主要任务,为从db中提取的每个约会打开一个事务:
@Component
public class MainTask {
@Inject
TransactionTemplate transactionTemplate;
@Scheduled(cron = "*/30 * * * * ?")
public void execute() {
List<Long> appointments = new ArrayList<Long>();
transactionTemplate.execute(status -> {
try {
appointments= communicationClass.loadAppointments();
} catch (Exception e) {
log.error("", e);
status.setRollbackOnly();
}
return null;
});
appointmens.parallelStream().forEach(id -> {
transactionTemplate.execute(status -> {
try {
communicationClass.evaluateSms(id);
} catch (Exception e) {
log.error("", e);
status.setRollbackOnly();
}
return null;
});
这是CommunicationClass:
@Component
public class CommunicationClass{
@PersistenceContext
EntityManager entityManager;
public List<Long> loadAppointments() {
//CALL A SERVICE TO GET THE LIST OF APPOINTMENTS
return appointmentService.loadAppointments();
}
public void evaluateSms(long idAppuntamento) {
boolean orarioSms = templateServiceImpl.canInviaSms(template, appuntamento);
//REMOTE CALL
boolean esitoInvio = inviaSmsGateway(appuntamento, sms, template);
if (esitoInvio == false)
throw new RuntimeException("Rollback of the entire transaction");
}
这是之前使用的TemplateServiceImpl类:
@Service
@Transactional
public class TemplateServiceImpl {
public boolean canInviaSms(Template template, IHasComunicazioni oggetto) {
//DO SOMETHING WITH OBJECTS RECEIVED
}
我的问题是:
谢谢