带输入流的Hessian

时间:2018-01-10 09:57:50

标签: java inputstream hessian

我正在尝试构建一个将使用Hessian(或任何其他远程调用)调用的远程服务,但我遇到了InputStreams的问题。黑森州的网站说我最好自己处理它们,但我无法弄清楚如何?

下面是一个使用spring-boot快速启动服务器和简单客户端的简单项目。如果传递的字符串太长而且 java.io.IOException:stream is close

,则对我的远程服务的调用将失败

任何人都可以帮助或指向我全面的文档吗?

简单的服务

public interface EchoService {InputStream echo(String text);}

实施

public class EchoServiceImpl implements EchoService {
  @Override
  public InputStream echo(String text) {
    System.out.println("Echo on server " + text);
    ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes());
    return bis;
  }
}

服务器(使用spring-boot)

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.caucho.HessianServiceExporter;
import org.springframework.remoting.support.RemoteExporter;

@Configuration @ComponentScan @EnableAutoConfiguration
public class Server {
    @Bean(name = "/repeat") 
    RemoteExporter bookingService() {
        HessianServiceExporter exporter = new HessianServiceExporter();
        exporter.setService(new EchoServiceImpl());
        exporter.setServiceInterface( EchoService.class );
        return exporter;
    }
    public static void main(String[] args) {
        SpringApplication.run(Server.class, args); 
    }
}

和客户

import com.caucho.hessian.client.HessianProxyFactory;
import com.google.common.io.ByteStreams;
public class Client {
    public static void main(String[] args) throws Exception {
        HessianProxyFactory factory = new HessianProxyFactory();
        EchoService service = (EchoService) factory.create(EchoService.class, "http://localhost:8080/repeat");
        try(InputStream is = service.echo(args[0])) {
            byte bis[] = ByteStreams.toByteArray(is);
            System.out.println(new String(bis));
        }
    }
}

pom.xml如果需要

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.3.RELEASE</version>
</parent>

<groupId>w.big.data</groupId>
<artifactId>hessian</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.report.sourceEncoding>UTF-8</project.report.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compilet.target>1.8</maven.compilet.target>
</properties>

<dependencies>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>18.0</version>
    </dependency>
    <dependency>
        <groupId>com.caucho</groupId>
        <artifactId>hessian</artifactId>
        <version>4.0.51</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${maven.compiler.source}</source>
                <target>${maven.compiler.source}</target>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

完整的堆栈异常

  

线程“main”中的异常java.io.IOException:stream关闭了   sun.net.www.protocol.http.HttpURLConnection $ HttpInputStream.ensureOpen(HttpURLConnection.java:3366)     在   sun.net.www.protocol.http.HttpURLConnection $ HttpInputStream.read(HttpURLConnection.java:3391)     在   com.caucho.hessian.io.Hessian2Input.readBuffer(Hessian2Input.java:2844)     在com.caucho.hessian.io.Hessian2Input.read(Hessian2Input.java:2790)     在   com.caucho.hessian.io.Hessian2Input $ ReadInputStream.read(Hessian2Input.java:2984)     在java.io.InputStream.read(InputStream.java:101)at   com.google.common.io.ByteStreams.copy(ByteStreams.java:70)at   com.google.common.io.ByteStreams.toByteArray(ByteStreams.java:115)at at   w.big.data.hessian.Client.main(Client.java:20)抑制:   java.io.IOException:stream关闭了   sun.net.www.protocol.http.HttpURLConnection $ HttpInputStream.ensureOpen(HttpURLConnection.java:3366)         在   sun.net.www.protocol.http.HttpURLConnection $ HttpInputStream.read(HttpURLConnection.java:3391)         在   com.caucho.hessian.io.Hessian2Input.readBuffer(Hessian2Input.java:2844)         在com.caucho.hessian.io.Hessian2Input.read(Hessian2Input.java:2816)         在   com.caucho.hessian.io.Hessian2Input.parseByte(Hessian2Input.java:2699)         在   com.caucho.hessian.io.Hessian2Input.access $ 000(Hessian2Input.java:74)         在   com.caucho.hessian.io.Hessian2Input $ ReadInputStream.read(Hessian2Input.java:2971)         在   com.caucho.hessian.io.Hessian2Input $ ReadInputStream.close(Hessian2Input.java:2994)         在w.big.data.hessian.Client.main(Client.java:22)

0 个答案:

没有答案