Hi all,

Thanks a lot for confirming this, I was not sure about  closing over self 
ref because of this thread 
<https://groups.google.com/forum/#!topic/akka-user/jUMQ4pGLJf4>.
I have to dig deep on other part of code and find out the issue :).

On Sunday, November 9, 2014 2:18:16 PM UTC+5:30, √ wrote:
>
> Hi Jestan,
>
> The code looks fine, I think your problem is caused by some other code.
>
> On Sun, Nov 9, 2014 at 8:10 AM, Jestan Nirojan <[email protected] 
> <javascript:>> wrote:
>
>> Forgot to mention, the resource used by the worker actor is a singleton.
>>
>> Here is the updated code
>>
>> import akka.actor.{Actor, ActorLogging, ActorRef}
>>
>> import scala.concurrent.{ExecutionContext, Future}
>> import scala.util.{Failure, Success}
>>
>> case class Start(input: String)
>> case class Cmd(id: Long, source: String, input: String)
>> case class Result(id: Long, source: String, output: String)
>>
>> class Resource {
>>   def process(cmd: Cmd)(implicit ec: ExecutionContext, ref: ActorRef) {
>>     val result = Future {
>>       //some IO/Computation here
>>       val output = String.valueOf(System.currentTimeMillis())
>>       Result(cmd.id, cmd.source, output)
>>     }
>>
>>     result.onComplete {
>>       case Success(resp) => ref ! resp
>>       case Failure(error) => ref ! Result(cmd.id, cmd.source, "ERROR")
>>     }
>>   }
>> }
>>
>> object Resource {
>>   val INS = new Resource
>> }
>>
>> class Worker(name: String) extends Actor with ActorLogging {
>>   var sequence = 0L
>>
>>   def receive = {
>>     case start: Start =>
>>       log.debug(" Worker [{}] received start [{}]", name, start)
>>       sequence = sequence + 1
>>       val cmd = Cmd(sequence, name, start.input)
>>       log.debug(" Worker [{}] processing command [{}]", name, cmd)
>>       Resource.INS.process(cmd)
>>     case result: Result =>
>>       log.debug(" Worker [{}] received result [{}]", name, result)
>>     case _@other =>
>>       log.debug(" Worker [{}] received unexpected message [{}]", name, 
>> other)
>>   }
>> }
>>
>>
>>
>> On Sunday, November 9, 2014 12:29:11 PM UTC+5:30, Jestan Nirojan wrote:
>>>
>>>
>>> I have tried to minimize the code into following sample.
>>>
>>>
>>> import akka.actor.{Actor, ActorLogging, ActorRef}
>>>
>>> import scala.concurrent.{ExecutionContext, Future}
>>> import scala.util.{Failure, Success}
>>>
>>> case class Start(input: String)
>>> case class Cmd(id: Long, source: String, input: String)
>>> case class Result(id: Long, source: String, output: String)
>>>
>>> class Resource {
>>>   def process(cmd: Cmd)(implicit ec: ExecutionContext, ref: ActorRef) {
>>>     val result = Future {
>>>       //some IO/Computation here
>>>       val output = String.valueOf(System.currentTimeMillis())
>>>       Result(cmd.id, cmd.source, output)
>>>     }
>>>
>>>     result.onComplete {
>>>       case Success(resp) => ref ! resp
>>>       case Failure(error) => ref ! Result(cmd.id, cmd.source, "ERROR")
>>>     }
>>>   }
>>> }
>>>
>>> class Worker(name: String) extends Actor with ActorLogging {
>>>   val resource = new Resource
>>>   
>>>   var sequence = 0L
>>>
>>>   def receive = {
>>>     case start: Start =>
>>>       log.debug(" Worker [{}] received start [{}]", name, start)
>>>       sequence = sequence + 1
>>>       val cmd = Cmd(sequence, name, start.input)
>>>       log.debug(" Worker [{}] processing command [{}]", name, cmd)
>>>       resource.process(cmd)
>>>     case result: Result =>
>>>       log.debug(" Worker [{}] received result [{}]", name, result)
>>>     case _@other =>
>>>       log.debug(" Worker [{}] received unexpected message [{}]", name, 
>>> other)
>>>   }
>>> }
>>>
>>>
>>> But I am unable to reproduce the issue with actual/sample code locally.
>>> There are thousands of worker actors with unique names in production 
>>> environment and
>>> I could see, result is going to wrong actor and some time duplicate 
>>> results received by the actor.
>>>
>>> I am using Akka 2.2.3 / Scala 2.10
>>>
>>>
>>> On Sunday, November 9, 2014 12:31:20 AM UTC+5:30, √ wrote:
>>>>
>>>> Could you share a minimized, self-contained example that demonstrates 
>>>> the issue?
>>>> On Nov 8, 2014 7:53 PM, "Jestan Nirojan" <[email protected]> wrote:
>>>>
>>>>> I have a question about passing self actor ref to different thread / 
>>>>> future.
>>>>>
>>>>> Closing over sender could return a null or be the wrong ActorRef. 
>>>>> Likewise closing over self reference in a future will result similar 
>>>>> outcome?
>>>>>
>>>>> I am seeing wired outputs like, future sends response to wrong actor / 
>>>>> actor receiving duplicate responses.
>>>>>
>>>>> -- 
>>>>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>> >>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/
>>>>> current/additional/faq.html
>>>>> >>>>>>>>>> Search the archives: https://groups.google.com/
>>>>> group/akka-user
>>>>> --- 
>>>>> You received this message because you are subscribed to the Google 
>>>>> Groups "Akka User List" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send 
>>>>> an email to [email protected].
>>>>> To post to this group, send email to [email protected].
>>>>> Visit this group at http://groups.google.com/group/akka-user.
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>  -- 
>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>> >>>>>>>>>> Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
>> --- 
>> You received this message because you are subscribed to the Google Groups 
>> "Akka User List" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> Visit this group at http://groups.google.com/group/akka-user.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> -- 
> Cheers,
> √
>  

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to