带有后台作业的Grails Mahout插件(quartz2)



我是Grails的新手。我正在使用Mahout推荐插件来创建推荐引擎。我正在学习本教程。它对我来说很好。

现在我要做的是,我只想使用后台作业来完成这个Mahout推荐。这样用户就可以根据自己喜欢的内容自动推荐我使用Quartz 2.x Scheduler进行后台作业。如何使用Mahout作为背景工作??

在本教程中,您可以看到我创建了一个PreferenceController.groovy,其中添加了userID、itemID和preference编号。

我的Prefrence.groovy域类文件——

package com.rbramley.mahout
import org.apache.commons.lang.builder.HashCodeBuilder
class Preference implements Serializable {
   long userId
   long itemId
   float prefValue
   static constraints = {
       userId()
       itemId()
       prefValue range: 0.0f..5.0f
   }
   boolean equals(other) {
      if(!(other instanceof Preference)) {
         return false
      }
    other.userId == userId && other.itemId == itemId
   }
   int hashCode() {
      def builder = new HashCodeBuilder()
      builder.append userId
      builder.append itemId
      builder.toHashCode()
   }
   static mapping = {
      id composite: ['userId', 'itemId']
      version false
   }
  }

和我的PreferenceController.groovy

package com.rbramley.mahout
import org.springframework.dao.DataIntegrityViolationException
class PreferenceController {
   static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
   def index() {
       redirect(action: "list", params: params)
   }
   def list(Integer max) {
      params.max = Math.min(max ?: 10, 100)
      [preferenceInstanceList: Preference.list(params), preferenceInstanceTotal: Preference.count()]
   }
   def create() {
       [preferenceInstance: new Preference(params)]
   }
   def save() {
       def preferenceInstance = new Preference(params)
       if (!preferenceInstance.save(flush: true)) {
          render(view: "create", model: [preferenceInstance: preferenceInstance])
          return
       }
    flash.message = message(code: 'default.created.message', args: [message(code: 'preference.label', default: 'Preference'), preferenceInstance.id])
    redirect(action: "show", id: preferenceInstance.id)
    }
    def show(Long id) {
      def preferenceInstance = Preference.get(id)
      if (!preferenceInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'preference.label', default: 'Preference'), id])
        redirect(action: "list")
        return
    }
    [preferenceInstance: preferenceInstance]
  }
  def edit(Long id) {
    def preferenceInstance = Preference.get(id)
    if (!preferenceInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'preference.label', default: 'Preference'), id])
        redirect(action: "list")
        return
    }
    [preferenceInstance: preferenceInstance]
  }
  def update(Long id, Long version) {
    def preferenceInstance = Preference.get(id)
    if (!preferenceInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'preference.label', default: 'Preference'), id])
        redirect(action: "list")
        return
    }
    if (version != null) {
        if (preferenceInstance.version > version) {
            preferenceInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                      [message(code: 'preference.label', default: 'Preference')] as Object[],
                      "Another user has updated this Preference while you were editing")
            render(view: "edit", model: [preferenceInstance: preferenceInstance])
            return
        }
    }
    preferenceInstance.properties = params
    if (!preferenceInstance.save(flush: true)) {
        render(view: "edit", model: [preferenceInstance: preferenceInstance])
        return
    }
    flash.message = message(code: 'default.updated.message', args: [message(code: 'preference.label', default: 'Preference'), preferenceInstance.id])
    redirect(action: "show", id: preferenceInstance.id)
}
def delete(Long id) {
    def preferenceInstance = Preference.get(id)
    if (!preferenceInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'preference.label', default: 'Preference'), id])
        redirect(action: "list")
        return
    }
    try {
        preferenceInstance.delete(flush: true)
        flash.message = message(code: 'default.deleted.message', args: [message(code: 'preference.label', default: 'Preference'), id])
        redirect(action: "list")
    }
    catch (DataIntegrityViolationException e) {
        flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'preference.label', default: 'Preference'), id])
        redirect(action: "show", id: id)
    }
}
}

假设我已经在数据库中手动添加了一些数据。现在,当用户点击推荐控制器并输入特定的用户ID时,它将显示推荐。但我想做这项背景工作建议应自动建议给所有用户,无需任何人工干预

如果我理解你的问题,你需要一个可以从控制器运行的按需作业。要做到这一点,请创建一个没有任何触发器的作业,然后您可以从控制器手动触发它,并将您的参数传递给它。这将在后台触发作业。在执行定义作业需要做什么的过程中,您还可以将所需的任何服务注入到该作业中。

控制器:

BackgroundJob.triggerNow([id:params.id,userId:userId]) 

作业

class BackgroundJob {
    static triggers = {}
    def execute(context) {
        def id = context.mergedJobDataMap.get('id')
        def userId = context.mergedJobDataMap.get('userId')
        ...
    }
}

相关内容

  • 没有找到相关文章

最新更新