百度首页 | 百度空间
 
查看文章
 
如何创建一个openqrm的插件 (part 3)
2008-04-21 10:28

Listening and responding to events (part 3)

Part 3 of this HowTo will give an overview and example how to listen to events in openQRM and connect them to custom commands. This mechanism is often used by plugins to execute custom commands on specific events.

Events in openQRM

In openQRM every action results in a specific event. Also error conditions are causing events within the openQRM-server.
The EventListener extension provided by the openQRM plugin API supports to hook custom commands to every event happening in the openQRM-server. It also allows plugins to define their own events.
As an example the Xen-plugin is using an EventListern listening for the "Resource.State.Operation.ShuttingSystem" and "Resource.State.Role.Deassigning" event to check if it needs to re-write the systems pxe-configuration file.

Please find a list of the possible events in the openQRM-server at :

http://openqrm.cvs.sourceforge.net/openqrm/base/openqrm-3.1.4/src/base/java/main/resources/events.properties?revision=1.1&view=markup

This file can also be found on an installed openQRM-server at :

/opt/qrm/java/webapp/WEB-INF/classes/events.properties

Adding a custom event to openQRM via the "myservice" plugin

Plugins can add their own events to the openQRM-server in an easy way by simply providing an "events.properties" file within the plugins /etc directory.
For the example "myservice" plugin we add a custom "MyService.Custom.Notice" event by creating ../src/plugins/myservice/etc/events.properties with the following content :
 MyService.Custom.Notice$level=Event
MyService.Custom.Notice$message=Custom MyService event arrived.

Please do not forget to add this new file to the INSTALL_FILES variable in the Makefile. The INSTALL_FILES in the Makefile should look like this :
INSTALL_FILES := etc/myservice.xml, -m 0700 etc/init.d, include, -m 700 sbin, etc/myservice-plugin.xml, etc/events.properties, web

Adding an EventListerner to the "myservice" plugin

Now we would like to listen for this "MyService.Custom.Notice" event. We simply add an EventListern to the plugins xml-configuration file to defines for which event to check and which command to run when the event happens.
Please edit ../src/plugins/myservice/etc/myservice-plugin.xml and add the following section :
 <extension id="MyServiceCustomListener" plugin-id="com.qlusters.qrm.plugins.core" point-id="EventListener">
<parameter id="eventKey" value="MyService.Custom.Notice"/>
<parameter id="execution">
<parameter id="type" value="exec"/>
<parameter id="resource" value="path=$QRM_SERVER_BASE_DIR/qrm/plugins/myservice/sbin/myservice-custom-cmd"/>
</parameter>
<parameter id="inTransaction" value="false"/>
</extension>

The whole ../src/plugins/myservice/etc/myservice-plugin.xml file should now look like this :
<?xml version="1.0" ?>
<!DOCTYPE plugin PUBLIC "-//JPF//Java Plug-in Manifest 0.2" "http://jpf.sourceforge.net/plugin_0_2.dtd">
<plugin id="com.qlusters.qrm.plugins.puppet-plugin" version="0.0.1" >
<requires>
<import plugin-id="com.qlusters.qrm.plugins.core"/>
</requires>
<extension plugin-id="com.qlusters.qrm.plugins.core" point-id="BootService" id="myservice">
<parameter id="name" value="myservice"/>
<parameter id="runlevel" value="35"/>
<parameter id="package" value="http://$QRM_SERVER_IP_ADDRESS/unsecure/myservice/myservice-client.tgz"/>
<parameter id="init" value="plugins/myservice/etc/init.d/myservice-client"/>
</extension>

<extension plugin-id="com.qlusters.qrm.plugins.core" point-id="MenuItem" id="MyService">
<parameter id="hierarchy" value="&lt;strong&gt;Management Tools&lt;/strong&gt;" />
<parameter id="key" value="navigation"/>
<parameter id="title" value="MyService"/>
<parameter id="description" value="Myservice web-page"/>
<parameter id="url" value="/unsecure/myservice/web/myservice.jsp"/>
<parameter id="composite" value="false"/>
<parameter id="item-id" value="Myservice web-page"/>
</extension>

<extension id="MyServiceCustomListener" plugin-id="com.qlusters.qrm.plugins.core" point-id="EventListener">
<parameter id="eventKey" value="MyService.Custom.Notice"/>
<parameter id="execution">
<parameter id="type" value="exec"/>
<parameter id="resource" value="path=$QRM_SERVER_BASE_DIR/qrm/plugins/myservice/sbin/myservice-custom-cmd"/>
</parameter>
<parameter id="inTransaction" value="false"/>
</extension>

</plugin>

This defines to listen for the "MyService.Custom.Notice" (set in "eventkey") and then run the command "$QRM_SERVER_BASE_DIR/qrm/plugins/myservice/sbin/myservice-custom-cmd" (set by "resource").

Creating the command-script connected to the EventListener

Now that we have created the EventListener we care about creating the custom command connected to our custom event. Please create ../src/plugins/myservice/sbin/myservice-custom-cmd with the following content :
#!/bin/bash

# this is a simple example custom command for the "howto create an openQRM plugin" howto
echo "MyService.Custom.Notice event happened in the openQRM-server"

Re-compiling and re-installing the "myservice" plugin

Please re-compile and re-install the "myservice" plugin in the same way as after part 2

Calling the custom event via the qrm-cli

After re-compiling and re-installing the "myservice" plugin the new created "MyService.Custom.Notice" event can be activated/called by adding it to the openQRM-servers event-list via the qrm-cli.

Adding a "MyService.Custom.Notice" event as a global event :
./qrm-cli -u qrm -p qrm event add -g -k "MyService.Custom.Notice"
in /var/log/qrm/qrmlog :
...
2007-11-06 12:20:18,478 DEBUG [EventsManager] (http-443-Processor1:) Event: [49] Custom MyService event arrived. Entities are: QRM
2007-11-06 12:20:18,478 DEBUG [EventListenerManager] (http-443-Processor1:) Calling to fireEventCreated with event [id=49:creationTime=Tue Nov 06 12:20:18 CET 2007:levelInt=4:key=MyService.Custom.Notice:]:
2007-11-06 12:20:18,485 DEBUG [EventListenerManager] (http-443-Processor1:) Calling to afterCommit with event [id=49:creationTime=Tue Nov 06 12:20:18 CET 2007:levelInt=4:key=MyService.Custom.Notice:]:
2007-11-06 12:20:19,093 DEBUG [ExecutableDelegate] (http-443-Processor1:) Run of /opt/qrm/sbin/exec_wrapper.sh QRM_SESSION="0.20774404329788887" -- $QRM_SERVER_BASE_DIR/qrm/plugins/myservice/sbin/myservice-custom-cmd returned:
Stdout:
MyService.Custom.Notice event happened in the openQRM-server

Stderr:
...
Adding a "MyService.Custom.Notice" event on behalf of a resource :
./qrm-cli -u qrm -p qrm event add --internal_cr_id 1 -k "MyService.Custom.Notice"
in /var/log/qrm/qrmlog :
...
2007-11-06 12:20:52,763 DEBUG [EventsManager] (http-443-Processor2:) Event: [50] Custom MyService event arrived. Entities are: Node 1
2007-11-06 12:20:52,763 DEBUG [EventListenerManager] (http-443-Processor2:) Calling to fireEventCreated with event [id=50:creationTime=Tue Nov 06 12:20:52 CET 2007:levelInt=4:key=MyService.Custom.Notice:]:
2007-11-06 12:20:52,771 DEBUG [EventListenerManager] (http-443-Processor2:) Calling to afterCommit with event [id=50:creationTime=Tue Nov 06 12:20:52 CET 2007:levelInt=4:key=MyService.Custom.Notice:]:
2007-11-06 12:20:53,492 DEBUG [ExecutableDelegate] (http-443-Processor2:) Run of /opt/qrm/sbin/exec_wrapper.sh QRM_SESSION="0.8713933908322177" -- $QRM_SERVER_BASE_DIR/qrm/plugins/myservice/sbin/myservice-custom-cmd returned:
Stdout:
MyService.Custom.Notice event happened in the openQRM-server

Stderr:
...
Please notice that when the event was added to openQRM on behalf of a resource it is possible to use additional commandline parameters for the custom script. Those extra parameters are defined in the ../src/plugins/myservice/etc/myservice-plugin.xml file and can be used by simply changing the parameter line :
  <parameter id="resource" value="path=$QRM_SERVER_BASE_DIR/qrm/plugins/myservice/sbin/myservice-custom-cmd"/>

to
  <parameter id="resource" value="path=$QRM_SERVER_BASE_DIR/qrm/plugins/myservice/sbin/myservice-custom-cmd ${resource.id} ${resource.ipString}"/>

Then the myservice-custom-cmd will be executed with the resource ip and id as the first 2 commandline parameters.

Summary (part 3)

Part 3 of this howto explained how to listen to events in the openQRM-server, how to create custom events via a plugin and how to connect specific events to custom commands by the example of a "MyService.Custom.Notice" event connected to a plugin command (myservice-custom-cmd). This mechanism is especially useful for the integration of third-party components because it makes it easy to map third-party-events to openQRM-events which then can be handled automatically.

.. to be continued

Get the files used for part 3

You can download all files used in for this howto (part 3) packed in a .tgz at :

http://downloads.openqrm.net/contrib/plugin-howto/myservice-plugin-1.2.tgz

类别:Openqrm | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码:
 

     

©2008 Baidu