Switching to English...
Using the Java API with Akka might result in somewhat ugly code, especially if using the UntypedActor classes. If an actor has many types of messages it can receive, it may result in long "if instanceof" chains. The situation can be improved by using AbstractActors or even TypedActors in some cases.
There might be an alternative though. To make the code a bit prettier, one solution can be to loan the idea of traits from Scala. Java interfaces with default methods can be used to achieve similar behavior.
Let's say we have an Actor A with some messages [B, C, D] it expects. Instead of writing a long handleMessage method with all kinds of if statements, we can set the A to implement interface CTrait. The CTrait has a default method handleC which then handles the C typed message. The CTrait may also force the A to expose some methods so it can do it's job. Finally we need such a setup that handleC is called from A, preferably without writing any code in A. In abstract actor case, we could use the ReceiveBuilder so when A is created, we add the CTrait method there. Alternatively we could annotate the CTrait handleC (@Handle(C.class)?) and use reflection to call the handleC from somewhere (A?) without knowing what handleC is.
These are a bit hacky solutions (to be honest, i have yet to figure out an elegant way). The usefulness of these kinds of traits is a bit questionable. The few benefits include that we don't need any "if instanceof [B,C,D]". Also one or more traits can be plugged in to any actor and we can avoid inheritance (A extends CHandlingActor) which will result to trouble. CTrait has not state so it can't mess with A unknowingly.
Using the Java API with Akka might result in somewhat ugly code, especially if using the UntypedActor classes. If an actor has many types of messages it can receive, it may result in long "if instanceof" chains. The situation can be improved by using AbstractActors or even TypedActors in some cases.
There might be an alternative though. To make the code a bit prettier, one solution can be to loan the idea of traits from Scala. Java interfaces with default methods can be used to achieve similar behavior.
Let's say we have an Actor A with some messages [B, C, D] it expects. Instead of writing a long handleMessage method with all kinds of if statements, we can set the A to implement interface CTrait. The CTrait has a default method handleC which then handles the C typed message. The CTrait may also force the A to expose some methods so it can do it's job. Finally we need such a setup that handleC is called from A, preferably without writing any code in A. In abstract actor case, we could use the ReceiveBuilder so when A is created, we add the CTrait method there. Alternatively we could annotate the CTrait handleC (@Handle(C.class)?) and use reflection to call the handleC from somewhere (A?) without knowing what handleC is.
These are a bit hacky solutions (to be honest, i have yet to figure out an elegant way). The usefulness of these kinds of traits is a bit questionable. The few benefits include that we don't need any "if instanceof [B,C,D]". Also one or more traits can be plugged in to any actor and we can avoid inheritance (A extends CHandlingActor) which will result to trouble. CTrait has not state so it can't mess with A unknowingly.
Comments
Post a Comment