spring-boot - 如何在soap webservice中实现两阶段提交?
问题描述
我尝试在我的应用程序中实现两阶段提交。这个概念是我的第一个应用程序公开了带有数据的 web 服务(获取和下一个删除数据),而我的第二个应用程序是一个使用该数据的客户端(在数据库上映射和保存数据)。如果在第一个应用程序数据库中的消费数据更改期间出现错误,则应回滚。我试图实现原子事务(https://javaee.github.io/metro/doc/user-guide/ch18.html),但它没有按我的意愿工作。
网络服务:
package pl.com.webServices.endpoints;
import com.sun.xml.ws.api.tx.at.Transactional;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
import pl.service.KpService;
import pl.GetKP;
import pl.GetResponseKP;
@Transactional(value = Transactional.TransactionFlowType.MANDATORY,
version = com.sun.xml.ws.api.tx.at.Transactional.Version.WSAT10)
@TransactionAttribute(TransactionAttributeType.MANDATORY)
@Endpoint
public class KpEndpoint {
private final KpService kpService;
@Autowired
public KpEndpoint(KpService kpService) {
this.kpService = kpService;
}
@PayloadRoot(namespace = "http://xxx.com.pl/webservice",
localPart = "getKp")
@ResponsePayload
public GetResponseKp getKp(@RequestPayload GetKp getKp) {
return kpService.przygotujGetResponseKp();
}
}
客户:
package pl.com.service;
import com.sun.xml.ws.api.tx.at.Transactional;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.xml.bind.JAXBException;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import pl.com.wsdl.xxx.GetResponseKp;
@Transactional(value = Transactional.TransactionFlowType.MANDATORY,
version = com.sun.xml.ws.api.tx.at.Transactional.Version.WSAT10)
@Service
public class BzClientService {
@Autowired
private BzClient bzClient;
@Autowired
private KpService KpService ;
@Autowired
private ModelMapper mapper;
@Scheduled(cron = "0 0/1 * * * ?")
public void getResponseKp() {
GetResponseKp kpResponse = null;
try {
kpResponse = bzClient.getResponseKp();
} catch (URISyntaxException | JAXBException ex) {
Logger.getLogger(BzClientService.class.getName()).log(Level.SEVERE, null, ex);
}
List<Kp> kp = kpResponse == null ? Collections.EMPTY_LIST : kpResponse.getKp();
kpService.dodajKp(kp);
}
而如果第二种应用方法会出现错误——“kpService.dodajKp(kp);” 然后更改第一个应用程序方法 - “kpService.przygotujGetResponseKp();” 应该回滚。