Don't like ads? PRO users don't see any ads ;-)
Guest

gpars actors debug 1

By: dewarim on Mar 27th, 2012  |  syntax: Groovy  |  size: 1.77 KB  |  hits: 60  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class LuceneActor extends DefaultActor{
  2.  // shortened version  
  3.  protected void act() {
  4.         loop() {
  5.             react { command ->
  6.                 try {
  7.                     def result = null
  8.                     log.debug("LuceneActor received: $command")
  9.                     switch (command.type) {
  10.                         case CommandType.ADD_TO_INDEX: addToIndex(command); break
  11.                         case CommandType.REMOVE_FROM_INDEX: removeFromIndex(command); break
  12.                         case CommandType.UPDATE_INDEX: removeFromIndex(command); addToIndex(command); break
  13.                         case CommandType.SEARCH: result = search(command); break;
  14.                     }
  15.                     result = result ?: 'finished'
  16.                     log.debug("reply & finish")
  17.                     reply result
  18.                 }
  19.                 catch (Exception e) {
  20.                     log.debug("Failed to act on command:", e)
  21.                 }
  22.             }
  23.         }
  24.     }
  25.  
  26. // -----------------------------------------
  27. // calling code in a Grails service:
  28. static LuceneActor lucene
  29. static {
  30.         lucene = new LuceneActor()
  31.         lucene.start()
  32. }
  33.  
  34. void doStuff(){
  35.   def cmd = new IndexCommand(indexable: indexable, repository: database, type: CommandType.ADD_TO_INDEX)
  36.  lucene.sendAndWait(cmd)
  37. }
  38. // -----------------------------------------
  39.  
  40. // result:
  41. /*
  42. java.lang.IllegalStateException: The actor cannot accept messages at this point.
  43.         at groovyx.gpars.actor.Actor.createActorMessage(Actor.java:288)
  44.         at groovyx.gpars.actor.AbstractLoopingActor.send(AbstractLoopingActor.java:241)
  45.         at groovyx.gpars.actor.impl.MessageStream.sendAndWait(MessageStream.java:115)
  46.         at vs.lucene.LuceneService.addToIndex(LuceneService.groovy:40)
  47. */