Stream 如何从未来的onComplete函数。;
我的问题是,获取的实体的数据字节不是ByteString类型,而是Source(SourceShape(StreamUtils$$anon$4.out(2020243329)),它很大(239807字节)。仅使用onComplete函数获取完整的ByteString。但是我需要处理接收到的数据并返回处理后的结果作为响应。我使用wait.ready(s2,Duration.Inf)函数等待s2的完成,但整个程序挂起 akka.0版本是2.6.10 可以从GitHub获得 ---------------服务器端---------------------------------------Stream 如何从未来的onComplete函数。;,stream,akka,Stream,Akka,我的问题是,获取的实体的数据字节不是ByteString类型,而是Source(SourceShape(StreamUtils$$anon$4.out(2020243329)),它很大(239807字节)。仅使用onComplete函数获取完整的ByteString。但是我需要处理接收到的数据并返回处理后的结果作为响应。我使用wait.ready(s2,Duration.Inf)函数等待s2的完成,但整个程序挂起 akka.0版本是2.6.10 可以从GitHub获得 -------------
object HTTPService extends DefaultJsonProtocol with Directives with SprayJsonSupport{
implicit val config = ConfigFactory.load()
implicit val system = ActorSystem("PiFlowHTTPService", config)
implicit val materializer = ActorMaterializer()
implicit val executionContext = system.dispatcher
def route(req: HttpRequest): Future[HttpResponse] = req match {
case HttpRequest(POST, Uri.Path("/test"), headers, entity, protocol) =>{
val s1 = entity.dataBytes
val s2 = s1.runReduce((u1, u2) => u1.++(u2))
s2.onComplete {
case scala.util.Success(json) => {
println(json)
println(json.utf8String)
//result = Future.successful(HttpResponse(500, entity="Fail!"))
}
case scala.util.Failure(ex) => {
println(ex)
//result = Future.successful(HttpResponse(500, entity="Fail!"))
}
case _ => {
println("Exception!!!!!!!!!!!!!!!!")
//result = Future.successful(HttpResponse(500, entity="Fail!"))
}
}
Future.successful(HttpResponse(500, entity="OK!"))
}
}
def run = {
val ip = InetAddress.getLocalHost.getHostAddress
println(ip + ":8001 started!!!!")
Http().bindAndHandleAsync(route, ip, 8001)
}
}
object HttpClient {
def main(args: Array[String]): Unit=
{
val url = "http://10.31.0.27:8001/test"
val timeout = 18000
val requestConfig = RequestConfig.custom()
.setConnectTimeout(timeout * 1000)
.setConnectionRequestTimeout(timeout * 1000)
.setSocketTimeout(timeout * 1000).build()
import scala.io.Source
val source = Source.fromFile("E:\\project\\sparktest\\src\\main\\resource\\group.json","UTF-8")
val json = source.getLines().toArray.mkString("\n")
val client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build()
val post: HttpPost = new HttpPost(url)
post.addHeader("Content-Type", "application/json")
post.setEntity(new StringEntity(json))
val response: CloseableHttpResponse = client.execute(post)
val entity = response.getEntity
val str = EntityUtils.toString(entity, "UTF-8")
println ("Code is " + str)
}
}
----------------客户端----------------------------------------
object HTTPService extends DefaultJsonProtocol with Directives with SprayJsonSupport{
implicit val config = ConfigFactory.load()
implicit val system = ActorSystem("PiFlowHTTPService", config)
implicit val materializer = ActorMaterializer()
implicit val executionContext = system.dispatcher
def route(req: HttpRequest): Future[HttpResponse] = req match {
case HttpRequest(POST, Uri.Path("/test"), headers, entity, protocol) =>{
val s1 = entity.dataBytes
val s2 = s1.runReduce((u1, u2) => u1.++(u2))
s2.onComplete {
case scala.util.Success(json) => {
println(json)
println(json.utf8String)
//result = Future.successful(HttpResponse(500, entity="Fail!"))
}
case scala.util.Failure(ex) => {
println(ex)
//result = Future.successful(HttpResponse(500, entity="Fail!"))
}
case _ => {
println("Exception!!!!!!!!!!!!!!!!")
//result = Future.successful(HttpResponse(500, entity="Fail!"))
}
}
Future.successful(HttpResponse(500, entity="OK!"))
}
}
def run = {
val ip = InetAddress.getLocalHost.getHostAddress
println(ip + ":8001 started!!!!")
Http().bindAndHandleAsync(route, ip, 8001)
}
}
object HttpClient {
def main(args: Array[String]): Unit=
{
val url = "http://10.31.0.27:8001/test"
val timeout = 18000
val requestConfig = RequestConfig.custom()
.setConnectTimeout(timeout * 1000)
.setConnectionRequestTimeout(timeout * 1000)
.setSocketTimeout(timeout * 1000).build()
import scala.io.Source
val source = Source.fromFile("E:\\project\\sparktest\\src\\main\\resource\\group.json","UTF-8")
val json = source.getLines().toArray.mkString("\n")
val client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build()
val post: HttpPost = new HttpPost(url)
post.addHeader("Content-Type", "application/json")
post.setEntity(new StringEntity(json))
val response: CloseableHttpResponse = client.execute(post)
val entity = response.getEntity
val str = EntityUtils.toString(entity, "UTF-8")
println ("Code is " + str)
}
}