Grails Mahout插件与背景工作(quartz2)

时间:2013-07-17 07:11:54

标签: grails grails-plugin grails-2.0 mahout

我是Grails的新人。我正在使用Mahout Recommender Plugin来创建推荐引擎。我正在关注这个tutorial。这对我来说可以。

现在我想做什么,我只想使用后台作业执行此Mahout推荐。因此,用户可以根据自己喜欢的内容自动推荐。我正在使用Quartz 2.x Scheduler进行后台作业。如何使用Mahout作为后台作业?

在这个tutorial中,你可以看到我创建了一个PrefrenceController.groovy,我在其中添加了userID,itemID和prefrence number。

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

和我的PrefrenceController.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)
    }
}
}

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

1 个答案:

答案 0 :(得分:0)

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

控制器:

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')
        ...
    }
}