posts - 156,  comments - 601,  trackbacks - 0

前一篇文章已经把spy2servers的用户使用手册整理出来了,这次更新主要是把开发手册部分的整理。

如果没有下载的朋友可以从下面下载spy2servers。
1. 下载
下载地址:
二进制程序
第三方类库下载,第三方类库下载2  Jetty类库 放到lib目录下。
api-docs
源代码


开发手册:

spy2servers对外提供三个组件接口,分别SpyComponent, AlertComponent和MessageAlertChannelActiveAwareComponent
下面是接口的实现类图:
SpyComponent



AlertComponent


MessageAlertChannelActiveAwareComponent


SpyComponet接口提供监控组件的功能。我们可以直接去实现该接口即可。不过为了方便 spy2servers还提供AbstractSpyComponent抽象类
帮助我们更快速的开发。
下面来简单的根据来实现一个SimpleSpyComponent.功能很简单,就是每5秒钟发送一个监控消息。
SimpleSpyComponent继承AbstractSpyComponent。
下面源代码:因为实现非常简单,我就不解释了。

 1 import java.util.Date;
 2 import java.util.UUID;
 3 
 4 import org.xmatthew.spy2servers.core.AbstractSpyComponent;
 5 import org.xmatthew.spy2servers.core.Message;
 6 
 7 /**
 8  * @author Matthew Xie
 9  *
10  */
11 public class SimpleSpyComponent extends AbstractSpyComponent {
12     
13     private boolean started;
14 
15     /* (non-Javadoc)
16      * @see org.xmatthew.spy2servers.core.Component#startup()
17      */
18     public void startup() {
19         
20         started = true;
21         setStatusRun();
22         try {
23             while (started) {
24                 onSpy(createMessage());
25                 Thread.sleep(5000);
26             }
27         } catch (Exception e) {
28             e.printStackTrace();
29         }
30     }
31     
32     private Message createMessage() {
33         Message message = new Message();
34         message.setId(UUID.randomUUID().toString());
35         message.setCreateDate(new Date());
36         message.setDescription("message sent by " + getName());
37         message.setLevel(Message.LV_INFO);
38         message.setType("Test Message");
39         return message;
40     }
41 
42     public void stop() {
43         started = false;
44         setStatusStop();
45     }
46 
47 }


好了,现在我实现一个监控组件,让我们再来实现一个报警组件SimpleAlertComponet,它继承AbstractAlertComponent抽象类。
下面是实现的源代码:功能也是超简单,就是如果组件的状态正常,则从屏幕输出Message消息内容

 1 import org.xmatthew.spy2servers.core.AbstractAlertComponent;
 2 import org.xmatthew.spy2servers.core.Message;
 3 
 4 /**
 5  * @author Matthew Xie
 6  *
 7  */
 8 public class SimpleAlertComponet extends AbstractAlertComponent{
 9     
10     private boolean started;
11 
12     @Override
13     protected void onAlert(Message message) {
14         if (started) {
15             System.out.println(message);
16         }
17     }
18 
19     public void startup() {
20         
21         started = true;
22         setStatusRun();
23     
24     }
25 
26     public void stop() {
27         started = false;
28         setStatusStop();
29         
30     }
31 
32 }

接下来我们来实现MessageAlertChannelActiveAwareComponent组件。它的功能是消息调度监控组件接口,提供消息调度监控的管理。
我们知道JmxServiceComponent组件就实现该接口,通过JMX服务提供消息调度情况。
下面是实现的源代码,只是把每次调试的消息打印出来

 1 import java.util.Collections;
 2 import java.util.LinkedList;
 3 import java.util.List;
 4 
 5 import org.xmatthew.spy2servers.core.AbstractComponent;
 6 import org.xmatthew.spy2servers.core.MessageAlertChannel;
 7 import org.xmatthew.spy2servers.core.MessageAlertChannelActiveAwareComponent;
 8 
 9 /**
10  * @author Matthew Xie
11  *
12  */
13 public class SimpleChannelAwareComponent extends AbstractComponent implements
14         MessageAlertChannelActiveAwareComponent {
15     
16     private boolean started;
17     
18     private List<MessageAlertChannel> channels = Collections.synchronizedList(new LinkedList<MessageAlertChannel>());
19 
20     public List<MessageAlertChannel> getChannels() {
21         return channels;
22     }
23 
24     public void onMessageAlertChannelActive(MessageAlertChannel channel) {
25         if (!started) {
26             return;
27         }
28         channels.add(channel);
29         printChannel(channel);
30     }
31 
32     public void startup() {
33         started = true;
34         setStatusRun();
35         
36     }
37 
38     public void stop() {
39         started = false;
40         setStatusStop();
41         
42     }
43 
44     private void printChannel(MessageAlertChannel channel) {
45         if (channel != null) {
46             System.out.println("channel aware component say:");
47             System.out.print("spyComponent is: ");
48             System.out.println(channel.getSpyComponent());
49             System.out.print("alertComponent is: ");
50             System.out.println(channel.getAlertComponent());
51             System.out.print("message is: ");
52             System.out.println(channel.getMessage());            
53         }
54     }
55 
56 
57 }

