Email: dev@concena.com       Twitter: @dbrucegrant
«Back

Nuxeo - Fire Events on Demand using REST

In my customer environments I have numerous agents (listeners that respond to timed events) that perform a variety of maintenance tasks. One example is an automated document locker. Documents added to locked folders are automatically locked after a specified period of time. When it comes to testing agents and running them on demand it's nice to be able to start them from the command line.

Are you looking for a simple working example of how to fire a Nuxeo event from a browser URL. This very basic version accepts the name of the event to be generated and then fires the event. Events fired using this approach are not tied to a specific document.

The EventOnDemand class is a restlet, defined to take a single parameter {eventName}. EventName is the name of the event to be fired. The relevant part of the restlet contribution is shown below.

<snip>

    <extension target="org.nuxeo.ecm.platform.ui.web.restAPI.service.PluggableRestletService" point="restlets">    
        
        <restletPlugin name="com.concena.EventOnDemand" class="com.concena.rest.api.EventOnDemand"
            enabled="true" useSeam="false">
            <urlPatterns>
                <urlPattern>/eventondemand/{eventName}</urlPattern>
            </urlPatterns>
        </restletPlugin>  

<snip>

The contribution above specifies the URL pattern to fire an event on demand, and defines the class that implements the firing code. The code to fire the event is...

public class EventOnDemand extends BaseStatelessNuxeoRestlet implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final Log log = LogFactory.getLog(EventOnDemand.class);

    @Override
    public void doHandleStatelessRequest (Request req, Response res) {
        
        // rest command line params
        // _eventName - the name of the event to fire!
        String _eventName = Utils.getRestVar(req, "eventName");
        
        // get EventProducer Service
        EventProducer eventProducer;
        try {
            eventProducer = Framework.getService(EventProducer.class);
        } catch (Exception e1) {
            log.error("*** Could not get the event producer service *** (error: " + e1.getMessage() + " )");
            return;
        }

        // prepare EventContext properties
        Map<String, Serializable> props = new HashMap<String, Serializable>();
        props.put("source", "eventOnDemand");

        // create simple EventContext
        EventContext ctx = new InlineEventContext(getUserPrincipal(null), props);

        // create the event from the context
        Event event = ctx.newEvent(_eventName);
        
        // fire the event
        try {
            eventProducer.fireEvent(event);
        } catch (ClientException e) {
            log.error("*** Could not fire the event - " + event.getName() + " *** (error: " + e.getMessage() + " )");
        }
    }
}

This code gets the _eventName parameter from the request, gets the EventProducer service, sets up an EventContext, and then fires the event. The call to Utils.getRestVar(req, "eventName") calls the getAttributes method of the request and returns the value of the parameter. "eventName" corresponds to the {eventName} in the urlpattern of the restlet contribution.

Next up - a sample listener that can be invoked on demand!

 

 

Comments
Trackback URL:

No comments yet. Be the first.

Recent Entries Recent Entries

RSS (Opens New Window)
Showing 1 - 5 of 15 results.
of 3