java - 问题: Sending and Receiving Data

标签 java spring spring-boot marshalling unmarshalling

我正在尝试使用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/

相关文章:

java - 未找到产品名称 : [Impala] 的数据库类型

java - JFrame全屏对焦。

java - 在 jFrame 中的同一 x 轴上创建 2 个按钮

java - 从 Tiles View (JSP) 访问 Spring bean

mysql - Spring @Transactional 死锁

java - 如何在 Spring Boot Dockerized 应用程序中连接到特定的本地 MongoDB 实例?

java - Spark : reading local file, 文件应该存在于所有节点上?

java - Partitioned Job 完成后不能自己停止?春批

java - 全局设置 hibernate 'default-cascade' 属性

仅具有客户端凭据的 Spring 安全端点(基本)