我有一个方法可以打开一个连接,查询一个站点,获取页面数,然后使用 NIO 来同时检索所有页面。第一个查询是使用完成的,URLConnection
并且工作得很好。当我尝试使用 NIO 选择器和通道时,我遇到了 2 个问题:
1)如果我不从迭代器中删除键,则在无限循环中运行打印size()
并发送查询。如果我尝试删除密钥,我会收到 UnsupportedOperationsException。呸!
2) 写入套接字后是否需要从 OP_WRITE 注销通道?如果是这样,我可以打电话channel.register(selector, SelectionKey.OP_READ)
来消除对写作的兴趣吗?
public void test() throws IOException {
// create selector
Selector selector = Selector.open();
System.out.println("opened");
// get the number of pages
URL itemUrl = new URL(ITEM_URL);
URLConnection conn = itemUrl.openConnection();
conn.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
// out.write(getHeaderString(itemUrl));
out.write(new Query("", "Internal Hard Drives", false, false, true, false, -1, 7603, 1, 14, -1, "", "PRICE", 1).toString());
out.close();
JsonReader in = new JsonReader(new InputStreamReader(conn.getInputStream()));
JsonParser parser = new JsonParser();
JsonObject tempObj = (JsonObject) parser.parse(in);
Pages.setNumOfPages(getNumberOfIterations(tempObj.get("PaginationInfo")));
System.out.println("Pages: " + Pages.getNumOfPages());
// for each page, create a channel, attach to selector with interest in read
// typically this would be i <= Pages.getNumberOfPages but to troubleshoot, i'm limiting this to just once.
for (int i = 1; i <= 1; i++) {
SocketChannel channel = SocketChannel.open();
channel.configureBlocking(false);
channel.connect(new InetSocketAddress(itemUrl.getHost(), 80));
channel.register(selector, SelectionKey.OP_WRITE | SelectionKey.OP_READ);
}
selector.select();
Set<SelectionKey> sk = selector.keys();
while (!sk.isEmpty()) {
System.out.println(sk.size());
Iterator<SelectionKey> iterator = sk.iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
iterator.remove();
if (key.isReadable()) {
SocketChannel channel = (SocketChannel) key.channel();
ByteBuffer buf = ByteBuffer.allocate(8192);
channel.read(buf);
buf.flip();
Product p = parse(buf, Product.class);
if (p != null) {
finalItems.add(p);
System.out.println("Item added!");
key.cancel();
}
} else if (key.isWritable()) {
SocketChannel channel = (SocketChannel) key.channel();
System.out.println(itemUrl);
System.out.println(new Query("", "Internal Hard Drives", false, false, true,
false, -1, 7603, 1, 14, -1, "", "PRICE", 1).toString());
channel.write(ByteBuffer.wrap(new Query("", "Internal Hard Drives", false,
false, true, false, -1, 7603, 1, 14, -1, "", "PRICE", 1).toString()
.getBytes()));
}
}
selector.select();
sk = selector.keys();
}
}