Skip to content

Commit

Permalink
增加ZooKeeper源代码解读之ClientCnxnSocketNIO的链接
Browse files Browse the repository at this point in the history
  • Loading branch information
llohellohe committed Feb 2, 2014
1 parent c9ff81b commit 7eb59af
Show file tree
Hide file tree
Showing 12 changed files with 268 additions and 51 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ zookeeper

[源代码解读之ClientCnxn](http://www.hiyangqi.com/%E5%88%86%E5%B8%83%E5%BC%8F/read-zookeeper-source-code-client-cnxn.html)

[源代码解读之ClientCnxnSocketNIO](http://www.hiyangqi.com/%E5%88%86%E5%B8%83%E5%BC%8F/read-zookeeper-source-code-nio-socket.html)


9 changes: 7 additions & 2 deletions src/main/java/yangqi/code/App.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package yangqi.code;

import java.io.IOException;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;

/**
* Hello world!
Expand All @@ -26,6 +27,10 @@ public void process(WatchedEvent event) {

});

Stat e=zk.exists("/yangqi_test",null);

System.out.println("exists "+e);

zk.setData("/yangqi_test", "Data of node 3".getBytes(), -1);
}

Expand Down
112 changes: 112 additions & 0 deletions src/main/java/yangqi/nio/ZkNIOClient.java
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();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/*
* Copyright 1999-2010 Alibaba.com All right reserved. This software is the
* confidential and proprietary information of Alibaba.com ("Confidential
* Information"). You shall not disclose such Confidential Information and shall
* use it only in accordance with the terms of the license agreement you entered
* into with Alibaba.com.
* Copyright 1999-2010 Alibaba.com All right reserved. This software is the confidential and proprietary information of
* Alibaba.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
* in accordance with the terms of the license agreement you entered into with Alibaba.com.
*/
package yangqi.zookeeper.example.masterworker;

Expand All @@ -25,18 +23,19 @@ public class ChildrenCallbackMonitor {
*/
public static void main(String[] args) throws IOException, InterruptedException {
final ZooKeeper zookeeper = new ZooKeeper("localhost:2181", 2000, null);

final ChildrenCallback callback = new ChildrenCallback() {

@Override
public void processResult(int rc, String path, Object ctx, List<String> children) {
System.out.println(children);

}

};

Watcher watcher = new Watcher() {

@Override
public void process(WatchedEvent event) {
System.out.println("Event is " + event);
Expand All @@ -46,11 +45,11 @@ public void process(WatchedEvent event) {
}
}
};

zookeeper.getChildren("/workers", watcher, callback, null);

zookeeper.getChildren("/workers", watcher, callback, null);

Thread.sleep(200000);
System.out.println("finish");
// Thread.sleep(200000);

}

Expand Down
38 changes: 0 additions & 38 deletions src/test/java/yangqi/code/AppTest.java

This file was deleted.

Binary file modified target/classes/yangqi/code/App$1.class
Binary file not shown.
Binary file modified target/classes/yangqi/code/App.class
Binary file not shown.
Binary file modified target/classes/yangqi/code/DataMonitor.class
Binary file not shown.
Binary file modified target/classes/yangqi/code/DataMonitorListener.class
Binary file not shown.
Binary file modified target/classes/yangqi/code/Executor$StreamWriter.class
Binary file not shown.
Binary file modified target/classes/yangqi/code/Executor.class
Binary file not shown.
137 changes: 137 additions & 0 deletions zookeeper.iml
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>

0 comments on commit 7eb59af

Please sign in to comment.