2017-06-06 13 views
1

私は、Beanを使用して2つのプロセスを実行しようとしています。私の問題は、これらのプロセスが連続して実行される方法を見つけることができないことです。最初のプロセスはオブジェクトを送信し、2番目のプロセスはオブジェクトの応答です。問題:データの送信と受信

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

コードで私の質問はより明確になると思います。 https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html

あなたを:

+1

おそらくあなたは、[スケジューラ](https://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html)を使用することができます –

答えて

1

は、受信機がjavadocの

詳細情報を完了していない何かを読んしようとしていたので、私はこのトラブルを何度も持っていた

あなたの方法にSyncronized追加しようこのキーワードをメソッドに追加します。これは、完了しないまで他のメソッドを待つことになります。

+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); 
    } 
関連する問題