Last active
August 29, 2015 14:24
-
-
Save aya-eiya/cf67a2e6b0e10fa9c716 to your computer and use it in GitHub Desktop.
Groovyのちょっとしたこと「Grails3のAsyncを試してみる」 ref: http://qiita.com/aya_eiya/items/24ee197fbd38290c125e
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.eiya.aya.grails.sample | |
class AsyncController { | |
def fooService | |
def index() { | |
log.info 'start index' | |
fooService.sayFoo(Hero.get(params.id?:1)) | |
log.info 'end index' | |
return 'foo' | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import footest.* | |
class BootStrap { | |
def fooService | |
def init = { servletContext -> | |
def clz = new Clazz(id:1,name:'archer').save(flush:true) | |
new Hero(id:1,name:'Shiro Emiya',masterName:'Rin',clazz:clz).save(flush:true) | |
clz = new Clazz(id:2,name:'saber').save(flush:true) | |
new Hero(id:2,name:'Artoria Pendragon',masterName:'Shiro',clazz:clz).save(flush:true) | |
fooService.changeMaster() | |
} | |
def destroy = { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.eiya.aya.grails.sample | |
class Clazz { | |
Long id | |
String name | |
String toString(){"$name($id)"} | |
static constraints = { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
org.hibernate.LazyInitializationException: could not initialize proxy - no Session |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task { | |
log.info 'start task foo' | |
Thread.currentThread().sleep(5000L) | |
try{ | |
log.info ((h.clazz.name=='saber')?"Is dinner ready yet $h.masterName?":"$h") // error by no session | |
}catch(e){ | |
log.error 'error',e // <--------------- 出してるのココね。 | |
} | |
log.info 'end task foo' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package footest | |
import grails.transaction.Transactional | |
import static grails.async.Promises.* | |
@Transactional | |
class FooService{ | |
def changeMaster(){ | |
log.info 'start changeMaster' | |
task { | |
log.info 'start task changeMaster' | |
Hero.withNewSession{ | |
def sclz = Clazz.findByName('saber') | |
def saber = Hero.findByClazz(sclz) | |
['Caster','Rin'].each{mstr-> | |
Hero.withTransaction{ | |
Thread.currentThread().sleep(15000L) | |
try{ | |
saber.masterName = mstr | |
saber.save(flush:true) | |
log.info "saber's master changed to $mstr" | |
}catch(e){ | |
log.error 'error',e | |
} | |
} | |
} | |
} | |
log.info 'end task changeMaster' | |
} | |
log.info 'end changeMaster' | |
} | |
def sayFoo(Hero h){ | |
log.info 'start sayFoo' | |
h.discard() | |
task { | |
log.info 'start task foo' | |
Hero.withNewTransaction{ | |
h.attach() | |
Thread.currentThread().sleep(5000L) | |
try{ | |
log.info ((h.clazz.name=='saber')?"Is dinner ready yet $h.masterName?":"I'm here. $h.clazz") // error session failed | |
}catch(e){ | |
log.error 'error',e | |
} | |
log.info 'end task foo' | |
} | |
log.info 'end sayFoo' | |
} | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.eiya.aya.grails.sample | |
import grails.transaction.Transactional | |
import static grails.async.Promises.* | |
@Transactional | |
class FooService{ | |
def sayFoo(Hero h){ | |
log.info 'start sayFoo' | |
h.discard() // dettach from main thread's session | |
task { | |
log.info 'start task foo' | |
Hero.withNewSession{ // use withNewTransaction in case you need transaction. | |
h.attach() // attach with async thread's new session | |
Thread.currentThread().sleep(5000L) | |
try{ | |
log.info ((h.clazz.name=='saber')?"Is dinner ready yet $h.masterName?":"I'm here. $h.clazz") // now OK | |
}catch(e){ | |
log.error 'error',e | |
} | |
log.info 'end task foo' | |
} | |
log.info 'end sayFoo' | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.eiya.aya.grails.sample | |
class Hero { | |
Long id | |
String name | |
String masterName | |
Clazz clazz | |
String toString(){"$masterName x $clazz[$name]"} | |
static constraints = { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment