我正在尝试使用bean执行两个进程,我的问题是我找不到连续执行这些进程的方式。第一个过程是发送对象,第二个过程是对象的响应。
@Component
public class Proceso implements InitializingBean{
private static final String XML_SCHEMA_LOCATION = "/proceso/model/schema/proceso.xsd";
private Envio envio;
private Respuesta respuesta;
public void Proceso_envio(Proceso proceso, OutputStream outputstream) throws JAXBException{
envio.marshal(proceso, outputstream);}
public void Proceso_respuesta(InputStream inputstream) throws JAXBException, FileNotFoundException{
Object obj = unmarshaller.unmarshal(inputStream);
return (Proceso_respuesta) obj;}
@Override
public void afterPropertiesSet() throws Exception{
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(getClass().getResource(XML_SCHEMA_LOCATION));
JAXBContext jc = JAXBContext.newInstance(Envio.class, Respuesta.class);
this.marshaller = jc.createMarshaller();
this.marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
this.marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.displayName());
this.marshaller.setSchema(schema);
this.unmarshaller = jc.createUnmarshaller();
this.unmarshaller.setSchema(schema);
}
我想通过代码我的问题会变得更加清晰。
最佳答案
尝试将 Synchronized 添加到您的方法中
我多次遇到这个麻烦,因为接收者试图读取未完成的内容
javadoc 中的更多信息:https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html
当您将此关键字添加到方法中时,它将等待另一个关键字,直到它完成
关于java - 问题: Sending and Receiving Data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44392223/