gpt4 book ai didi

java - 使用 Netty 的异步 HTTP 客户端

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:01:22 26 4
gpt4 key购买 nike

我是 netty 的新手,仍在努力寻找自己的出路。我正在寻找创建一个异步工作的 http 客户端。http的netty例子只展示了如何等待IO操作,没有展示如何使用addListener,所以这几天一直在想办法。

我正在尝试创建一个请求类来处理请求的所有不同状态,从连接、发送数据、处理响应到关闭连接。为此,我的类扩展了 SimpleChannelUpstreamHandler 并实现了 ChannelFutureListener。我使用 ChannelPipelineFactory 将类(作为 SimpleChannelUpstreamHandler)的(this)实例作为处理程序添加到管道中。

连接是这样创建的:

this.state = State.Connecting;
this.clientBootstrap.connect(this.address).addListener(this);

然后是operationComplete方法:

@Override
public void operationComplete(ChannelFuture future) throws Exception {
State oldState = this.state;

if (!future.isSuccess()) {
this.status = Status.Failed;
future.getChannel().disconnect().addListener(this);
}
else if (future.isCancelled()) {
this.status = Status.Canceled;
future.getChannel().disconnect().addListener(this);
}
else switch (this.state) {
case Connecting:
this.state = State.Sending;
Channel channel = future.getChannel();
channel.write(this.createRequest()).addListener(this);
break;

case Sending:
this.state = State.Disconnecting;
future.getChannel().disconnect().addListener(this);
break;

case Disconnecting:
this.state = State.Closing;
future.getChannel().close().addListener(this);
break;

case Closing:
this.state = State.Finished;
break;
}
System.out.println("request operationComplete start state: " + oldState + ", end state: " + this.state + ", status: " + this.status);
}

private HttpRequest createRequest() {
String url = this.url.toString();

HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, url);
request.setHeader(HttpHeaders.Names.HOST, this.url.getHost());
request.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
request.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);

return request;
}

该类还覆盖了messageReceived 方法:

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
System.out.println("messageReceived");
HttpResponse response = (HttpResponse) e.getMessage();

ChannelBuffer content = response.getContent();
if (content.readable()) {
System.out.println("CONTENT: " + content.toString(CharsetUtil.UTF_8));
}
}

问题是我得到了这个输出:

request operationComplete start state: Connecting, end state: Sending, status: Unknown
request operationComplete start state: Sending, end state: Disconnecting, status: Unknown
request operationComplete start state: Closing, end state: Finished, status: Unknown
request operationComplete start state: Disconnecting, end state: Finished, status: Unknown

如您所见,messageReceived 由于某种原因未被执行,即使管道工厂将此类的实例添加到管道。

知道我在这里遗漏了什么吗?谢谢。


编辑

感谢@JestanNirojan 的帮助,我终于成功了,以防有人对解决方案感兴趣:

public class ClientRequest extends SimpleChannelUpstreamHandler {

....

public void connect() {
this.state = State.Connecting;
System.out.println(this.state);
this.clientBootstrap.connect(this.address);
}

@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
this.state = State.Sending;
System.out.println(this.state);
ctx.getChannel().write(this.createRequest());
}

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
HttpResponse response = (HttpResponse) e.getMessage();

ChannelBuffer content = response.getContent();
if (content.readable()) {
System.out.println("CONTENT: " + content.toString(CharsetUtil.UTF_8));
}

this.state = State.Disconnecting;
System.out.println(this.state);
}

@Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
this.state = State.Closing;
System.out.println(this.state);
}

@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
this.state = State.Finished;
System.out.println(this.state);
}

private HttpRequest createRequest() {
String url = this.url.toString();

HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, url);
request.setHeader(HttpHeaders.Names.HOST, this.url.getHost());
request.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
request.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);

return request;
}
}

最佳答案

您正在使用 ChannelFutureListener 来执行 channel 中的所有操作(这很糟糕), future 的监听器将在调用这些 channel 操作后立即执行。

问题是,发送消息后,channel 立即断开连接,handler 收不到后面的响应消息。

        ........
case Sending:
this.state = State.Disconnecting;
future.getChannel().disconnect().addListener(this);
break;
........

你根本不应该阻止 channel future 的话题。最好的方法是扩展 SimpleChannelUpstreamHandler 的

    channelConnected(..) {} 
messageReceived(..) {}
channelDisconnected(..) {}

方法并对这些事件使用react。您也可以将状态保留在该处理程序中。

关于java - 使用 Netty 的异步 HTTP 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9745727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com