pom.xml 內容如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
? <modelVersion>4.0.0</modelVersion>
? <groupId>com.packt.samples</groupId>
? <artifactId>namenode</artifactId>
? <packaging>jar</packaging>
? <version>1.0.0</version>
? <name>parquet-writer</name>
? <url>http://maven.apache.org</url>
? <dependencies>
? ? <dependency>
? ? ? <groupId>junit</groupId>
? ? ? <artifactId>junit</artifactId>
? ? ? <version>3.8.1</version>
? ? ? <scope>test</scope>
? ? </dependency>
? </dependencies>
<build>
? <plugins>
? ? <plugin>
? ? ? <groupId>org.apache.maven.plugins</groupId>
? ? ? <artifactId>maven-assembly-plugin</artifactId>
? ? ? <version>2.4</version>
? ? ? <configuration>
? ? ? ? <descriptorRefs>
? ? ? ? ? <descriptorRef>jar-with-dependencies</descriptorRef>
? ? ? ? </descriptorRefs>
? ? ? ? <archive>
? ? ? ? ? <manifest>
? ? ? ? ? ? <addClasspath>true</addClasspath>
? ? ? ? ? ? <mainClass>com.packt.samples.NameNode</mainClass>
? ? ? ? ? </manifest>
? ? ? ? </archive>
? ? ? </configuration>
? ? ? <executions>
? ? ? ? <execution>
? ? ? ? ? <id>assemble-all</id>
? ? ? ? ? <phase>package</phase>
? ? ? ? ? <goals>
? ? ? ? ? ? <goal>single</goal>
? ? ? ? ? </goals>
? ? ? ? </execution>
? ? ? </executions>
? ? </plugin>
? </plugins>
</build>
</project>
代碼如下
cat src/main/java/com/packt/samples/Server.java
package com.packt.samples;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.Selector;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.Iterator;
import java.net.SocketAddress;
import java.net.InetSocketAddress;
import java.io.IOException;
public class Server {
private Listener listener;
Server() throws IOException {
this.listener = new Listener();
}
public void start() {
listener.start();
}
public void join() throws InterruptedException {
listener.join();
}
private class Listener extends Thread {
private ServerSocketChannel acceptChannel = null;
private Selector selector = null;
private InetSocketAddress address;
private int currentReader;
private int port;
public Listener() throws IOException {
currentReader = 0;
address = new InetSocketAddress("0.0.0.0", 8020);
acceptChannel = ServerSocketChannel.open();
acceptChannel.configureBlocking(false);
port = acceptChannel.socket().getLocalPort();
selector= Selector.open();
acceptChannel.socket().bind(address, 100);
acceptChannel.register(selector, SelectionKey.OP_ACCEPT);
}
@Override
public void run() {
while (true) {
SelectionKey key = null;
try {
getSelector().select();
Iterator<SelectionKey> iter = getSelector().selectedKeys().iterator();
while (iter.hasNext()) {
try {
key = iter.next();
if (key.isValid()) {
if (key.isAcceptable()) {
ServerSocketChannel server = (ServerSocketChannel) key.channel();
System.out.println("accept a new channel " + server.accept().getRemoteAddress());
//SocketChannel channel;
//while ((channel = server.accept()) != null) {
// System.out.println("accept a new channel ");
//}
}
}
} catch (IOException e) {
}
key = null;
}
} catch (Exception e) {
}
}
}
synchronized Selector getSelector() {
return selector;
}
}
}
------------------------------
cat src/main/java/com/packt/samples/NameNode.java
package com.packt.samples;
/**
* Hello world!
*
*/
public class NameNode
{
public static void main( String[] args ) throws java.io.IOException,InterruptedException {
Server server = new Server();
server.start();
server.join();
}
}
-----------------------
編譯
mvn clean package -DskipTests
-----------------------
執(zhí)行
java -jar target/namenode-1.0.0-jar-with-dependencies.jar
----------------------
另外開3個窗口,執(zhí)行以下命令
[root@localhost ~]# telnet 192.168.1.15 8020
Trying 192.168.1.15...
Connected to 192.168.1.15.
Escape character is '^]'.
程序打印如下內容
accept a new channel /192.168.1.15:45798
accept a new channel /192.168.1.15:45800
accept a new channel /192.168.1.15:45802