How to store result of quartz job in session?

邮差的信 提交于 2019-12-11 04:18:52

问题


I have installed Quartz plugin in my Grails 2.1 application. Every 5 minutes one job is triggered that will calculate some numbers. The numbers are being shown on the side bar of every page. The calculated results will change frequently and my goal is when users refresh their screen they can see the new result on their sidebar. Right now my approach is to send ajax call to a controller and grab the result from the database and render it on the screen.

Is there any way to store (cache) the calculated result from the JOB somewhere other than database so that my views can use them without executing query each time users click on a link or refresh the screen?

I thought about session but not sure its even possible outside of normal web request or its a good solution at all. Is there any solution or alternative approach? Thanks

class MonitoringJob {
    def calculatorService
    def name  = 'Monitoring'
    def group = 'ApplicationGroup'
    static triggers = {
        simple name: 'mySimpleTrigger', startDelay: 60000, repeatInterval: 30000
    }

    def execute() {
        def results = calculatorService.runCalculators()
            //something like this:   session.results  =result
    }
}

回答1:


Services in Grails are by default singletons, so the calculatorService accessed in the Quartz job will be the same calculatorService accessed in other classes in which it is wired (e.g., controllers).

Just store the values at the class level within your calculatorService and add an accessor method for the controllers to retrieve the data.

class CalculatorService {

    ConcurrentHashMap cache = [:]

    def runCalculators() {
        // Do calculations
        cache.result1 = calculatedResult1
        // etc...
    }

    def retrieveCalculation(String key) {
        cache[(key)]
    }
}



回答2:


You can use grailsApplication. Just inject grailsApplication into your job, then save the value with an assignment grailsApplication.config.calcResults = calculatorService.runCalculators

You may want to initialize the grailsApplication.config.calcResults value in Bootstrap.groovy as well so it's available before the job runs for the first time.



来源:https://stackoverflow.com/questions/12319037/how-to-store-result-of-quartz-job-in-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!