Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦中观雨
cat
提交
85d814ed
C
cat
项目概览
梦中观雨
/
cat
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
cat
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
85d814ed
编写于
12月 13, 2013
作者:
Y
youyong205
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix the tcpsocket
上级
52c2b75c
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
26 addition
and
35 deletion
+26
-35
cat-client/src/main/java/com/dianping/cat/message/io/TcpSocketSender.java
...ain/java/com/dianping/cat/message/io/TcpSocketSender.java
+26
-35
未找到文件。
cat-client/src/main/java/com/dianping/cat/message/io/TcpSocketSender.java
浏览文件 @
85d814ed
package
com.dianping.cat.message.io
;
import
java.net.InetSocketAddress
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.TimeUnit
;
...
...
@@ -119,8 +117,6 @@ public class TcpSocketSender implements Task, MessageSender, LogEnabled {
}
}
}
m_manager
.
releaseAll
();
}
@Override
...
...
@@ -169,23 +165,21 @@ public class TcpSocketSender implements Task, MessageSender, LogEnabled {
m_manager
.
shutdown
();
}
static
class
ChannelManager
implements
Task
{
private
static
class
ChannelManager
implements
Task
{
private
List
<
InetSocketAddress
>
m_serverAddresses
;
private
List
<
ChannelFuture
>
m_futures
;
private
ClientBootstrap
m_bootstrap
;
private
ChannelFuture
m_activeFuture
;
private
int
m_activeIndex
;
private
Logger
m_logger
;
private
ChannelFuture
m_lastFuture
;
private
boolean
m_active
=
true
;
private
int
m_activeIndex
=
-
1
;
private
AtomicInteger
m_reconnects
=
new
AtomicInteger
(
999
);
public
ChannelManager
(
Logger
logger
,
List
<
InetSocketAddress
>
serverAddresses
)
{
...
...
@@ -193,7 +187,6 @@ public class TcpSocketSender implements Task, MessageSender, LogEnabled {
m_logger
=
logger
;
m_serverAddresses
=
serverAddresses
;
m_futures
=
new
ArrayList
<
ChannelFuture
>(
Collections
.<
ChannelFuture
>
nCopies
(
len
,
null
));
ExecutorService
bossExecutor
=
Threads
.
forPool
().
getFixedThreadPool
(
"Cat-TcpSocketSender-Boss"
,
10
);
ExecutorService
workerExecutor
=
Threads
.
forPool
().
getFixedThreadPool
(
"Cat-TcpSocketSender-Worker"
,
10
);
...
...
@@ -223,24 +216,28 @@ public class TcpSocketSender implements Task, MessageSender, LogEnabled {
}
}
ChannelFuture
createChannel
(
int
index
)
{
private
ChannelFuture
createChannel
(
int
index
)
{
InetSocketAddress
address
=
m_serverAddresses
.
get
(
index
);
ChannelFuture
future
=
m_bootstrap
.
connect
(
address
);
try
{
ChannelFuture
future
=
m_bootstrap
.
connect
(
address
);
future
.
awaitUninterruptibly
(
100
,
TimeUnit
.
MILLISECONDS
);
// 100 ms
future
.
awaitUninterruptibly
(
100
,
TimeUnit
.
MILLISECONDS
);
// 100 ms
if
(!
future
.
isSuccess
())
{
future
.
getChannel
().
getCloseFuture
().
awaitUninterruptibly
(
100
,
TimeUnit
.
MILLISECONDS
);
// 100ms
int
count
=
m_reconnects
.
incrementAndGet
();
if
(!
future
.
isSuccess
())
{
future
.
getChannel
().
getCloseFuture
().
awaitUninterruptibly
(
100
,
TimeUnit
.
MILLISECONDS
);
// 100ms
int
count
=
m_reconnects
.
incrementAndGet
();
if
(
count
%
1000
==
0
)
{
m_logger
.
error
(
"Error when try to connecting to "
+
address
+
", message: "
+
future
.
getCause
());
if
(
count
%
100
==
0
)
{
m_logger
.
error
(
"Error when try to connecting to "
+
address
+
", message: "
+
future
.
getCause
());
}
}
else
{
m_logger
.
info
(
"Connected to CAT server at "
+
address
);
return
future
;
}
return
null
;
}
else
{
m_logger
.
info
(
"Connected to CAT server at "
+
address
);
return
future
;
}
catch
(
Throwable
e
)
{
m_logger
.
error
(
"Error when connect server "
+
address
.
getAddress
(),
e
);
}
return
null
;
}
public
ChannelFuture
getChannel
()
{
...
...
@@ -257,17 +254,6 @@ public class TcpSocketSender implements Task, MessageSender, LogEnabled {
return
"TcpSocketHierarchySender-ChannelManager"
;
}
public
void
releaseAll
()
{
for
(
ChannelFuture
future
:
m_futures
)
{
if
(
future
!=
null
)
{
future
.
getChannel
().
getCloseFuture
().
awaitUninterruptibly
(
100
,
TimeUnit
.
MILLISECONDS
);
// 100ms
}
}
m_bootstrap
.
getFactory
().
releaseExternalResources
();
m_futures
=
null
;
}
@Override
public
void
run
()
{
try
{
...
...
@@ -276,7 +262,10 @@ public class TcpSocketSender implements Task, MessageSender, LogEnabled {
if
(
m_activeFuture
!=
null
&&
!
m_activeFuture
.
getChannel
().
isOpen
())
{
m_activeIndex
=
m_serverAddresses
.
size
();
}
if
(
m_activeIndex
==
-
1
)
{
m_activeIndex
=
m_serverAddresses
.
size
();
}
for
(
int
i
=
0
;
i
<
m_activeIndex
;
i
++)
{
ChannelFuture
future
=
createChannel
(
i
);
...
...
@@ -304,7 +293,7 @@ public class TcpSocketSender implements Task, MessageSender, LogEnabled {
}
}
static
class
ExceptionHandler
extends
SimpleChannelHandler
{
private
static
class
ExceptionHandler
extends
SimpleChannelHandler
{
private
Logger
m_logger
;
public
ExceptionHandler
(
Logger
logger
)
{
...
...
@@ -314,10 +303,12 @@ public class TcpSocketSender implements Task, MessageSender, LogEnabled {
@Override
public
void
channelDisconnected
(
ChannelHandlerContext
ctx
,
ChannelStateEvent
e
)
throws
Exception
{
m_logger
.
warn
(
"Channel disconnected by remote address: "
+
e
.
getChannel
().
getRemoteAddress
());
e
.
getChannel
().
close
();
}
@Override
public
void
exceptionCaught
(
ChannelHandlerContext
ctx
,
ExceptionEvent
e
)
{
m_logger
.
warn
(
"Channel disconnected by remote address: "
+
e
.
getChannel
().
getRemoteAddress
());
e
.
getChannel
().
close
();
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录