18.8 Participating in Auto Reload Events - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari
Version: 3.1.9
18.8 Participating in Auto Reload Events
Monitoring Resources for Changes
Often it is valuable to monitor resources for changes and perform some action when they occur. This is how Grails implements advanced reloading of application state at runtime. For example, consider this simplified snippet from the GrailsServicesPlugin:class ServicesGrailsPlugin extends Plugin { … def watchedResources = "file:./grails-app/services/*Service.groovy" … void onChange( Map<String, Object> event) { if (event.source) { def serviceClass = grailsApplication.addServiceClass(event.source) def serviceName = "${serviceClass.propertyName}" beans { "$serviceName"(serviceClass.getClazz()) { bean -> bean.autowire = true } } } } }
watchedResources as either a String or a List of strings that contain either the references or patterns of the resources to watch. If the watched resources specify a Groovy file, when it is changed it will automatically be reloaded and passed into the onChange closure in the event object.The event object defines a number of useful properties:
event.source- The source of the event, either the reloadedClassor a SpringResourceevent.ctx- The SpringApplicationContextinstanceevent.plugin- The plugin object that manages the resource (usuallythis)event.application- TheGrailsApplicationinstanceevent.manager- TheGrailsPluginManagerinstance
ApplicationContext when one of the service classes changes.Influencing Other Plugins
In addition to reacting to changes, sometimes a plugin needs to "influence" another.Take for example the Services and Controllers plugins. When a service is reloaded, unless you reload the controllers too, problems will occur when you try to auto-wire the reloaded service into an older controller Class.To get around this, you can specify which plugins another plugin "influences". This means that when one plugin detects a change, it will reload itself and then reload its influenced plugins. For example consider this snippet from theServicesGrailsPlugin:def influences = ['controllers']
Observing other plugins
If there is a particular plugin that you would like to observe for changes but not necessary watch the resources that it monitors you can use the "observe" property:def observe = ["controllers"]def observe = ["*"]log property back to any artefact that changes while the application is running.
