2017-02-14 28 views
0

我目前正在嘗試將MSF4J與StreamingOutput API結合使用。然而,我不想流式傳輸一個文件,而是想傳輸一系列無休止的短字符串/文本。我想立即將字符串刷新到客戶端。但是,客戶端在刷新後沒有得到它。我相信這是由於默認的8kb緩衝區,因爲我的字符串會在一段時間後被刷新。我如何覆蓋這個默認緩衝區,就像在glassfish中完成的一樣? https://jersey.java.net/apidocs/2.22/jersey/org/glassfish/jersey/server/ServerProperties.html#OUTBOUND_CONTENT_LENGTH_BUFFERwso2 msf4j:如何配置服務器屬性

我想是這樣......

Properties properties = new Properties() 
properties.set("jersey.config.server.contentLength.buffer", 0);** 

new MicroservicesRunner() 
    .setProperties(properties)** 
    .addInterceptor(new HTTPMonitoringInterceptor()) 
    .deploy(new MyService()) 
    .start(); 

我streamingout類

new StreamingOutput(){  
    public void write(OutputStream os) throws IOException, WebApplicationException  { 
     while(true){ 
      os.write("some string".getBytes()); 
      os.flush(); 
     }  
     } 
} 

謝謝。

+0

因此,你的字符串立即得到flused,但不是int值是這種情況? –

+0

@ThatithaThilinaDayaratne,整數也被視爲字符串,因爲它們通過REST返回。即使使用刷新,客戶端也不會立即收到字符串,直到8kb緩衝區被消耗完。 – Speng

+0

你能分享全套服務嗎? –

回答

1

這是整個代碼。 它的工作原理,但我想我可以覆蓋服務器屬性的方式。

例如properties.set(「jersey.config.server.contentLength.buffer」,0); 謝謝。

package ph.sample.api.ms;

import org.wso2.msf4j.MicroservicesRunner; import org.wso2.msf4j.analytics.httpmonitoring.HTTPMonitoringInterceptor;

公共類的應用{ 公共靜態無效的主要(字串[] args){

new MicroservicesRunner(9000) 
      .addInterceptor(new HTTPMonitoringInterceptor()) 
      .deploy(new StreamService()) 
      .start(); 
} } 

包ph.sample.api.ms;

import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.core.Response;

import org.wso2.carbon.metrics.core.annotation.Timed; import java.io.IOException; import java.io.OutputStream;

import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.StreamingOutput;

@Path( 「/流」) 公共類StreamService { @GET @Path( 「/訂閱」) @Timed 公共響應訂閱(){

return Response.ok(new StreamingOutput(){ 
     @Override   
     public void write(OutputStream os) throws IOException, WebApplicationException { 
     while(true){ 
      os.write("some string".getBytes()); 
      os.flush(); 
     }    
     } 
    }).build(); 
} 

}

+0

用這個更新你的問題。不要發佈爲答案 –

+0

您可以根據您的要求更改緩衝區大小./請參閱示例https://github.com/wso2/msf4j/blob/master/samples/fileserver/src/main/java/org/ WSO2/msf4j /示例/ FileServer.java#L110 –

相關問題