使用telnet连接到基于spring的应用上执行容器中的bean的任意方法 .
01.使用telnet连接到基于spring的应用上执行容器中的bean的任意方法
02.
03.使用telnet连接到基于spring的应用上执行容器中配置的任何bean的任意方法,可以用来诊断某个方法是否执行有问题,响应时间多少,在生产环境中可以很好的定位及监控方法是否存在问题。
04.
05.代码在:https://github.com/zhwj184/springInvokemonitor
06.
07. git clone git@github.com:zhwj184/springInvokemonitor.git
08.
09. maven clean install
10.
11.pom依赖:
12.
13.
14.
15.
16.
17.
18.
19.使用方式,在spring的配置文件中添加下面这个bean即可。
20.
21.
22.
23.使用示例
http://blog.csdn.net/zhongweijian/article/details/9830527:先写一个service TestBean,里面有两个方法,add和addBean
24.
25. import com.alibaba.fastjson.JSON;
26.
27. public class TestBean {
28.
29. public float add(int a, float b){
30. return a + b;
31. }
32.
33. public A addBean(A a, A b){
34. A c = new A();
35. c.setC(a.getC() + b.getC());
36. c.setD(a.getD() + b.getD());
37. return c;
38. }
39. public static void main(String[] args) {
40. A c = new A();
41. c.setC(23);
42. c.setD(323.34);
43. System.out.println(JSON.toJSON(c));
44. }
45.
46. }
47.
48. class A{
49. int c ;
50. double d;
51. public int getC() {
52. return c;
53. }
54. public void setC(int c) {
55. this.c = c;
56. }
57. public double getD() {
58. return d;
59. }
60. public void setD(double d) {
61. this.d = d;
62. }
63.
64. }
65.
66.然后在spring配置文件 service.xml添加
67.
68.
69.
70.
71.写一个测试类,执行这个main方法,
72.
73. import org.springframework.context.support.ClassPathXmlApplicationContext;
74.
75. public class MainTest {
76.
77. /**
78. * @param args
79. * @throws InterruptedException
80. */
81. public static void main(String[] args) throws InterruptedException {
82. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(“classpath:service.xml”);
83. Thread.sleep(100000000);
84. }
85.
86. }
87.
88.然后通过telnet连到这个服务上,打开命令行窗口,输入telnet localhost 12667,连上之后,输入ls bean名称,即可查询这个bean的所有方法,使用invoke beanname.method(param1,param2) 执行某个方法,参数如果为bean则可以使用json格式传入,参数之间用;分隔
89.
90.
91. ======================================================
92.
93. Welcome to Telnet Server: Version 1.0
94.
95. ======================================================
96.
97. List of possible commands:
98.
99. Status: displays the status of the server
100. cd : [ cd /usr/local]
101. pwd: displays the working directory
102. ls: displays list of files in the working directory
103. mkdir : [ mkdir /usr/local/tmp]
104. exit : quit this programme
105.
106. ls testBean
107. org.zhwj184.A org.zhwj184.TestBean.addBean(org.zhwj184.A,org.zhwj184.A)
108. void org.zhwj184.TestBean.main([Ljava.lang.String;)
109. float org.zhwj184.TestBean.add(int,float)
110. int org.zhwj184.TestBean.hashCode()
111. java.lang.Class org.zhwj184.TestBean.getClass()
112. void org.zhwj184.TestBean.wait(long,int)
113. void org.zhwj184.TestBean.wait()
114. void org.zhwj184.TestBean.wait(long)
115. boolean org.zhwj184.TestBean.equals(java.lang.Object)
116. java.lang.String org.zhwj184.TestBean.toString()
117. void org.zhwj184.TestBean.notify()
118. void org.zhwj184.TestBean.notifyAll()
119.
120. invoke testBean.add(1;2.4)
121. result:java.lang.Float 3.4
122. run time: 0ms
123.
124. invoke testBean.addBean({“c”:23,”d”:323.34};{“c”:3433,”d”:3232433.34})
125. result:org.zhwj184.A {“c”:3456,”d”:3232756.6799999997}
126. run time: 0ms
127.
128.这样可以诊断spring容易中任何bean的任意方法,执行方法,看看返回结果是否跟预期的一致。一般生产环境跟测试环境很多依赖条件(数据库,服务等)都不一样,所以线上出现问题一般都可以知道参数,通过执行方法就可以看看结果是否正确,并且查看某个方法的执行时间来看方法是否有性能问题。
129.
130.不过这里的代码只是简单做个示例,代码中关于参数的类型,结果匹配反射等可能不够完善也没有测试得很充分,有问题有兴趣的环境反馈给我。