Akka-typed usage patterns

Good day. i`m new to akka so can you explain me some aspects of akka-typed usage?
as far as i concerned base feature of akka typed is creating actor with Type parameter wich represent types of messages wich this actor can process. i’m looking for a way to force to implement some method , say processIncomingMesage(<? extends SupportedProtocol> msg ), in class that exstends AbstractBehavior as many times as SupportedProtocol have concrete implementations of mesasges, so Behavior class may look like

public class SimpleDevice extend AbstractBehavior<SimpleDeviceProtocol>
@Override
private Behavior<SimpleDeviceProtocol> processIncomingMesage (final MonitoringProtocol.GetStateRequest getStateRequestMsg){
// processing message here
return Behaviors.same()
}
@Override
private Behavior<SimpleDeviceProtocol> processIncomingMesage (final SimpleManagementProtocol.TurnOnRequest turnOnRequestMsg){
// processing message here
return Behaviors.same()
}
    @Override
    public Receive<SimpleDeviceProtocol> createReceive() {
        final Receive<SimpleDeviceProtocol> receive
                = newReceiveBuilder().onMessageUnchecked(SimpleDeviceProtocol.class, this::handlerSelector).build();
        return receive;
    }

private <M> Behavior<SimpleDeviceProtocol> handlerSelector(final M m)
            throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        final Class<? extends Object> messageClass = m.getClass();
        final Object processIncomingMesageResult = this.getClass().getDeclaredMethod("processIncomingMesage"
                , messageClass).invoke(this,m);
        return (Behavior<SimpleDeviceProtocol>) processIncomingMesage;
    }

main idea is that somehow create SimpleDeviceProtocol that include all messages types from SimpleManagementProtocol and MonitoringProtocol , than somehow force to implement
method
private Behavior processIncomingMesage (final T message){}
for each processable message Type in SimpleDeviceProtocol

so we can create diferent types of actors wich can implements same protocols in diferent ways
and if later on we will extend protocol with say SimpleManagementProtocol.TurnOffRequest
than all behaviors classes that extends SimpleDeviceProtocol or another protocol that contains SimpleManagementProtocol must implement
private Behavior processIncomingMesage (final SimpleManagementProtocol.TurnOffRequest turnOffRequestMsg);
or may be threre is more simple aproach ?

1 Like

In general I think you should not start out with trying to abstract over message protocols, but aim for concrete sets of messages per behavior/actor.

Having several different actors supporting the same set of messages can be a fine design for some use cases but is likely not that common.

Doing reflection on messages is most certainly a bad idea, make sure you get a firm understanding of the basics of using the actor model before reaching for such power tools.

Making sure all messages in a protocol is covered is not possible in Java, however in Scala you can use a sealed super type and get a compiler error when you do not cover all possible messages in a behavior.