Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦中观雨
cat
提交
9bdd2a8e
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,发现更多精彩内容 >>
提交
9bdd2a8e
编写于
1月 28, 2012
作者:
F
Frankie Wu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
auto reconnect
上级
2c952ef9
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
62 addition
and
8 deletion
+62
-8
cat-core/src/main/java/com/dianping/cat/message/io/TcpSocketSender.java
...ain/java/com/dianping/cat/message/io/TcpSocketSender.java
+62
-8
未找到文件。
cat-core/src/main/java/com/dianping/cat/message/io/TcpSocketSender.java
浏览文件 @
9bdd2a8e
...
...
@@ -3,6 +3,8 @@ package com.dianping.cat.message.io;
import
java.net.InetSocketAddress
;
import
java.util.concurrent.Executors
;
import
org.codehaus.plexus.logging.LogEnabled
;
import
org.codehaus.plexus.logging.Logger
;
import
org.jboss.netty.bootstrap.ClientBootstrap
;
import
org.jboss.netty.buffer.ChannelBuffer
;
import
org.jboss.netty.buffer.ChannelBuffers
;
...
...
@@ -11,6 +13,7 @@ import org.jboss.netty.channel.ChannelFuture;
import
org.jboss.netty.channel.ChannelHandlerContext
;
import
org.jboss.netty.channel.ChannelPipeline
;
import
org.jboss.netty.channel.ChannelPipelineFactory
;
import
org.jboss.netty.channel.ChannelStateEvent
;
import
org.jboss.netty.channel.Channels
;
import
org.jboss.netty.channel.ExceptionEvent
;
import
org.jboss.netty.channel.MessageEvent
;
...
...
@@ -21,7 +24,7 @@ import com.dianping.cat.message.spi.MessageCodec;
import
com.dianping.cat.message.spi.MessageTree
;
import
com.site.lookup.annotation.Inject
;
public
class
TcpSocketSender
implements
MessageSender
{
public
class
TcpSocketSender
implements
MessageSender
,
LogEnabled
{
@Inject
private
String
m_host
;
...
...
@@ -35,13 +38,25 @@ public class TcpSocketSender implements MessageSender {
private
ChannelFuture
m_future
;
private
ClientBootstrap
m_bootstrap
;
private
int
m_reconnectPeriod
=
5000
;
// every 5 seconds
private
long
m_lastReconnectTime
;
private
Logger
m_logger
;
@Override
public
void
enableLogging
(
Logger
logger
)
{
m_logger
=
logger
;
}
@Override
public
void
initialize
()
{
if
(
m_host
==
null
)
{
throw
new
RuntimeException
(
"No host was configured for TcpSocketSender!"
);
}
InetSocketAddress
address
=
new
InetSocketAddress
(
m_host
,
m_port
);
ChannelFactory
factory
=
new
NioClientSocketChannelFactory
(
Executors
.
newCachedThreadPool
(),
Executors
.
newCachedThreadPool
());
ClientBootstrap
bootstrap
=
new
ClientBootstrap
(
factory
);
...
...
@@ -55,24 +70,56 @@ public class TcpSocketSender implements MessageSender {
bootstrap
.
setOption
(
"tcpNoDelay"
,
true
);
bootstrap
.
setOption
(
"keepAlive"
,
true
);
InetSocketAddress
address
=
new
InetSocketAddress
(
m_host
,
m_port
);
ChannelFuture
future
=
bootstrap
.
connect
(
address
);
future
.
awaitUninterruptibly
();
if
(!
future
.
isSuccess
())
{
future
.
getCause
().
printStackTrace
(
);
m_logger
.
error
(
"Error when connecting to "
+
address
,
future
.
getCause
()
);
}
else
{
m_factory
=
factory
;
m_future
=
future
;
m_logger
.
info
(
"Connected to CAT server at "
+
address
);
}
m_bootstrap
=
bootstrap
;
}
public
void
reconnect
()
{
long
now
=
System
.
currentTimeMillis
();
if
(
m_lastReconnectTime
>
0
&&
m_lastReconnectTime
+
m_reconnectPeriod
>
now
)
{
return
;
}
m_lastReconnectTime
=
now
;
InetSocketAddress
address
=
new
InetSocketAddress
(
m_host
,
m_port
);
ChannelFuture
future
=
m_bootstrap
.
connect
(
address
);
future
.
awaitUninterruptibly
();
if
(!
future
.
isSuccess
())
{
m_logger
.
error
(
"Error when reconnecting to "
+
address
,
future
.
getCause
());
}
else
{
m_future
=
future
;
m_logger
.
info
(
"Reconnected to CAT server at "
+
address
);
}
}
@Override
public
void
send
(
MessageTree
tree
)
{
ChannelBuffer
buf
=
ChannelBuffers
.
dynamicBuffer
(
20
*
1024
);
// 20K
if
(
m_future
==
null
||
!
m_future
.
getChannel
().
isOpen
())
{
reconnect
();
}
m_codec
.
encode
(
tree
,
buf
);
m_future
.
getChannel
().
write
(
buf
);
if
(
m_future
!=
null
&&
m_future
.
getChannel
().
isOpen
())
{
ChannelBuffer
buf
=
ChannelBuffers
.
dynamicBuffer
(
20
*
1024
);
// 20K
m_codec
.
encode
(
tree
,
buf
);
m_future
.
getChannel
().
write
(
buf
);
}
}
public
void
setCodec
(
MessageCodec
codec
)
{
...
...
@@ -87,6 +134,10 @@ public class TcpSocketSender implements MessageSender {
m_port
=
port
;
}
public
void
setReconnectPeriod
(
int
reconnectPeriod
)
{
m_reconnectPeriod
=
reconnectPeriod
;
}
@Override
public
void
shutdown
()
{
m_future
.
getChannel
().
getCloseFuture
().
awaitUninterruptibly
();
...
...
@@ -95,9 +146,12 @@ public class TcpSocketSender implements MessageSender {
class
MyHandler
extends
SimpleChannelHandler
{
@Override
public
void
exceptionCaught
(
ChannelHandlerContext
ctx
,
ExceptionEvent
e
)
{
e
.
getCause
().
printStackTrace
();
public
void
channelDisconnected
(
ChannelHandlerContext
ctx
,
ChannelStateEvent
e
)
throws
Exception
{
m_logger
.
warn
(
"Channel disconnected by remote address: "
+
e
.
getChannel
().
getRemoteAddress
());
}
@Override
public
void
exceptionCaught
(
ChannelHandlerContext
ctx
,
ExceptionEvent
e
)
{
e
.
getChannel
().
close
();
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录