lucene + hadoop 分布式并行計算搜索框架
BlogJava
首頁
新隨筆
聯(lián)系
聚合
管理
隨筆-23 評論-58 文章-0 trackbacks-0
JAVA NIO 多線程服務(wù)器 1.2版
Reactor 模式的 JAVA NIO 多線程服務(wù)器
public
class
MiniServer
extends
Thread
{
private
static
final
Log log
=
LogFactory.getLog(MiniServer.
class
);
private
final
Selector s;
private
final
ServerSocketChannel ssc;
private
ExecutorService executor;
public
MiniServer(
int
portnumber,ExecutorService executor)
throws
IOException
{
this
.executor
=
executor;
s
=
Selector.open();
ssc
=
ServerSocketChannel.open();
ssc.socket().bind(
new
InetSocketAddress(portnumber));
ssc.configureBlocking(
false
);
ssc.register(s,SelectionKey.OP_ACCEPT);
}
public
void
run()
{
try
{
while
(s.isOpen())
{
int
nKeys
=
s.select();
if
(nKeys
>
0
)
{
Iterator
<
SelectionKey
>
it
=
s.selectedKeys().iterator();
while
(it.hasNext())
{
SelectionKey key
=
it.next();
it.remove();
if
(
!
key.isValid()
||
!
key.channel().isOpen())
continue
;
if
(key.isAcceptable())
{
SocketChannel sc
=
ssc.accept();
if
(sc
!=
null
)
{
sc.configureBlocking(
false
);
sc.register(s, SelectionKey.OP_READ,
new
Reader(executor));
}
}
else
if
(key.isReadable()
||
key.isWritable())
{
Reactor reactor
=
(Reactor) key.attachment();
reactor.execute(key);
}
}
}
}
}
catch
(IOException e)
{
log.info(e);
}
}
}
public
interface
Reactor
{
void
execute(SelectionKey key);
}
public
class
Reader
implements
Reactor
{
private
static
final
Log log
=
LogFactory.getLog(Reader.
class
);
private
byte
[] bytes
=
new
byte
[
0
];
private
ExecutorService executor;
public
Reader(ExecutorService executor)
{
this
.executor
=
executor;
}
@Override
public
void
execute(SelectionKey key)
{
SocketChannel sc
=
(SocketChannel) key.channel();
try
{
ByteBuffer buffer
=
ByteBuffer.allocate(
1024
);
int
len
=-
1
;
while
(sc.isConnected()
&&
(len
=
sc.read(buffer))
>
0
)
{
buffer.flip();
byte
[] content
=
new
byte
[buffer.limit()];
buffer.get(content);
bytes
=
NutUtil.ArrayCoalition(bytes,content);
buffer.clear();
}
if
(len
==
0
)
{
key.interestOps(SelectionKey.OP_READ);
key.selector().wakeup();
}
else
if
(len
==-
1
)
{
Callable
<
byte
[]
>
call
=
new
ProcessCallable(bytes);
Future
<
byte
[]
>
task
=
executor.submit(call);
ByteBuffer output
=
ByteBuffer.wrap(task.get());
sc.register(key.selector(), SelectionKey.OP_WRITE,
new
Writer(output));
}
}
catch
(Exception e)
{
log.info(e);
}
}
}
public
class
Writer
implements
Reactor
{
private
static
final
Log log
=
LogFactory.getLog(Writer.
class
);
private
ByteBuffer output;
public
Writer(ByteBuffer output)
{
this
.output
=
output;
}
public
void
execute(SelectionKey key)
{
SocketChannel sc
=
(SocketChannel) key.channel();
try
{
while
(sc.isConnected()
&&
output.hasRemaining())
{
int
len
=
sc.write(output);
if
(len
<
0
)
{
throw
new
EOFException();
}
if
(len
==
0
)
{
key.interestOps(SelectionKey.OP_WRITE);
key.selector().wakeup();
break
;
}
}
if
(
!
output.hasRemaining())
{
output.clear();
key.cancel();
sc.close();
}
}
catch
(IOException e)
{
log.info(e);
}
}
}
posted on 2011-08-29 18:35
nianzai
閱讀(3107)
評論(3)
編輯
收藏
所屬分類:
NIO
評論:
#
re: JAVA NIO 多線程服務(wù)器 1.2版 2011-08-30 13:59 |
seo千里眼
這個多線程程序挺實用哦。
回復(fù)
更多評論
#
re: JAVA NIO 多線程服務(wù)器 1.2版 2011-09-03 16:13 |
阿不都外力
收藏一下!以后看。。。
回復(fù)
更多評論
#
re: JAVA NIO 多線程服務(wù)器 1.2版
2011-09-05 23:54 |
步步為營
Tomcat中用NIO比較多,搭建高性能服務(wù)器時NIO挺好用的,呵呵
回復(fù)
更多評論
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關(guān)文章:
JAVA NIO 多線程服務(wù)器 1.3版
JAVA NIO 多線程服務(wù)器 1.2版
JAVA NIO 多線程服務(wù)器 1.1版
<
2011年8月
>
日
一
二
三
四
五
六
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(9)
給我留言
查看公開留言
查看私人留言
隨筆分類
NIO(3)
Nut(lucene + hadoop 分布式并行計算框架)(5)
中文分詞(8)
分布式(2)
開發(fā)工具(2)
機(jī)器學(xué)習(xí)(1)
隨筆檔案
2013年5月 (1)
2013年4月 (3)
2012年9月 (3)
2012年8月 (2)
2012年7月 (1)
2012年6月 (2)
2011年8月 (3)
2011年4月 (2)
2011年2月 (1)
2010年12月 (1)
2010年11月 (1)
2010年10月 (1)
2010年9月 (1)
2010年7月 (1)
搜索
最新評論
1.?re: 基于詞典的正向最大匹配中文分詞算法,能實現(xiàn)中英文數(shù)字混合分詞
您好,您沒有給出Sentence和Token的定義,我猜不出啊
hdwgz@qq.com
--余道
2.?re: 全切分分詞程序,能實現(xiàn)中英文數(shù)字混合分詞
能對車牌號進(jìn)行分詞嗎? M 是什么啊
--sdyjmc
3.?re: JAVA NIO 多線程服務(wù)器 1.3版 [未登錄]
Handle 這個方法里面寫的是什么處理呢?能否也貼出來看看
--z
4.?re: 腳本、Ajax網(wǎng)頁內(nèi)容抓取工具(第二版)
共享源碼么
--diyunpeng
5.?re: JAVA NIO 多線程服務(wù)器 1.1版
ProcessCallable 這是什么包的呢
--jnan77
閱讀排行榜
1.?lucene + hadoop 分布式搜索運行框架 Nut 1.0a8(6676)
2.?lucene + hadoop 分布式搜索運行框架 Nut 1.0a9(5401)
3.?基于詞典的逆向最大匹配中文分詞算法,逆向分詞比正向分詞效果好 (4496)
4.?Nut開發(fā)環(huán)境搭建(虛擬機(jī)下hadoop0.20.2+zookeeper3.3.3+hbase0.90.2開發(fā)環(huán)境的搭建)(4089)
5.?隱馬可夫(HMM)中文分詞詞性標(biāo)注程序(3872)
評論排行榜
1.?lucene + hadoop 分布式搜索運行框架 Nut 1.0a8(11)
2.?lucene + hadoop 分布式搜索運行框架 Nut 1.0a9(9)
3.?Nut開發(fā)環(huán)境搭建(虛擬機(jī)下hadoop0.20.2+zookeeper3.3.3+hbase0.90.2開發(fā)環(huán)境的搭建)(6)
4.?lucene + hadoop 分布式搜索運行框架 Nut 1.0a7(4)
5.?全切分分詞程序,能實現(xiàn)中英文數(shù)字混合分詞(4)
Powered by:
博客園
模板提供:
滬江博客
Copyright ©2025 nianzai
主站蜘蛛池模板:
热re99久久6国产精品免费
|
亚洲人成影院77777
|
一级毛片在线免费播放
|
亚洲精品视频观看
|
精品亚洲AV无码一区二区三区
|
日韩精品极品视频在线观看免费
|
wwwxxx亚洲
|
99久久99久久免费精品小说
|
亚洲AV无码码潮喷在线观看
|
在线毛片片免费观看
|
免费可以看黄的视频s色
|
亚洲综合成人网在线观看
|
18禁止看的免费污网站
|
性xxxx黑人与亚洲
|
日本二区免费一片黄2019
|
亚洲高清在线mv
|
全部一级一级毛片免费看
|
久久免费区一区二区三波多野
|
无码精品A∨在线观看免费
|
国产成人精品亚洲日本在线
|
免费无码黄网站在线看
|
精品国产亚洲一区二区三区
|
午夜视频在线免费观看
|
亚洲国产电影在线观看
|
免费看a级黄色片
|
亚洲精品在线不卡
|
操美女视频免费网站
|
亚洲黄色在线电影
|
天天拍拍天天爽免费视频
|
亚洲国产一区在线观看
|
日韩成人免费视频播放
|
亚洲国产免费综合
|
亚洲精品国产免费
|
四虎影库久免费视频
|
亚洲日韩精品无码专区加勒比
|
四虎影永久在线高清免费
|
99热在线观看免费
|
韩国亚洲伊人久久综合影院
|
国产亚洲美日韩AV中文字幕无码成人
|
亚洲尹人香蕉网在线视颅
|
96免费精品视频在线观看
|