2017-06-06 99 views
1

我想使用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); 
    } 

我想,随着代码我的问题变得更加清晰。

+1

也许可以使用[调度](https://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html) –

回答

1

尝试Syncronized添加到您的方法

我因为接收器试图读取的东西,是不是在Javadoc完成

更多信息有这个烦恼很多次:https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html

当你添加这个关键词的方法,这将等待对方,直到它insnt完成

+1

我理解的方法应该被调用的问题**不断** **不**同步** –

+1

也许他的意思是“同步”:) –

+0

对不起,我有同样的问题,在西班牙语stackokverflow和谷歌翻译没有工作很好 – mantamusica

1

谢谢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); 
    }