ok,接下来我们来配置spy2servers.xml文件使用它工作起来。

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.xmatthew.org/spy2servers/schema"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans
="http://www.springframework.org/schema/beans"
    xmlns:context
="http://www.springframework.org/schema/context"
    xmlns:util
="http://www.springframework.org/schema/util"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.xmatthew.org/spy2servers/schema
        http://www.xmatthew.org/spy2servers/schema/spy2servers-1.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.0.xsd"
>

    
<core-component> <!-- 配置核组件,这个必须要有  -->
        
<simple-alertRule> <!-- 配置 消息报警机制-->
            
<channel> 
                
<from value="mySpyComponent"/>
                
<to value="myAlertComponent"/>
            
</channel>
        
</simple-alertRule>
    
</core-component>
    
    
<jmxService-component/> <!-- 开启jmx监控服务,其IP通过 java启动命令设置 默认为1616 -->

    
<!-- 定义 SimpleSpyComponent组件-->
    
<beans:bean class="org.xmatthew.mypractise.SimpleSpyComponent">
        
<beans:property name="name" value="mySpyComponent"></beans:property>
    
</beans:bean>
    
    
<!-- 定义 SimpleAlertComonent 组件-->
    
<beans:bean class="org.xmatthew.mypractise.SimpleAlertComponet">
        
<beans:property name="name" value="myAlertComponent"></beans:property>
    
</beans:bean>    

    
<!-- 定义 SimpleChannelAwareComponent 组件-->
    
<beans:bean class="org.xmatthew.mypractise.SimpleChannelAwareComponent">
        
<beans:property name="name" value="SimpleChannelAwareComponent"></beans:property>
    
</beans:bean>    

  
<jetty> <!-- 配置内置服务器  -->
    
<connectors>
      
<nioConnector port="7758" /> <!-- using nio connector port is 7758 -->
    
</connectors>
    
<handlers>
        
<!-- 配置内置基于web 方式的平台组件监控 servlet context为 /admin  -->
      
<servlet servletClass="org.xmatthew.spy2servers.component.web.ComponentsViewServlet" path="/admin" /> 
    
</handlers>
  
</jetty>
    
</beans:beans>


好了,现在我们运行start.sh启动看一下效果
INFO  Main                           - Server starting
INFO  
log                            - Logging to org.slf4j.impl.JCLLoggerAdapter(org.mortbay.log) via org.mortbay.log.Slf4jLog
INFO  CoreComponent                  
- plug component CoreComponent
INFO  CoreComponent                  
- plug component JmxServiceComponent
INFO  CoreComponent                  
- plug component mySpyComponent
INFO  CoreComponent                  
- plug component myAlertComponent
INFO  CoreComponent                  
- plug component SimpleChannelAwareComponent
INFO  
log                            - jetty-6.1.4

没问题我们的组件已经运行起来,接下就是五秒输出结果:

channel aware component say:
spyComponent 
is: org.xmatthew.mypractise.SimpleSpyComponent@193385d
alertComponent 
is: org.xmatthew.mypractise.SimpleAlertComponet@5973ea
message 
is: org.xmatthew.spy2servers.core.Message@171f189[id=cb8c02c8-62b3-4c7f-8dc3-aea41382c178,body=<null>,level=4,properties={},createDate=Fri Apr 25 22:39:24 CST 2008,description=message sent by mySpyComponent,type=Test Message]
org.xmatthew.spy2servers.core.Message
@1a897a9[id=52a954ef-2b3b-4374-a257-e3f3a9b5e209,body=<null>,level=4,properties={from=mySpyComponent, to=myAlertComponent},createDate=Fri Apr 25 22:39:29 CST 2008,description=message sent by mySpyComponent,type=Test Message]
channel aware component say:
spyComponent 
is: org.xmatthew.mypractise.SimpleSpyComponent@193385d
alertComponent 
is: org.xmatthew.mypractise.SimpleAlertComponet@5973ea
message 
is: org.xmatthew.spy2servers.core.Message@17cec96[id=52a954ef-2b3b-4374-a257-e3f3a9b5e209,body=<null>,level=4,properties={},createDate=Fri Apr 25 22:39:29 CST 2008,description=message sent by mySpyComponent,type=Test Message]
org.xmatthew.spy2servers.core.Message
@1947496[id=548c0c9f-0aa5-480d-a91b-b0a6d98d4aeb,body=<null>,level=4,properties={from=mySpyComponent, to=myAlertComponent},createDate=Fri Apr 25 22:39:34 CST 2008,description=message sent by mySpyComponent,type=Test Message]

可以使用JMX或是进入web管理页面查看组件的状态。

大功告成,我们已经对spy2serves平台提供三个组件都做了简单实现。
如果有什么问题欢迎大家给我留言。

Good Luck!
Yours Matthew!
2008年4月25日
posted on 2008-04-25 22:47 x.matthew 阅读(3596) 评论(18)  编辑  收藏 所属分类: Spy2Servers

只有注册用户登录后才能发表评论。


网站导航: