0

我是 Grails 的新手。我正在使用Mahout Recommender Plugin来创建推荐引擎。我正在关注本教程。这对我来说可以。

现在我想做的是,我只想使用后台作业来做这个 Mahout 推荐。这样用户就可以根据自己的喜好自动推荐。我将Quartz 2.x 调度程序用于后台作业目的。如何使用 Mahout 作为后台作业?

在本教程中,您可以看到我创建了一个 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 时,它将显示推荐。但我想将其作为背景工作。建议应自动向所有用户建议,无需任何人工干预。

4

1 回答 1

0

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

控制器:

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')
        ...
    }
}
于 2013-07-17T13:12:36.243 回答