[英]Akka stream of objects over http
I have got a piece of code (see bellow) which spawns a server that echoes every stream of ByteString it receives from port 6001. The example also defines a client that connects to the server and sends a stream of ByteString containing a list of characters from letter 'a' to 'z'.我有一段代码(见下文),它生成一个服务器,该服务器响应它从端口 6001 接收的每个 ByteString 流。该示例还定义了一个客户端,该客户端连接到服务器并发送一个包含来自以下位置的字符列表的 ByteString 流字母“a”到“z”。
My question at this point is, does akka offer a way to send and receive a Stream of objects instead of ByStreams over http?我现在的问题是, akka 是否提供了一种通过 http 发送和接收对象流而不是 ByStreams 的方法? For instance, objects of class Client.
例如,类 Client 的对象。
If so, how could I send and receive such a stream of objects?如果是这样,我怎么能发送和接收这样的对象流? Could you provide me a snippet that shows how to carry it out?
你能给我一个片段来展示如何执行它吗?
Akka documentation is not user-friendly for non-toy examples... Akka 文档对于非玩具示例不是用户友好的......
Thanks for your help谢谢你的帮助
public class TcpEcho {公共类 TcpEcho {
/**
* Use without parameters to start both client and server.
*
* Use parameters `server 0.0.0.0 6001` to start server listening on port
* 6001.
*
* Use parameters `client 127.0.0.1 6001` to start client connecting to
* server on 127.0.0.1:6001.
*
*/
public static void main(String[] args) throws IOException {
if (args.length == 0) {
ActorSystem system = ActorSystem.create("ClientAndServer");
InetSocketAddress serverAddress = new InetSocketAddress("127.0.0.1", 6000);
server(system, serverAddress);
client(system, serverAddress);
} else {
InetSocketAddress serverAddress;
if (args.length == 3) {
serverAddress = new InetSocketAddress(args[1], Integer.valueOf(args[2]));
} else {
serverAddress = new InetSocketAddress("127.0.0.1", 6000);
}
if (args[0].equals("server")) {
ActorSystem system = ActorSystem.create("Server");
server(system, serverAddress);
} else if (args[0].equals("client")) {
ActorSystem system = ActorSystem.create("Client");
client(system, serverAddress);
}
}
}
public static void server(ActorSystem system, InetSocketAddress serverAddress) {
final ActorMaterializer materializer = ActorMaterializer.create(system);
final Sink<IncomingConnection, CompletionStage<Done>> handler = Sink.foreach(conn -> {
System.out.println("Client connected from: " + conn.remoteAddress());
conn.handleWith(Flow.<ByteString> create(), materializer);
});
final CompletionStage<ServerBinding> bindingFuture = Tcp.get(system)
.bind(serverAddress.getHostString(), serverAddress.getPort()).to(handler).run(materializer);
bindingFuture.whenComplete((binding, throwable) -> {
System.out.println("Server started, listening on: " + binding.localAddress());
});
bindingFuture.exceptionally(e -> {
System.err.println("Server could not bind to " + serverAddress + " : " + e.getMessage());
system.terminate();
return null;
});
}
public static void client(ActorSystem system, InetSocketAddress serverAddress) {
final ActorMaterializer materializer = ActorMaterializer.create(system);
final List<ByteString> testInput = new ArrayList<>();
for (char c = 'a'; c <= 'z'; c++) {
testInput.add(ByteString.fromString(String.valueOf(c)));
}
Source<ByteString, NotUsed> responseStream = Source.from(testInput)
.via(Tcp.get(system).outgoingConnection(serverAddress.getHostString(), serverAddress.getPort()));
CompletionStage<ByteString> result = responseStream.runFold(ByteString.empty(), (acc, in) -> acc.concat(in),
materializer);
result.whenComplete((success, failure) -> {
if (failure != null) {
System.err.println("Failure: " + failure.getMessage());
} else {
System.out.println("Result: " + success.utf8String());
}
System.out.println("Shutting down client");
system.terminate();
});
}
} }
akka.stream.{javadsl,scaladsl}.Framing
contains utilities to help you build consistent messages. akka.stream.{javadsl,scaladsl}.Framing
包含可帮助您构建一致消息的实用程序。 For example, you can send your messages through Framing.simpleFramingProtocolEncoder(maxLength)
to automatically add length information to them.例如,您可以通过
Framing.simpleFramingProtocolEncoder(maxLength)
发送消息以自动向它们添加长度信息。 On the other end, Framing.simpleFramingProtocolDecoder(maxLength)
will take care of decoding the message according to its enclosed length information.在另一端,
Framing.simpleFramingProtocolDecoder(maxLength)
将负责根据其包含的长度信息对消息进行解码。
If you want to manipulate plain objects, you just have to serialize them into a ByteString
before sending them through the encoder, and deserialize them from the ByteString
after receiving their representation from the decoder.如果您想操作普通对象,您只需在通过编码器发送它们之前将它们序列化为
ByteString
,并在从解码器接收到它们的表示后从ByteString
反序列化它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.