Developer Guide
Only serveral steps will help you to develop your own monitor and alert component.
The following prerequisites are required for this tutorial:
- Spy2Servers-1.2.x(include the third-party libraries)
- JDK 1.5 or greater
1. download Spy2Servers-1.2.x and then unzip to a local directory.
2. add Spy2Servers-1.2.x.jar (include the third-party libraries) to your build path
3. to implement a very simple monitor component
3a. Here we are going to develop very easy monitor component, it will trigger monitor event every 5 seconds.
the first step is let our class to extends from org.xmatthew.spy2servers.core.AbstractSpyComponent
public class SimpleSpyComponent extends AbstractSpyComponent {
public void startup() {
}
public void stop() {
}
}
Now we go to implenent our code. the complent code as bellow.
public class SimpleSpyComponent extends AbstractSpyComponent {
public void startup() {
setStatusRun();
try {
while (isActive()) {
//every 5 seconds invoke the callback method onSpy(Message message)
onSpy(createMessage());
Thread.sleep(5000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private Message createMessage() {
Message message = new Message();
message.setId(UUID.randomUUID().toString());
message.setCreateDate(new Date());
message.setDescription("message sent by " + getName());
message.setLevel(Message.LV_INFO);
message.setType("Test Message");
return message;
}
public void stop() {
setStatusStop();
}
}
3b. configure the component. Here we has two ways to configure.
--- Add follow message in conf/spy2servers.xml file
<beans:bean id="mySpyComponent" class="org.xmatthew.mypractise.SimpleSpyComponet"/>
--- Use annotation uner JDK5.0+. it is quit easy and simple only add @AlertComponent(name = "compnentName") to your impleneted class.
@SpyComponent(name = "mySpyComponent")
public class SimpleSpyComponent extends AbstractSpyComponent {
Note if you using annotation follow configure should add in spy2servers.xml file
<annotation-driven />
<context:component-scan base-package="org.xmatthew.mypractise"/>
4. to implement a very simple alert component
4a. Here we are going to develop very easy alert component, it will print the received message detail on the console screen.
the first step is let our class to extends from org.xmatthew.spy2servers.core.AbstractAlertComponent
public class SimpleAlertComponet extends AbstractAlertComponent{
@Override
protected void onAlert(Message message) {
}
public void startup() {
}
public void stop() {
}
}
Now we go to implenent our code. the complent code as bellow.
public class SimpleAlertComponet extends AbstractAlertComponent{
//the message received will invoke the method onAlert(Message message)
@Override
protected void onAlert(Message message) {
if (isActive()) {
System.out.println(message); //here we only print to the screen
}
}
public void startup() {
setStatusRun();
}
public void stop() {
setStatusStop();
}
}
4b. configure the component. Here we has two ways to configure.
--- Add follow message in conf/spy2servers.xml file
<beans:bean id="myAlertComponent" class="org.xmatthew.mypractise.SimpleAlertComponet"/>
--- Use annotation uner JDK5.0+. it is quit easy and simple only add @AlertComponent(name = "compnentName") to your impleneted class.
@AlertComponent(name = "myAlertComponent")
public class SimpleAlertComponent extends AbstractAlertComponent {
Note if you using annotation follow configure should add in spy2servers.xml file
<annotation-driven />
<context:component-scan base-package="org.xmatthew.mypractise"/>
5. use SimpleAlertRule to set the message alert rule.
If we want the messaeg from mySpyComponent componet will route the myAlertComponent component .just add follow content in the spy2servers.xml file
<core-component>
<simple-alertRule>
<channel>
<from value="mySpyComponent"/>
<to value="myAlertComponent" />
</channel>
</simple-alertRule>
</core-component>
6. now we conuld run the spy2servers to see what will happen.
under windows:
%spy2servers.home%/conf/start.bat
under linux:
${spy2servers.home}/conf/start.sh
7. (Optional) to implement a very simple MessageAlertChannelActiveAwareComponent component. this compnent is very useful if you want to trace back the message route history as the component will aware all the messages route action.
Here we have a very simple example. just log out the message routing info to the screen.
@org.xmatthew.spy2servers.annotation.MessageAlertChannelActiveAwareComponent (name = "SimpleChannelAwareComponent")
public class SimpleChannelAwareComponent extends AbstractComponent implements
MessageAlertChannelActiveAwareComponent {
private boolean started;
private List<MessageAlertChannel> channels = Collections.synchronizedList(new LinkedList<MessageAlertChannel>());
public List<MessageAlertChannel> getChannels() {
return channels;
}
//each message route action will invoke the method
//onMessageAlertChannelActive(MessageAlertChannel channel)
public void onMessageAlertChannelActive(MessageAlertChannel channel) {
if (!started) {
return;
}
channels.add(channel);
printChannel(channel);
}
public void startup() {
started = true;
setStatusRun();
}
public void stop() {
started = false;
setStatusStop();
}
private void printChannel(MessageAlertChannel channel) {
if (channel != null) {
System.out.println("channel aware component say:");
System.out.print("spyComponent is: ");
System.out.println(channel.getSpyComponent());
System.out.print("alertComponent is: ");
System.out.println(channel.getAlertComponent());
System.out.print("message is: ");
System.out.println(channel.getMessage());
}
}
}
Author: Matthew Xie Blog
site: 孤独键盘手
Email: ant_miracle@163.com
|