Fire Plugin OnChange/onConfigChange Event at Run Time Manually

26 / Sep / 2012 by Kushal Likhi 1 comments

Hi,

 

Suppose you have implemented some logic in your application which updates the Config at runtime.

 

Now after you have updated the config, you should notify the plugins about configChanges such that they reload their configurations. This can be done by firing the onConfigChange Event of the plugins.

 

Similarly you can also fire the onChange event of the plugin(s) whenever required.

 

We can do this via the pluginManager Bean. the Code is As follows:
[java]
//Bean of plugin manager, it can be autowired by name in controllers/services etc. or can be fetched via the ApplicationContext:getBean() method.
def pluginManager
def grailsApplication

//We will notify Config change to all plugins
void notifyConfigChangeToAllPlugins(){
pluginManager.allPlugins.each {
it.notifyOfEvent(GrailsPlugin.EVENT_ON_CONFIG_CHANGE, grailsApplication.config) //Second parameter is Source of event, here we have just passed the ConfigObject for simplicity.
}
}

[/java]

 

Now, We will fire the event for a particular plugin:
[java]

void notifyConfigChangeToPlugin(String name){
pluginManager.getGrailsPlugin(name).notifyOfEvent(GrailsPlugin.EVENT_ON_CONFIG_CHANGE, grailsApplication.config) //Second parameter is Source of event, here we have just passed the ConfigObject for simplicity.
}
}

[/java]

 

Similarly If we want to fire the OnChange event then we can do as follows:
[java]
void notifyOnChangeToAllPlugins(){
pluginManager.allPlugins.each {
it.notifyOfEvent(GrailsPlugin.EVENT_ON_CHANGE, myChangedArtifact) //Second parameter is Source of event, here we have just passed the object that has changed in simple terms, and its better if this object is an Artifact, as many plugins call the method .isArtifactOfType on this object as this object is passed in the "event" Map.
}
}

[/java]

 

 

Hope It Helps!!
Kushal Likhi

FOUND THIS USEFUL? SHARE IT

comments (1 “Fire Plugin OnChange/onConfigChange Event at Run Time Manually”)

  1. xuan

    hi, may i know where is the exact place to put void notifyConfigChangeToAllPlugins() ?
    i’m using grails mail plugin and i wish to override the mail config which include the host, port, etc in config.groovy by the data i fetched from mysql database. I had tried inserting this pluginManager.getGrailsPlugin(‘mail’).notifyOfEvent(GrailsPlugin.EVENT_ON_CONFIG_CHANGE, Holders.config) in OnChange Event and also OnConfigChange Event but still failed to override the mail config in config.groovy
    i’m new to grails, may i know where is the exact place of inserting the code above or where to find the pluginManager?

    Reply

Leave a Reply to xuan Cancel reply

Your email address will not be published. Required fields are marked *