Wiki Spaces
Documentation
Projects
Resources
Get Help from Others
Q&A: Ask OpenMRS
Discussion: OpenMRS Talk
Real-Time: IRC Chat | Slack
This module creates easy handle points for other modules to hook onto. When an event occurs in openmrs it will notify all registered/subscribed listeners.
View Source: https://github.com/openmrs/openmrs-module-event
Checkout Source: https://github.com/openmrs/openmrs-module-event
Download: https://addons.openmrs.org/#/show/org.openmrs.module.event
An enum on Event.Action contains the possible actions of CREATED, UPDATED, RETIRED, UNRETIRED, VOIDED, UNVOIDED, PURGED
The module's domain objects must be subclasses of OpenmrsObject.
If you wish for retired/unretired or voided/unvoided events to be fired, they should implement Retireable or Voidable respectively.
In your module activator or anywhere else in your module code:
Event.subscribe(Class, String, EventListener); // The String argument can be any of the values in the Event.Action Enum or Event.setSubscription(SubscribableEventListener); // spring callable, see below
or in your Spring moduleApplicationContext:
<bean class="org.openmrs.eventbus.Event"> <property name="subscription"><bean class="org.your.module.package.SubscribableEventListenerImpl"></bean></property> </bean>
A SubscribableEventListener specifies the list of Classes and list of Actions that it supports.
The module uses apache's ActiveMQ messaging server, your EventListener class should extend EventListener and its onMessage(Message message) will be called
Event.unsubscribe(Class, Action, EventListener); or Event.unsetSubscription(SubscribableEventListener); // just because its cute
By default, event module uses embedded ActiveMQ instance.
If you would like to use standalone ActiveMQ instance, simply set activeMQ.externalUrl Global property with a value of your desired ActiveMQ instance and restart module.
Example ip address: 172.17.0.1:61616
// A simple event listener that just keeps a count of all created, updated and purged items public class CountEventListener implements EventListener { private int createdCount = 0; private int updatedCount = 0; private int purgedCount = 0; public int getCreatedCount() { return createdCount; } public void getUpdatedCount() { return updatedCount; } public int getPurgedCount() { return purgedCount; } @Override public void onMessage(Message message) { try { MapMessage mapMessage = (MapMessage) message; if (Action.CREATED.toString().equals(mapMessage.getString("action"))) createdCount++; else if (Action.UPDATED.toString().equals(mapMessage.getString("action"))) updatedCount++; else if (Action.PURGED.toString().equals(mapMessage.getString("action"))) purgedCount++; //..... Keep counts for more event actions } catch (JMSException e) { System.out.println("Ooops! some error occurred"); } } }
2 Comments
Darius Jazayeri
At a glance, I'm not sure we want to be exposing these as static methods (Event.subscribe); for style shouldn't they be in a service or a bean or something?
(I'm sure the 99.99% use case is to have a singleton Event object, so the static method will work fine.)
Ben Wolfe
I intentionally didn't put it into a service to make it easier to call. And making it a bean doesn't make it any more settable by sprint.