{"id":8793,"date":"2012-09-26T23:57:06","date_gmt":"2012-09-26T18:27:06","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8793"},"modified":"2012-09-26T23:57:06","modified_gmt":"2012-09-26T18:27:06","slug":"fire-plugin-onchangeonconfigchange-event-at-run-time-manually","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/fire-plugin-onchangeonconfigchange-event-at-run-time-manually\/","title":{"rendered":"Fire Plugin OnChange\/onConfigChange Event at Run Time Manually"},"content":{"rendered":"<p>Hi,<\/p>\n<p>&nbsp;<\/p>\n<p>Suppose you have implemented some logic in your application which updates the <strong>Config<\/strong> at runtime. <\/p>\n<p>&nbsp;<\/p>\n<p>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 <strong>onConfigChange<\/strong> Event of the plugins.<\/p>\n<p>&nbsp;<\/p>\n<p>Similarly you can also fire the <strong>onChange<\/strong> event of the plugin(s) whenever required.<\/p>\n<p>&nbsp;<\/p>\n<p>We can do this via the <strong>pluginManager<\/strong> Bean. the Code is As follows:<br \/>\n[java]<br \/>\n\/\/Bean of plugin manager, it can be autowired by name in controllers\/services etc. or can be fetched via the ApplicationContext:getBean() method.<br \/>\ndef pluginManager<br \/>\ndef grailsApplication<\/p>\n<p>\/\/We will notify Config change to all plugins<br \/>\nvoid notifyConfigChangeToAllPlugins(){<br \/>\n    pluginManager.allPlugins.each {<br \/>\n            it.notifyOfEvent(GrailsPlugin.EVENT_ON_CONFIG_CHANGE, grailsApplication.config) \/\/Second parameter is Source of event, here we have just passed the ConfigObject for simplicity.<br \/>\n    }<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p>&nbsp;<\/p>\n<p>Now, We will fire the event for a particular plugin:<br \/>\n[java]<\/p>\n<p>void notifyConfigChangeToPlugin(String name){<br \/>\n    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.<br \/>\n    }<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p>&nbsp;<\/p>\n<p>Similarly If we want to fire the OnChange event then we can do as follows:<br \/>\n[java]<br \/>\nvoid notifyOnChangeToAllPlugins(){<br \/>\n    pluginManager.allPlugins.each {<br \/>\n            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 &quot;event&quot; Map.<br \/>\n    }<br \/>\n} <\/p>\n<p>[\/java]<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Hope It Helps!!<br \/>\n<strong>Kushal Likhi<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, &nbsp; Suppose you have implemented some logic in your application which updates the Config at runtime. &nbsp; 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. &nbsp; Similarly you can also [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8793"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8793"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8793\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}