如何在会话中存储石英工作的结果? [英] How to store result of quartz job in session?

查看:91
本文介绍了如何在会话中存储石英工作的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Grails 2.1应用程序中安装了Quartz插件。每5分钟触发一次工作,计算一些数字。数字显示在每个页面的侧栏上。计算结果会经常更改,我的目标是当用户刷新屏幕时,他们可以在侧边栏上看到新的结果。
现在我的方法是向控制器发送ajax调用,并从数据库中获取结果并将其显示在屏幕上。

有什么方法可以将JOB的计算结果存储(缓存)到数据库以外的地方,这样我的视图就可以在每次用户点击链接或刷新屏幕?



我想到了会话,但并不确定它甚至可能在正常的Web请求之外,或者它是一个很好的解决方案。有没有解决方案或替代方法?
谢谢

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

def execute( ){
def results = calculatorService.runCalculators()
//像这样:session.results = result
}
}


解决方案

Grails中的服务默认为singletons,所以 calculatorService 将与其他有线连接的类(例如控制器)访问的 calculatorService 相同。



只需将类中的值存储在 calculatorService 中,并为控制器添加访问器方法以检索数据。

 类CalculatorService {

ConcurrentHashMap cache = [:]
$ b $ def runCalculators(){
//计算
cache.result1 = calculatedResult1
/ / etc ...
}

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


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
    }
}

解决方案

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)]
    }
}

这篇关于如何在会话中存储石英工作的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