forked from llohellohe/zookeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
增加ZooKeeper源代码解读之ClientCnxnSocketNIO的链接
- Loading branch information
1 parent
c9ff81b
commit 7eb59af
Showing
12 changed files
with
268 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package yangqi.nio; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.net.SocketAddress; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.ClosedChannelException; | ||
import java.nio.channels.SelectionKey; | ||
import java.nio.channels.Selector; | ||
import java.nio.channels.SocketChannel; | ||
|
||
/** | ||
* Created by yangqi on 2/1/14. | ||
*/ | ||
public class ZkNIOClient { | ||
private Selector selector = null; | ||
|
||
private SelectionKey selectionKey = null; | ||
|
||
private SocketChannel socketChannel; | ||
|
||
public static void main(String[] args) { | ||
ZkNIOClient client = new ZkNIOClient(); | ||
|
||
int port=2181; | ||
|
||
String host="127.0.0.1"; | ||
|
||
System.out.println("start to connect to "+host+" :" + port); | ||
|
||
InetSocketAddress address=new InetSocketAddress(host,port); | ||
|
||
boolean connected = client.connect(address); | ||
|
||
System.out.println("start to connect to "+host+" :" + port+",status "+connected); | ||
|
||
int i=0; | ||
while(i++<3){ | ||
client.listen(); | ||
} | ||
} | ||
|
||
private boolean connect(SocketAddress socketAddress) { | ||
socketChannel = createSocket(); | ||
|
||
try { | ||
selectionKey = socketChannel.register(selector, SelectionKey.OP_CONNECT); | ||
return socketChannel.connect(socketAddress); | ||
} catch (ClosedChannelException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return false; | ||
|
||
} | ||
|
||
private SocketChannel createSocket() { | ||
try { | ||
selector = Selector.open(); | ||
|
||
SocketChannel socketChannel = SocketChannel.open(); | ||
|
||
socketChannel.socket().setTcpNoDelay(false); | ||
socketChannel.socket().setSoLinger(false, -1); | ||
socketChannel.configureBlocking(false); | ||
|
||
return socketChannel; | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private void listen(){ | ||
try { | ||
int keys= selector.select(); | ||
|
||
for (SelectionKey key:selector.selectedKeys()){ | ||
System.out.println("Selection key is "+key); | ||
|
||
if(key.isConnectable()){ | ||
System.out.println("Connecting "); | ||
|
||
SocketChannel channel = (SocketChannel) key.channel(); | ||
|
||
if(channel.isConnectionPending()){ | ||
channel.finishConnect(); | ||
} | ||
|
||
channel.write(ByteBuffer.wrap(new String("stat").getBytes())); | ||
|
||
channel.register(selector,SelectionKey.OP_READ); | ||
}else if(key.isReadable()){ | ||
System.out.println("Reading data "); | ||
|
||
SocketChannel channel=(SocketChannel)key.channel(); | ||
ByteBuffer buffer=ByteBuffer.allocate(1024); | ||
channel.read(buffer); | ||
|
||
System.out.println("read zk server stat "+new String(buffer.array())); | ||
|
||
} | ||
} | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="EclipseModuleManager"> | ||
<libelement value="jar://$USER_HOME$/opensource/zookeeper-3.4.5/zookeeper-3.4.5.jar!/" /> | ||
<libelement value="jar://$USER_HOME$/opensource/zookeeper-3.4.5/lib/jline-0.9.94.jar!/" /> | ||
<libelement value="jar://$USER_HOME$/opensource/zookeeper-3.4.5/lib/log4j-1.2.15.jar!/" /> | ||
<libelement value="jar://$USER_HOME$/opensource/zookeeper-3.4.5/lib/netty-3.2.2.Final.jar!/" /> | ||
<libelement value="jar://$USER_HOME$/opensource/zookeeper-3.4.5/lib/slf4j-api-1.6.1.jar!/" /> | ||
<libelement value="jar://$USER_HOME$/opensource/zookeeper-3.4.5/lib/slf4j-log4j12-1.6.1.jar!/" /> | ||
<varelement var="file://$M2_REPO$/javax/mail/mail/1.4/mail-1.4.jar" value="M2_REPO/javax/mail/mail/1.4/mail-1.4.jar" /> | ||
<varelement var="file://$M2_REPO$/javax/mail/mail/1.4/mail-1.4-sources.jar" value="src:M2_REPO/javax/mail/mail/1.4/mail-1.4-sources.jar" /> | ||
<varelement var="file://$M2_REPO$/javax/activation/activation/1.1/activation-1.1.jar" value="M2_REPO/javax/activation/activation/1.1/activation-1.1.jar" /> | ||
<varelement var="file://$M2_REPO$/javax/activation/activation/1.1/activation-1.1-sources.jar" value="src:M2_REPO/javax/activation/activation/1.1/activation-1.1-sources.jar" /> | ||
<varelement var="file://$M2_REPO$/junit/junit/3.8.1/junit-3.8.1.jar" value="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar" /> | ||
<varelement var="file://$M2_REPO$/junit/junit/3.8.1/junit-3.8.1-sources.jar" value="src:M2_REPO/junit/junit/3.8.1/junit-3.8.1-sources.jar" /> | ||
<varelement var="file://$M2_REPO$/jline/jline/0.9.94/jline-0.9.94.jar" value="M2_REPO/jline/jline/0.9.94/jline-0.9.94.jar" /> | ||
<varelement var="file://$M2_REPO$/jline/jline/0.9.94/jline-0.9.94-sources.jar" value="src:M2_REPO/jline/jline/0.9.94/jline-0.9.94-sources.jar" /> | ||
<src_description expected_position="0"> | ||
<src_folder value="file://$MODULE_DIR$/src/test/java" expected_position="0" /> | ||
<src_folder value="file://$MODULE_DIR$/src/main/java" expected_position="1" /> | ||
<src_folder value="file://$MODULE_DIR$/src/main/resources" expected_position="2" /> | ||
</src_description> | ||
</component> | ||
<component name="NewModuleRootManager" inherit-compiler-output="false"> | ||
<output url="file://$MODULE_DIR$/target/classes" /> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="module-library"> | ||
<library name="mail-1.4.jar"> | ||
<CLASSES> | ||
<root url="file://$M2_REPO$/javax/mail/mail/1.4/mail-1.4.jar" /> | ||
</CLASSES> | ||
<JAVADOC /> | ||
<SOURCES> | ||
<root url="file://$M2_REPO$/javax/mail/mail/1.4/mail-1.4-sources.jar" /> | ||
</SOURCES> | ||
</library> | ||
</orderEntry> | ||
<orderEntry type="module-library"> | ||
<library name="activation-1.1.jar"> | ||
<CLASSES> | ||
<root url="file://$M2_REPO$/javax/activation/activation/1.1/activation-1.1.jar" /> | ||
</CLASSES> | ||
<JAVADOC /> | ||
<SOURCES> | ||
<root url="file://$M2_REPO$/javax/activation/activation/1.1/activation-1.1-sources.jar" /> | ||
</SOURCES> | ||
</library> | ||
</orderEntry> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="module-library"> | ||
<library name="junit-3.8.1.jar"> | ||
<CLASSES> | ||
<root url="file://$M2_REPO$/junit/junit/3.8.1/junit-3.8.1.jar" /> | ||
</CLASSES> | ||
<JAVADOC /> | ||
<SOURCES> | ||
<root url="file://$M2_REPO$/junit/junit/3.8.1/junit-3.8.1-sources.jar" /> | ||
</SOURCES> | ||
</library> | ||
</orderEntry> | ||
<orderEntry type="module-library"> | ||
<library name="jline-0.9.94.jar"> | ||
<CLASSES> | ||
<root url="file://$M2_REPO$/jline/jline/0.9.94/jline-0.9.94.jar" /> | ||
</CLASSES> | ||
<JAVADOC /> | ||
<SOURCES> | ||
<root url="file://$M2_REPO$/jline/jline/0.9.94/jline-0.9.94-sources.jar" /> | ||
</SOURCES> | ||
</library> | ||
</orderEntry> | ||
<orderEntry type="module-library"> | ||
<library name="zookeeper-3.4.5.jar"> | ||
<CLASSES> | ||
<root url="jar://$USER_HOME$/opensource/zookeeper-3.4.5/zookeeper-3.4.5.jar!/" /> | ||
</CLASSES> | ||
<JAVADOC /> | ||
<SOURCES> | ||
<root url="jar://$USER_HOME$/opensource/zookeeper-3.4.5/zookeeper-3.4.5.jar!/" /> | ||
</SOURCES> | ||
</library> | ||
</orderEntry> | ||
<orderEntry type="module-library"> | ||
<library name="$USER_HOME$/opensource/zookeeper-3.4.5/lib/jline-0.9.94.jar"> | ||
<CLASSES> | ||
<root url="jar://$USER_HOME$/opensource/zookeeper-3.4.5/lib/jline-0.9.94.jar!/" /> | ||
</CLASSES> | ||
<JAVADOC /> | ||
<SOURCES /> | ||
</library> | ||
</orderEntry> | ||
<orderEntry type="module-library"> | ||
<library name="log4j-1.2.15.jar"> | ||
<CLASSES> | ||
<root url="jar://$USER_HOME$/opensource/zookeeper-3.4.5/lib/log4j-1.2.15.jar!/" /> | ||
</CLASSES> | ||
<JAVADOC /> | ||
<SOURCES /> | ||
</library> | ||
</orderEntry> | ||
<orderEntry type="module-library"> | ||
<library name="netty-3.2.2.Final.jar"> | ||
<CLASSES> | ||
<root url="jar://$USER_HOME$/opensource/zookeeper-3.4.5/lib/netty-3.2.2.Final.jar!/" /> | ||
</CLASSES> | ||
<JAVADOC /> | ||
<SOURCES /> | ||
</library> | ||
</orderEntry> | ||
<orderEntry type="module-library"> | ||
<library name="slf4j-api-1.6.1.jar"> | ||
<CLASSES> | ||
<root url="jar://$USER_HOME$/opensource/zookeeper-3.4.5/lib/slf4j-api-1.6.1.jar!/" /> | ||
</CLASSES> | ||
<JAVADOC /> | ||
<SOURCES /> | ||
</library> | ||
</orderEntry> | ||
<orderEntry type="module-library"> | ||
<library name="slf4j-log4j12-1.6.1.jar"> | ||
<CLASSES> | ||
<root url="jar://$USER_HOME$/opensource/zookeeper-3.4.5/lib/slf4j-log4j12-1.6.1.jar!/" /> | ||
</CLASSES> | ||
<JAVADOC /> | ||
<SOURCES /> | ||
</library> | ||
</orderEntry> | ||
</component> | ||
</module> | ||
|