This was the subject of the original link I sent.  I actually wanted to have a 
lot more than just a message ID - workId, senderId, recipientId, etc... - and I 
didn't want to burden my "business logic" actors with having to worry about it.

The solution was, as Patrik has suggested, to use use an envelope.  I just 
spiced it up with a bunch of implicits and such to make the pain of using the 
envelope go away.

I'll plug it again, because I really think it might help solve your problem: 
http://blog.primal.com/using-scala-implicits-to-implement-a-messaging-protocol/

On Dec 18, 2013, at 2:37 AM, Patrik Nordwall <[email protected]> wrote:

> 
> On Tue, Dec 17, 2013 at 9:39 PM, Shawn <[email protected]> wrote:
> Gosh, I must have done a poor job making my point! My question is probably 
> more about Scala but since I'm looking for ideas on modeling related data 
> (messages) specifically for Akka, I posted here. 
> 
> So, my original issue is really asking how to define common data between 
> messages. My first reply to this thread was a naive (scala newbie) attempt at 
> that.
> 
> Ideally I'd like to say something like
> 
> case class MyMessage(foo:String) extends MessageWithId
> 
> ... and have it act as if MyMessage was a case class defined with an id 
> property and preferably some implementation (implementation important, but 
> not for this discussion) to generate the id. So my question is about whether 
> we can define messages in something akin to an OO inheritance hierarchy. 
> 
> To include id in the automatic case class equals/hashCode/toString/copy the 
> id must be part of the first parameter list. You can sometimes benefit from 
> having a `trait MessageWithId { def id: ID }`, but that is for being able to 
> operate on id-carrying messages without having to know all message types.
> 
> I often find it more flexible to use an envelope:
> case class Envelope(id: ID, message: Any)
> 
> Cheers,
> Patrik
>  
> 
> Alternately, perhaps someone can enlighten me as to why I should always 
> prefer to define this id property (repeatedly) on each case class message?
> 
> e.g. MyMessage(id:something, foo:String), MyOtherMsg(id:something, 
> bar:String), ...
> 
> 
> 
> I do.
> 
> It's quite possible that this thread has gone awry :)  I believe that the 
> original issue was probably meaning to say something like, "Are you sure that 
> using nanoseconds as unique identifiers isn't a terrible idea?".  Of course, 
> to this I would certainly say "yes".  And if that was what the original 
> question was, I probably tore this thread way off course :)
> 
>> 
>> Cheers,
>> √
>>  
>> 
>>> 
>>> Cheers,
>>> √
>>>  
>>> 
>>> def genId(implicit uniqueifier: Long) = s"${uniqueifier}-${System.nanoTime}"
>>> 
>>> Now, give each Actor an implicit uniqueifier, and then put in a guarantee 
>>> that no single Actor will call this more than once per nano second. e.g. 
>>> create enough Actors to do the work, loop until the new Id isn't the same 
>>> as the last one, etc...
>>> 
>>> Or, give a each Actor a uniqueifier, and let it use a monotonically 
>>> increasing postfix identifier.  e.g. gen the uniqueifier as a UUID and then 
>>> let them increment a Long until it gets exhausted, then pick another UUID, 
>>> or crash the Actor, or...
>>> 
>>> Vary these themes as you see fit.  "Solved" here means that I'm not being 
>>> all that inventive; for non-generalized situations, the above should work 
>>> perfectly fine.
>>> 
>>>> 
>>>> Regards,
>>>> 
>>>> Roland
>>>> 
>>>>> 
>>>>>> 
>>>>>> -- 
>>>>>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>> >>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>>>> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
>>>>>> --- 
>>>>>> You received this message because you are subscribed to a topic in the 
>>>>>> Google Groups "Akka User List" group.
>>>>>> To unsubscribe from this topic, visit 
>>>>>> https://groups.google.com/d/topic/akka-user/84Mb4pEp4wQ/unsubscribe.
>>>>>> To unsubscribe from this group and all its topics, 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/groups/opt_out.
>>>>> 
>>>>> 
>>>>> -- 
>>>>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>> >>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>>> >>>>>>>>>> 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/groups/opt_out.
>>>> 
>>>> 
>>>> 
>>>> 
>>>> Dr. Roland Kuhn
>>>> Akka Tech Lead
>>>> Typesafe – Reactive apps on the JVM.
>>>> twitter: @rolandkuhn
>>>> 
>>>> 
>>>> 
>>>> -- 
>>>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>>>> >>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
>>>> --- 
>>>> You received this message because you are subscribed to a topic in the 
>>>> Google Groups "Akka User List" group.
>>>> To unsubscribe from this topic, visit 
>>>> https://groups.google.com/d/topic/akka-user/84Mb4pEp4wQ/unsubscribe.
>>>> To unsubscribe from this group and all its topics, 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/groups/opt_out.
>>> 
>>> 
>>> -- 
>>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>>> >>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>> >>>>>>>>>> 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/groups/opt_out.
>>> 
>>> 
>>> 
>>> -- 
>>> Cheers,
>>> √
>>> 
>>> Viktor Klang
>>> Director of Engineering
>>> Typesafe
>>> 
>>> Twitter: @viktorklang
>>> 
>>> -- 
>>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>>> >>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
>>> --- 
>>> You received this message because you are subscribed to a topic in the 
>>> Google Groups "Akka User List" group.
>>> To unsubscribe from this topic, visit 
>>> https://groups.google.com/d/topic/akka-user/84Mb4pEp4wQ/unsubscribe.
>>> To unsubscribe from this group and all its topics, 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/groups/opt_out.
>> 
>> 
>> -- 
>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>> >>>>>>>>>> Check the FAQ: http://akka.io/faq/
>> >>>>>>>>>> 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/groups/opt_out.
>> 
>> 
>> 
>> -- 
>> Cheers,
>> √
>> 
>> Viktor Klang
>> Director of Engineering
>> Typesafe
>> 
>> Twitter: @viktorklang
>> 
>> -- 
>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>> >>>>>>>>>> Check the FAQ: http://akka.io/faq/
>> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
>> --- 
>> You received this message because you are subscribed to a topic in the 
>> Google Groups "Akka User List" group.
>> To unsubscribe from this topic, visit 
>> https://groups.google.com/d/topic/akka-user/84Mb4pEp4wQ/unsubscribe.
>> To unsubscribe from this group and all its topics, 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/groups/opt_out.
> 
> 
> -- 
> >>>>>>>>>> Read the docs: http://akka.io/docs/
> >>>>>>>>>> Check the FAQ: http://akka.io/faq/
> >>>>>>>>>> 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/groups/opt_out.
> 
> 
> 
> -- 
> 
> Patrik Nordwall
> Typesafe -  Reactive apps on the JVM
> Twitter: @patriknw
> 
> 
> 
> -- 
> >>>>>>>>>> Read the docs: http://akka.io/docs/
> >>>>>>>>>> Check the FAQ: http://akka.io/faq/
> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
> --- 
> You received this message because you are subscribed to a topic in the Google 
> Groups "Akka User List" group.
> To unsubscribe from this topic, visit 
> https://groups.google.com/d/topic/akka-user/84Mb4pEp4wQ/unsubscribe.
> To unsubscribe from this group and all its topics, 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/groups/opt_out.

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: http://akka.io/faq/
>>>>>>>>>>      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/groups/opt_out.

Reply via email to