Java 如何将数据注入akka队列
我正在尝试使用akka队列来处理来自TCP连接的数据。 起初,我把连接作为steam的一部分,但它没有给我控制和反馈的灵活性 然而,处理效果很好,但我无法让队列工作,即使有数据,似乎也没有处理(也尝试了BlockingQueue,可以看到队列填充,但没有处理)。它作为流的一部分工作,因此代码看起来还可以,我希望我需要以不同的方式执行它 下面是我的代码:Java 如何将数据注入akka队列,java,queue,akka,akka-stream,Java,Queue,Akka,Akka Stream,我正在尝试使用akka队列来处理来自TCP连接的数据。 起初,我把连接作为steam的一部分,但它没有给我控制和反馈的灵活性 然而,处理效果很好,但我无法让队列工作,即使有数据,似乎也没有处理(也尝试了BlockingQueue,可以看到队列填充,但没有处理)。它作为流的一部分工作,因此代码看起来还可以,我希望我需要以不同的方式执行它 下面是我的代码: private static class Qu16ConnectionClient extends AbstractActor {
private static class Qu16ConnectionClient extends AbstractActor {
final InetSocketAddress remote;
final ActorRef listener;
final SourceQueueWithComplete<ByteBuffer> sourceQueue;
private final BlockingQueue<String> nextCmd = new LinkedBlockingQueue<>();
public static Props props(InetSocketAddress remote, ActorRef listener) {
return Props.create(Qu16ConnectionClient.class, remote, listener);
}
public Qu16ConnectionClient(InetSocketAddress remote, ActorRef listener) {
this.remote = remote;
this.listener = listener;
final ActorRef tcp = Tcp.get(getContext().getSystem()).manager();
tcp.tell(TcpMessage.connect(remote), getSelf());
int bufferSize = 10;
sourceQueue = Source.<ByteBuffer>queue(bufferSize, OverflowStrategy.dropTail())
.map(input -> Hex.encodeHexString(input.array()))
.to(Sink.foreach(this::startProcessing))
.run(getContext().getSystem());
}
@Override
public Receive createReceive() {
return receiveBuilder()
.match(String.class, message -> {
Logger.debug("Incoming cmd: " + message);
nextCmd.put(message);
})
.match(
Tcp.CommandFailed.class,
msg -> {
listener.tell("failed", getSelf());
getContext().stop(getSelf());
})
.match(
Tcp.Connected.class,
msg -> {
listener.tell(msg, getSelf());
getSender().tell(TcpMessage.register(getSelf()), getSelf());
getContext().become(connected(getSender()));
})
.build();
}
private Receive connected(final ActorRef connection) {
return receiveBuilder()
.match(
ByteString.class,
msg -> {
Logger.debug("Received ByteString:" + Hex.encodeHexString(msg.toArray()));
connection.tell(TcpMessage.write((ByteString) msg), getSelf());
})
.match(
Tcp.CommandFailed.class,
msg -> {
Logger.warn("Command failed oon TCP");
})
.match(
Tcp.Received.class,
msg -> {
sourceQueue.offer(msg.data().asByteBuffer());
String hexCmd;
if (!nextCmd.isEmpty()) {
hexCmd = nextCmd.take();
} else {
hexCmd = "fe";
}
connection.tell(TcpMessage.write(ByteString.fromArray(Hex.decodeHex(hexCmd))), getSelf());
})
.matchEquals(
"close",
msg -> {
Logger.debug("Received close");
connection.tell(TcpMessage.close(), getSelf());
})
.match(
Tcp.ConnectionClosed.class,
msg -> {
Logger.debug("TCP Connection closed.");
getContext().stop(getSelf());
})
.build();
}
private void startProcessing(String fullResult) {
try {
Logger.debug("Receiving data: " + fullResult);
//Do the processing
} catch (Exception e) {
Logger.warn("Problem processing result: " + e.getMessage(), e);
}
}
}
私有静态类Qu16ConnectionClient扩展了AbstractActor{
最终InetSocketAddress远程;
最终ActorRef监听器;
最终SourceQueueWithComplete sourceQueue;
private final BlockingQueue nextCmd=new LinkedBlockingQueue();
公共静态道具道具(InetSocketAddress远程、ActorRef侦听器){
返回Props.create(Qu16ConnectionClient.class、remote、listener);
}
公用QU16连接客户端(InetSocketAddress远程,ActorRef侦听器){
this.remote=远程;
this.listener=listener;
final ActorRef tcp=tcp.get(getContext().getSystem()).manager();
tell(TcpMessage.connect(remote),getSelf());
int bufferSize=10;
sourceQueue=Source.queue(bufferSize,OverflowStrategy.dropTail())
.map(输入->十六进制.encodeHexString(输入.array()))
.to(Sink.foreach(this::startProcessing))
.run(getContext().getSystem());
}
@凌驾
公共接收createReceive(){
return receiveBuilder()
.match(String.class,消息->{
Logger.debug(“传入cmd:+消息”);
nextCmd.put(消息);
})
.比赛(
Tcp.CommandFailed.class,
味精->{
tell(“失败”,getSelf());
getContext().stop(getSelf());
})
.比赛(
Tcp.Connected.class,
味精->{
tell(msg,getSelf());
getSender().tell(TcpMessage.register(getSelf()),getSelf());
getContext().Been(已连接(getSender());
})
.build();
}
已连接专用接收(最终ActorRef连接){
return receiveBuilder()
.比赛(
ByteString.class,
味精->{
debug(“由testring接收:+Hex.encodeHexString(msg.toArray()));
tell(TcpMessage.write((ByteString)msg),getSelf();
})
.比赛(
Tcp.CommandFailed.class,
味精->{
Logger.warn(“命令失败”);
})
.比赛(
Tcp.Received.class,
味精->{
offer(msg.data().asByteBuffer());
字符串hexCmd;
如果(!nextCmd.isEmpty()){
hexCmd=nextCmd.take();
}否则{
hexCmd=“fe”;
}
tell(TcpMessage.write(ByteString.fromArray(Hex.decodehx(hexCmd))),getSelf();
})
.matchEquals(
“结束”,
味精->{
Logger.debug(“接收关闭”);
tell(TcpMessage.close(),getSelf());
})
.比赛(
Tcp.ConnectionClosed.class,
味精->{
调试(“TCP连接关闭”);
getContext().stop(getSelf());
})
.build();
}
私有void启动处理(字符串fullResult){
试一试{
Logger.debug(“接收数据:+fullResult”);
//处理
}捕获(例外e){
Logger.warn(“问题处理结果:+e.getMessage(),e”);
}
}
}