posts - 156,  comments - 601,  trackbacks - 0

          前一篇文章已經(jīng)把spy2servers的用戶使用手冊(cè)整理出來(lái)了,這次更新主要是把開(kāi)發(fā)手冊(cè)部分的整理。

          如果沒(méi)有下載的朋友可以從下面下載spy2servers。
          1. 下載
          下載地址:
          二進(jìn)制程序
          第三方類庫(kù)下載,第三方類庫(kù)下載2  Jetty類庫(kù) 放到lib目錄下。
          api-docs
          源代碼


          開(kāi)發(fā)手冊(cè):

          spy2servers對(duì)外提供三個(gè)組件接口,分別SpyComponent, AlertComponent和MessageAlertChannelActiveAwareComponent
          下面是接口的實(shí)現(xiàn)類圖:
          SpyComponent



          AlertComponent


          MessageAlertChannelActiveAwareComponent


          SpyComponet接口提供監(jiān)控組件的功能。我們可以直接去實(shí)現(xiàn)該接口即可。不過(guò)為了方便 spy2servers還提供AbstractSpyComponent抽象類
          幫助我們更快速的開(kāi)發(fā)。
          下面來(lái)簡(jiǎn)單的根據(jù)來(lái)實(shí)現(xiàn)一個(gè)SimpleSpyComponent.功能很簡(jiǎn)單,就是每5秒鐘發(fā)送一個(gè)監(jiān)控消息。
          SimpleSpyComponent繼承AbstractSpyComponent。
          下面源代碼:因?yàn)閷?shí)現(xiàn)非常簡(jiǎn)單,我就不解釋了。

           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 }


          好了,現(xiàn)在我實(shí)現(xiàn)一個(gè)監(jiān)控組件,讓我們?cè)賮?lái)實(shí)現(xiàn)一個(gè)報(bào)警組件SimpleAlertComponet,它繼承AbstractAlertComponent抽象類。
          下面是實(shí)現(xiàn)的源代碼:功能也是超簡(jiǎn)單,就是如果組件的狀態(tài)正常,則從屏幕輸出Message消息內(nèi)容

           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 }

          接下來(lái)我們來(lái)實(shí)現(xiàn)MessageAlertChannelActiveAwareComponent組件。它的功能是消息調(diào)度監(jiān)控組件接口,提供消息調(diào)度監(jiān)控的管理。
          我們知道JmxServiceComponent組件就實(shí)現(xiàn)該接口,通過(guò)JMX服務(wù)提供消息調(diào)度情況。
          下面是實(shí)現(xiàn)的源代碼,只是把每次調(diào)試的消息打印出來(lái)

           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,接下來(lái)我們來(lái)配置spy2servers.xml文件使用它工作起來(lái)。

          <?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> <!-- 配置核組件,這個(gè)必須要有  -->
                  
          <simple-alertRule> <!-- 配置 消息報(bào)警機(jī)制-->
                      
          <channel> 
                          
          <from value="mySpyComponent"/>
                          
          <to value="myAlertComponent"/>
                      
          </channel>
                  
          </simple-alertRule>
              
          </core-component>
              
              
          <jmxService-component/> <!-- 開(kāi)啟jmx監(jiān)控服務(wù),其IP通過(guò) java啟動(dòng)命令設(shè)置 默認(rèn)為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> <!-- 配置內(nèi)置服務(wù)器  -->
              
          <connectors>
                
          <nioConnector port="7758" /> <!-- using nio connector port is 7758 -->
              
          </connectors>
              
          <handlers>
                  
          <!-- 配置內(nèi)置基于web 方式的平臺(tái)組件監(jiān)控 servlet context為 /admin  -->
                
          <servlet servletClass="org.xmatthew.spy2servers.component.web.ComponentsViewServlet" path="/admin" /> 
              
          </handlers>
            
          </jetty>
              
          </beans:beans>


          好了,現(xiàn)在我們運(yùn)行start.sh啟動(dòng)看一下效果
          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

          沒(méi)問(wèn)題我們的組件已經(jīng)運(yùn)行起來(lái),接下就是五秒輸出結(jié)果:

          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或是進(jìn)入web管理頁(yè)面查看組件的狀態(tài)。

          大功告成,我們已經(jīng)對(duì)spy2serves平臺(tái)提供三個(gè)組件都做了簡(jiǎn)單實(shí)現(xiàn)。
          如果有什么問(wèn)題歡迎大家給我留言。

          Good Luck!
          Yours Matthew!
          2008年4月25日
          posted on 2008-04-25 22:47 x.matthew 閱讀(3650) 評(píng)論(18)  編輯  收藏 所屬分類: Spy2Servers
          主站蜘蛛池模板: 会理县| 雷波县| 浑源县| 嘉兴市| 连山| 大安市| 新源县| 正阳县| 信阳市| 漯河市| 阿勒泰市| 邓州市| 抚顺市| 开阳县| 台北市| 丹江口市| 广河县| 鸡东县| 修文县| 祥云县| 中卫市| 保靖县| 荥经县| 新丰县| 布拖县| 米脂县| 屯留县| 天水市| 勐海县| 鄯善县| 南京市| 宣武区| 永宁县| 响水县| 鄂伦春自治旗| 察隅县| 扎鲁特旗| 新昌县| 厦门市| 武宣县| 利津县|