有段时间没有更新了,今天上来与大家打声招呼先。Spy2servers这个项目自发布以来,受到不少的朋友支持,在此表示感谢。
经过一段时间的整理,现已经把整个项目转移到了SourceForge。目前的网址为
https://sourceforge.net/projects/spy2servers 希望大家能依旧大力支持。
言归正转,此次更新主要是支持Annotation,用于简单Spring的xml配置
此处下载
提供三种Annotation支持,分别 @SpyComponent @AlertComponent和@MessageAlertChannelActiveAwareComponent
我们在
上篇文章中应用Spy2servers组件接口简单演示了接口开发的实现。下面改成基于Annotation,让我们从配置中解放出来
下面是实现的代码,与原先的方法区别,就是使用了Annotation
1 package org.xmatthew.mypractise;
2
3 import org.xmatthew.spy2servers.annotation.AlertComponent;
4 import org.xmatthew.spy2servers.core.AbstractAlertComponent;
5 import org.xmatthew.spy2servers.core.Message;
6
7 /**
8 * @author Matthew Xie
9 *
10 */
11 @AlertComponent(name = "myAlertComponent")
12 public class SimpleAlertComponet extends AbstractAlertComponent{
13
14 private boolean started;
15
16 @Override
17 protected void onAlert(Message message) {
18 if (started) {
19 System.out.println(message);
20 }
21 }
22
23 public void startup() {
24
25 started = true;
26 setStatusRun();
27
28 }
29
30 public void stop() {
31 started = false;
32 setStatusStop();
33
34 }
35
36 }
1 /**
2 *
3 */
4 package org.xmatthew.mypractise;
5
6 import java.util.Collections;
7 import java.util.LinkedList;
8 import java.util.List;
9
10 import org.xmatthew.spy2servers.core.AbstractComponent;
11 import org.xmatthew.spy2servers.core.MessageAlertChannel;
12 import org.xmatthew.spy2servers.core.MessageAlertChannelActiveAwareComponent;
13
14 /**
15 * @author Matthew Xie
16 *
17 */
18 @org.xmatthew.spy2servers.annotation.MessageAlertChannelActiveAwareComponent(name = "SimpleChannelAwareComponent")
19 public class SimpleChannelAwareComponent extends AbstractComponent implements
20 MessageAlertChannelActiveAwareComponent {
21
22 private boolean started;
23
24 private List<MessageAlertChannel> channels = Collections.synchronizedList(new LinkedList<MessageAlertChannel>());
25
26 public List<MessageAlertChannel> getChannels() {
27 return channels;
28 }
29
30 public void onMessageAlertChannelActive(MessageAlertChannel channel) {
31 if (!started) {
32 return;
33 }
34 channels.add(channel);
35 printChannel(channel);
36 }
37
38 public void startup() {
39 started = true;
40 setStatusRun();
41
42 }
43
44 public void stop() {
45 started = false;
46 setStatusStop();
47
48 }
49
50 private void printChannel(MessageAlertChannel channel) {
51 if (channel != null) {
52 System.out.println("channel aware component say:");
53 System.out.print("spyComponent is: ");
54 System.out.println(channel.getSpyComponent());
55 System.out.print("alertComponent is: ");
56 System.out.println(channel.getAlertComponent());
57 System.out.print("message is: ");
58 System.out.println(channel.getMessage());
59 }
60 }
61
62
63 }
64
1 /**
2 *
3 */
4 package org.xmatthew.mypractise;
5
6 import java.util.Date;
7 import java.util.UUID;
8
9 import org.xmatthew.spy2servers.annotation.SpyComponent;
10 import org.xmatthew.spy2servers.core.AbstractSpyComponent;
11 import org.xmatthew.spy2servers.core.Message;
12
13 /**
14 * @author Matthew Xie
15 *
16 */
17 @SpyComponent(name = "mySpyComponent")
18 public class SimpleSpyComponent extends AbstractSpyComponent {
19
20 private boolean started;
21
22 /* (non-Javadoc)
23 * @see org.xmatthew.spy2servers.core.Component#startup()
24 */
25 public void startup() {
26 started = true;
27 setStatusRun();
28 try {
29 while (started) {
30 onSpy(createMessage());
31 Thread.sleep(5000);
32 }
33 } catch (Exception e) {
34 e.printStackTrace();
35 }
36 }
37
38 private Message createMessage() {
39 Message message = new Message();
40 message.setId(UUID.randomUUID().toString());
41 message.setCreateDate(new Date());
42 message.setDescription("message sent by " + getName());
43 message.setLevel(Message.LV_INFO);
44 message.setType("Test Message");
45 return message;
46 }
47
48 public void stop() {
49 started = false;
50 setStatusStop();
51
52 }
53
54 }
55
接下来,我们看一下配置文档 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.5.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.5.xsd">
<annotation-driven />
<context:component-scan base-package="org.xmatthew.mypractise"/>
<core-component> <!-- 配置核组件,这个必须要有 -->
<simple-alertRule> <!-- 配置 消息报警机制-->
<channel>
<from value="mySpyComponent"/>
<to value="myAlertComponent"/>
</channel>
</simple-alertRule>
</core-component>
<jmxService-component/> <!-- 开启jmx监控服务,其IP通过 java启动命令设置 默认为1616 -->
<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>
我们看到使用Annoation后,让配置部分精简了很多。
如有什么问题希望大家给我留言。
Good Luck!
Yours Matthew!
2008年5月5日 22:48:32
posted on 2008-05-05 22:49
x.matthew 阅读(1726)
评论(2) 编辑 收藏 所属分类:
Spy2Servers