简体   繁体   中英

How to keep the client and server connection alive in webservice for StreamingOutput

My webservice code

final StreamingOutput stream = new StreamingOutput() {
  @Override
  public void write(final OutputStream out) {
    dao.getData(
        query,
        new SdmxObserver(writerFactory.getDataWriter(sdmxFormat, out, properties), request
            .getRemoteAddr(), request.getHeader("User-Agent"), query.toString(), sdmxFormat
            .toString(), query.getlist()));
  }
};
res = Response.ok(stream, MediaType.valueOf("application/vnd.sdmx.genericdata+xml;version=2.1"))
                .cacheControl(cc).lastModified(lastModified).header("Vary", "Accept,Accept-Encoding").build();
return res;

The database call to retrive the data is taking long, so when we use the proxy it has time out of 2 minutes and hence as the data is not coming from data base withn 2 minutes it is showing connection time out. Though we have use StreamingOutput then why the streaming is not happening to keep the connection alive? Also if we byepass the proxy then we can get the data sucessfully from the webservice.

But we can't change the proxy setting and also we need to use the application with or without using the proxy.

Chances are your proxy server simply kills the connection after its configured timeout, whether or not you're transferring data. If you were wondering, Connection: keep-alive header only indicates that the same connection can be reused for multiple request/responses ( RFC7230 ), it is not supposed to control timeout for a single connection.

Try talking to your proxy administrator about:

  • setting headers the proxy understands (perhaps Keep-alive: timeout=10000 , but could be also something completely different, or may not be possible at all)
  • increasing proxy timeout configuration

If that doesn't work out you may need to rethink your design and take a different approach, for example Spring Data + pageable resources or, if you feel brave, asynchronous processing with Spring Integration + Splitter pattern .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM