查看文章 |
如何创建一个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 openQRMIn 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" pluginPlugins 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 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" pluginNow 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">The whole ../src/plugins/myservice/etc/myservice-plugin.xml file should now look like this : <?xml version="1.0" ?> 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 EventListenerNow 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 Re-compiling and re-installing the "myservice" pluginPlease re-compile and re-install the "myservice" plugin in the same way as after part 2Calling the custom event via the qrm-cliAfter 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 : ...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 : ...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 3You 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 |
最近读者: