Hi Kevin,

Looks like a nice way to reduce boilerplate.
I spotted two potential problems:
- it should probably only accept methods with 1 parameter
- it doesn't handle sub-classes, which might be published by the eventStream

Thanks for sharing.
/Patrik


On Fri, Jun 6, 2014 at 5:49 PM, Kevin Wong <[email protected]>
wrote:

> What does everyone think of this?  My only concerns are the possibility of
> an increased actor memory footprint, and of course performance.
>
> import java.lang.annotation.ElementType;
> import java.lang.annotation.Retention;
> import java.lang.annotation.RetentionPolicy;
> import java.lang.annotation.Target;
> import java.lang.reflect.Method;
> import java.util.concurrent.TimeUnit;
> import scala.concurrent.duration.Duration;
> import scala.concurrent.duration.FiniteDuration;
> import akka.actor.ActorRef;
> import akka.actor.Cancellable;
> import akka.actor.UntypedActor;
> import akka.event.Logging;
> import akka.event.LoggingAdapter;
> import com.google.common.collect.ImmutableMap;
> import com.google.common.collect.ImmutableMap.Builder;
> public abstract class EasyUntypedActor extends UntypedActor
> {
> @Retention(RetentionPolicy.RUNTIME)
> @Target(ElementType.METHOD)
> public @interface EasySubscribe {
> }
>
> private ImmutableMap<Class<?>,Method> methodsByMessageClass;
>
> @Override
> public void preStart() throws Exception
> {
> super.preStart();
> Method[] methods = getClass().getMethods();
> Builder<Class<?>,Method> builder = ImmutableMap.builder();
> for (Method method : methods)
> {
> EasySubscribe annotation = method.getAnnotation(EasySubscribe.class);
> if ( annotation != null ) {
> Class<?>[] parameterTypes = method.getParameterTypes();
> if ( parameterTypes.length <= 0 )
> throw new RuntimeException("@Subscribe method "+method+
> " does not have a parameter.");
> final Class<?> msgClass = parameterTypes[0];
> if ( shouldSubscribeToEventStream() )
> context().system().eventStream().subscribe(getSelf(),
> msgClass);
> builder.put(msgClass, method);
> };
> }
> methodsByMessageClass = builder.build();
> }
> /**
>  * Default is true.
>  */
> protected boolean shouldSubscribeToEventStream()
> {
> return true;
> }
> @Override
> public void onReceive(Object message) throws Exception
> {
> final Class<? extends Object> msgClass = message.getClass();
> if ( methodsByMessageClass.containsKey(msgClass) )
> methodsByMessageClass.get(msgClass).invoke(this, message);
> else
> unhandled(message);
> }
>
> protected LoggingAdapter log() {
> return Logging.getLogger(getContext().system(), this);
> }
>
> protected void publish(Object event) {
> context().system().eventStream().publish(event);
> }
> }
>
>  --
> >>>>>>>>>> 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.
>



-- 

Patrik Nordwall
Typesafe <http://typesafe.com/> -  Reactive apps on the JVM
Twitter: @patriknw

<http://www.scaladays.org/>

-- 
>>>>>>>>>>      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