[英]Problems: Sending and Receiving Data
我正在嘗試使用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);
}
我想有了代碼,我的問題就會變得更加清楚。
嘗試將Syncronized添加到您的方法中
我遇到了很多麻煩,因為接收器試圖讀取未完成的內容
javadoc中的更多信息: https : //docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html
當您將此關鍵字添加到方法中時,它將等待另一個,直到它完成
謝謝maquina,
這是解決方案:
@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 synchronized void Proceso_envio(Proceso proceso, OutputStream outputstream) throws JAXBException{
envio.marshal(proceso, outputstream);}
public void synchronized 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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.