<!DOCTYPE html>
<!-- [if IE 8]><html class="no - js lt - ie9" lang="zh - CN" > <![endif] -->
<!-- [if gt IE 8]><! --> < html class = "no-js" lang = "zh-CN" > <!-- <![endif] -->
< head >
< meta charset = "utf-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
< title > groovy — Arthas 3.1.7 文档< / title >
< link rel = "shortcut icon" href = "_static/favicon.ico" / >
< script type = "text/javascript" src = "_static/js/modernizr.min.js" > < / script >
< script type = "text/javascript" id = "documentation_options" data-url_root = "./" src = "_static/documentation_options.js" > < / script >
< script type = "text/javascript" src = "_static/jquery.js" > < / script >
< script type = "text/javascript" src = "_static/underscore.js" > < / script >
< script type = "text/javascript" src = "_static/doctools.js" > < / script >
< script type = "text/javascript" src = "_static/language_data.js" > < / script >
< script type = "text/javascript" src = "_static/translations.js" > < / script >
< script type = "text/javascript" src = "_static/js/theme.js" > < / script >
< link rel = "stylesheet" href = "_static/css/theme.css" type = "text/css" / >
< link rel = "stylesheet" href = "_static/pygments.css" type = "text/css" / >
< link rel = "stylesheet" href = "_static/overrides.css" type = "text/css" / >
< link rel = "index" title = "索引" href = "genindex.html" / >
< link rel = "search" title = "搜索" href = "search.html" / >
< script src = "_static/center_page.js" > < / script >
< link rel = "stylesheet" href = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/github.min.css" >
< / head >
< body class = "wy-body-for-nav" >
< div class = "wy-grid-for-nav" >
< nav data-toggle = "wy-nav-shift" class = "wy-nav-side" >
< div class = "wy-side-scroll" >
< div class = "wy-side-nav-search" >
< a href = "index.html" class = "icon icon-home" > Arthas
< / a >
< div class = "version" >
3.1.7
< / div >
< div role = "search" >
< form id = "rtd-search-form" class = "wy-form" action = "search.html" method = "get" >
< input type = "text" name = "q" placeholder = "Search docs" / >
< input type = "hidden" name = "check_keywords" value = "yes" / >
< input type = "hidden" name = "area" value = "default" / >
< / form >
< / div >
< / div >
< div class = "wy-menu wy-menu-vertical" data-spy = "affix" role = "navigation" aria-label = "main navigation" >
< ul >
< li class = "toctree-l1" > < a class = "reference external" href = "https://alibaba.github.io/arthas/en/" > English Docs< / a > < / li >
< li class = "toctree-l1" > < a class = "reference external" href = "https://alibaba.github.io/arthas/arthas-tutorials?language=cn" > 在线教程(推荐)< / a > < / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "install-detail.html" > 安装< / a > < / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "download.html" > 下载< / a > < / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "quick-start.html" > 快速入门< / a > < / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "advanced-use.html" > 进阶使用< / a > < / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "commands.html" > 命令列表< / a > < / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "web-console.html" > WebConsole< / a > < / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "docker.html" > Docker< / a > < / li >
< li class = "toctree-l1" > < a class = "reference external" href = "https://github.com/alibaba/arthas/issues?q=label%3Auser-case" > 用户案例< / a > < / li >
< li class = "toctree-l1" > < a class = "reference external" href = "https://github.com/alibaba/arthas/issues?q=label%3Aquestion-answered" > 常见问题< / a > < / li >
< li class = "toctree-l1" > < a class = "reference external" href = "https://github.com/alibaba/arthas" > Fork me at GitHub< / a > < / li >
< li class = "toctree-l1" > < a class = "reference external" href = "https://github.com/alibaba/arthas/blob/master/CONTRIBUTING.md#" > 编译调试/参与贡献< / a > < / li >
< li class = "toctree-l1" > < a class = "reference external" href = "https://github.com/alibaba/arthas/releases" > Release Notes< / a > < / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "contact-us.html" > QQ群/钉钉群< / a > < / li >
< / ul >
< / div >
< / div >
< / nav >
< section data-toggle = "wy-nav-shift" class = "wy-nav-content-wrap" >
< nav class = "wy-nav-top" aria-label = "top navigation" >
< i data-toggle = "wy-nav-top" class = "fa fa-bars" > < / i >
< a href = "index.html" > Arthas< / a >
< / nav >
< div class = "wy-nav-content" >
< div class = "rst-content" >
< div role = "navigation" aria-label = "breadcrumbs navigation" >
< ul class = "wy-breadcrumbs" >
< li > < a href = "index.html" > Docs< / a > » < / li >
< li > groovy< / li >
< li class = "wy-breadcrumbs-aside" >
< a href = "https://github.com/alibaba/arthas/blob/master/site/src/site/sphinx/groovy.md" class = "fa fa-github" > Edit on GitHub< / a >
< / li >
< / ul >
< hr / >
< / div >
< div role = "main" class = "document" itemscope = "itemscope" itemtype = "http://schema.org/Article" >
< div itemprop = "articleBody" >
< div class = "section" id = "groovy" >
< h1 > groovy< a class = "headerlink" href = "#groovy" title = "永久链接至标题" > ¶< / a > < / h1 >
< blockquote >
< div > < p > Arthas 支持 groovy 脚本增强,允许像 BTrace 一样编写脚本来解决问题,可以在 groovy 脚本中进行if/for/switch/while 等控制语句,不受限制,但相比 BTrace 而言拥有更多的限制范围。< / p >
< / div > < / blockquote >
< div class = "section" id = "id1" >
< h2 > 限制内容< a class = "headerlink" href = "#id1" title = "永久链接至标题" > ¶< / a > < / h2 >
< ol class = "simple" >
< li > < p > 禁止改变原有逻辑,与 watch 等命令一样,重点保证的是监听和观察。< / p > < / li >
< li > < p > 只允许在方法的 before/success/exception/finish 四个环节进行监听。< / p > < / li >
< / ol >
< / div >
< div class = "section" id = "id2" >
< h2 > 参数说明< a class = "headerlink" href = "#id2" title = "永久链接至标题" > ¶< / a > < / h2 >
< table border = "1" class = "docutils" >
< thead >
< tr >
< th align = "right" > 参数名称< / th >
< th align = "left" > 参数说明< / th >
< / tr >
< / thead >
< tbody >
< tr >
< td align = "right" > < em > class-pattern< / em > < / td >
< td align = "left" > 类名表达式匹配< / td >
< / tr >
< tr >
< td align = "right" > < em > method-pattern< / em > < / td >
< td align = "left" > 方法名表达式匹配< / td >
< / tr >
< tr >
< td align = "right" > < em > script-filepath< / em > < / td >
< td align = "left" > groovy 脚本的绝对路径< / td >
< / tr >
< tr >
< td align = "right" > [S]< / td >
< td align = "left" > 匹配所有的子类< / td >
< / tr >
< tr >
< td align = "right" > [E]< / td >
< td align = "left" > 开启正则表达式匹配,默认为通配符匹配< / td >
< / tr >
< / tbody >
< / table > < p > 需要说明的是,第三个输入参数是脚本的绝对路径,比如 < code class = "docutils literal notranslate" > < span class = "pre" > /tmp/test.groovy< / span > < / code > ,不建议输入相对路径,比如 < code class = "docutils literal notranslate" > < span class = "pre" > ./test.groovy< / span > < / code > < / p >
< / div >
< div class = "section" id = "id3" >
< h2 > 五个关键函数声明< a class = "headerlink" href = "#id3" title = "永久链接至标题" > ¶< / a > < / h2 >
< div class = "highlight-java notranslate" > < div class = "highlight hljs" > < pre class = "java" > /**
* 增强脚本监听器
*/
interface ScriptListener {
/**
* 脚本创建
*
* @param output 输出器
*/
void create(Output output);
/**
* 脚本销毁
*
* @param output 输出器
*/
void destroy(Output output);
/**
* 方法执行前
*
* @param output 输出器
* @param advice 通知点
*/
void before(Output output, Advice advice);
/**
* 方法正常返回
*
* @param output 输出器
* @param advice 通知点
*/
void afterReturning(Output output, Advice advice);
/**
* 方法异常返回
*
* @param output 输出器
* @param advice 通知点
*/
void afterThrowing(Output output, Advice advice);
}< / pre > < / div >
< / div >
< / div >
< div class = "section" id = "advice" >
< h2 > 参数 < code class = "docutils literal notranslate" > < span class = "pre" > Advice< / span > < / code > 说明< a class = "headerlink" href = "#advice" title = "永久链接至标题" > ¶< / a > < / h2 >
< p > < code class = "docutils literal notranslate" > < span class = "pre" > Advice< / span > < / code > 参数最主要是封装了通知节点的所有信息。参考< a class = "reference internal" href = "advice-class.html" > < span class = "doc" > 表达式核心变量< / span > < / a > 中关于该节点的描述。< / p >
< / div >
< div class = "section" id = "output" >
< h2 > 参数 < code class = "docutils literal notranslate" > < span class = "pre" > Output< / span > < / code > 说明< a class = "headerlink" href = "#output" title = "永久链接至标题" > ¶< / a > < / h2 >
< p > < code class = "docutils literal notranslate" > < span class = "pre" > Output< / span > < / code > 参数只拥有三个方法,主要的工作还是输出对应的文本信息< / p >
< div class = "highlight-java notranslate" > < div class = "highlight hljs" > < pre class = "java" > /**
* 输出器
*/
interface Output {
/**
* 输出字符串(不换行)
*
* @param string 待输出字符串
* @return this
*/
Output print(String string);
/**
* 输出字符串(换行)
*
* @param string 待输出字符串
* @return this
*/
Output println(String string);
/**
* 结束当前脚本
*
* @return this
*/
Output finish();
}< / pre > < / div >
< / div >
< / div >
< div class = "section" id = "id4" >
< h2 > 一个输出日志的 groovy 脚本示例< a class = "headerlink" href = "#id4" title = "永久链接至标题" > ¶< / a > < / h2 >
< div class = "highlight-groovy notranslate" > < div class = "highlight hljs" > < pre class = "groovy" > import com.taobao.arthas.core.command.ScriptSupportCommand
import com.taobao.arthas.core.util.Advice
import static java.lang.String.format
/**
* 输出方法日志
*/
public class Logger implements ScriptSupportCommand.ScriptListener {
@Override
void create(ScriptSupportCommand.Output output) {
output.println(" script create." );
}
@Override
void destroy(ScriptSupportCommand.Output output) {
output.println(" script destroy." );
}
@Override
void before(ScriptSupportCommand.Output output, Advice advice) {
output.println(format(" before:class=%s;method=%s;paramslen=%d;%s;" ,
advice.getClazz().getSimpleName(),
advice.getMethod().getName(),
advice.getParams().length, advice.getParams()))
}
@Override
void afterReturning(ScriptSupportCommand.Output output, Advice advice) {
output.println(format(" returning:class=%s;method=%s;" ,
advice.getClazz().getSimpleName(),
advice.getMethod().getName()))
}
@Override
void afterThrowing(ScriptSupportCommand.Output output, Advice advice) {
output.println(format(" throwing:class=%s;method=%s;" ,
advice.getClazz().getSimpleName(),
advice.getMethod().getName()))
}
}< / pre > < / div >
< / div >
< p > 使用示例:< / p >
< div class = "highlight-default notranslate" > < pre class = "literal-block" > $ groovy com.alibaba.sample.petstore.dal.dao.ProductDao getProductById /Users/zhuyong/middleware/arthas/scripts/Logger.groovy -S
script create.
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 102 ms.
before:class=IbatisProductDao;method=getProductById;paramslen=1;[Ljava.lang.Object;@45df64fc;
returning:class=IbatisProductDao;method=getProductById;
before:class=IbatisProductDao;method=getProductById;paramslen=1;[Ljava.lang.Object;@5b0e2d00;
returning:class=IbatisProductDao;method=getProductById;< / pre >
< / div >
< / div >
< / div >
< / div >
< / div >
< footer >
< hr / >
< div role = "contentinfo" >
< p >
© Copyright 2018-2019, Alibaba Middleware Group, and contributors
< / p >
< / div >
Built with < a href = "http://sphinx-doc.org/" > Sphinx< / a > using a < a href = "https://github.com/rtfd/sphinx_rtd_theme" > theme< / a > provided by < a href = "https://readthedocs.org" > Read the Docs< / a > .
< / footer >
< / div >
< / div >
< / section >
< / div >
< script type = "text/javascript" >
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
< / script >
< div class = "github-fork-ribbon-wrapper right" >
< div class = "github-fork-ribbon" >
< a href = "https://github.com/alibaba/arthas" target = "_blank" > Fork me at GitHub< / a >
< / div >
< / div >
< script type = "text/javascript" src = "_static/add_badges.js" > < / script >
< script src = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/highlight.min.js" > < / script >
< script src = "https://cdnjs.cloudflare.com/ajax/libs/highlightjs-line-numbers.js/2.5.0/highlightjs-line-numbers.min.js" > < / script >
< script >
document.querySelectorAll('div.hljs > pre').forEach(function(block) {
hljs.highlightBlock(block);
hljs.lineNumbersBlock(block);
});
< / script >
< script >
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?d5c5e25b100f0eb51a4c35c8a86ea9b4";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
< / script >
< / body >
< / html >