tomcat - Can I force a grails dev server to reload when any code recompiles? -


the short question is: can force grails 2.2 dev server reload (reinitializing singleton services, etc) whenever new code hot-compiled , swapped in?

let me make question clear example. let's

  1. i have singleton-scoped service called myservice. in @postconstruct function, initializes new object of type helper (and holds on reference).

  2. i run-app in dev mode, test things out, , then...

  3. i edit source code helper.groovy

what happens next is:

  • grails automatically recompiles helper.groovy
  • grails swaps new code running dev server
  • but new helper code isn't called because already-instantiated myservice object has handle on instance of old kind.

my current workaround keep dependency graph in head, , touch or trivially modify myservice every time change 1 of dependencies. i'd rather force dev server reload whenever new code swapped in.

so... is possible force dev server reload whenever code changes?

and bonus question: when change domain object, way can force dev server "follow" changes stop-app, clean, , run-app manually. can automate happen whenever it's required?

thanks!

critical update

i able looking for. laying down step wise:

requirement -
when helper class modified, service class should refreshed refer modified helper class.

setup

//src/groovy class samplehelper {     string name = 'heisenberg'      def saymyname(){         "$name"     } }  //grails-app/service import javax.annotation.postconstruct class sampleservice {     string cachedname      @postconstruct     def initit(){         cachedname = new samplehelper().saymyname()     }      def servicemethod() {         cachedname     } }  //controller class samplecontroller {     def sampleservice      def index() {         render sampleservice.servicemethod()     } } 

problem statement
when name in helper class updated gus, class refreshes service class still referencing old instance of samplehelper. result, name still shows heisenberg.

solution

  • watch helper class.
  • refresh service class onchange of helper class.

this can achieved using pluginator plugin in application gives flexibility watch file , onchange event.

  • install plugin.
  • add below applicationplugin.groovy under grails-app/conf.

as

class applicationplugin {     //watch helper class     def watchedresources = "file:./src/groovy/**/*.groovy"      //re-register bean (service class) onchange of helper class     //this can generalized more.     def onchange = { event ->         if (event.source) {             def beans = beans {                 sampleservice(sampleservice) { bean ->                     bean.autowire =  true                 }             }             if (event.ctx) {                 event.ctx.registerbeandefinition(                         "sampleservice",                         beans.getbeandefinition("sampleservice"))             }         }     } } 
  • problem statement should fixed after above steps followed.

note

  • this process might resource intensive if followed classes , artefacts.

old & valid answer

i unable recreate problem in grails 2.2.0. below setup have (correct me if wrong anywhere):

//src/groovy class myhelper{     def saymyname(){         "heisenberg"     } }  //service import javax.annotation.postconstruct class myservice {     def myhelper      @postconstruct     def initit(){         myhelper = new myhelper()     }      def servicemethod() {         myhelper.saymyname()     } }  //controller (to test) class mycontroller {     def myservice      def index() {         render myservice.servicemethod()     } } 

steps:

  • run initial setup. (run-app)
  • hit controller see "heisenberg"
  • modify myhelper.saymyname() return "walter white" instead of "heisenberg"
  • hit controller again , see "walter white"

observation:

  • i rather make myhelper bean , use (inject) in service class use.

entry resources.groovy follows:

beans = {     myhelper(com.example.myhelper) } 

service class becomes:

class myservice {     def myhelper      def servicemethod() {         myhelper.saymyname()     } } 
  • now, referring field in myhelper in service class give issue because bean has been instantiated in container

regarding bonus question:

i unable recreate problem either in grails 2.2.0

//domain class mydomain {     string name }  //controller action def add(){     def mydomain = new mydomain(name: 'testing').save(flush: true)     render mydomain.properties } 
  • change domain, add string email
  • save domain class.
  • modify action add email in mydomain (obviously new row created).
  • save controller.
  • hit action again.
  • see result.

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -