For reference - cluster sharding can also be used on just a single node
(weird, but true ;-)).

It handles the situation well and also creates actors for ids that don’t
yet have an actor running etc.

—
Konrad `kto.so` Malawski
Akka <http://akka.io> @ Lightbend <http://lightbend.com>

On 30 August 2017 at 22:19:50, Wei Guo (SH-LTS) ([email protected]) wrote:



On Wednesday, August 30, 2017 at 8:33:00 PM UTC+8, Justin du coeur wrote:
>
> I'd recommend looking into Akka Cluster Sharding.  Even if you're not on a
> cluster, this is usually the most straightforward way to get messages
> routed consistently based on a key...
>
> On Wed, Aug 30, 2017 at 5:37 AM, Wei Guo (SH-LTS) <[email protected]>
> wrote:
>
>> Hi everyone,
>>
>> I'm a newbie for Akka, for load balance consideration, I created 5 actors
>> to handle the incoming requests, but one important requirement is that
>> different types of objects with same key(Symbol of Quote/Trade/RefData in
>> my code below) should be routed to same actor, after searching Akka doc, 
>> ConsistentHashingRoutingLogic
>> is a choice
>>
>> After running code with testing data, same type of objects were routed to
>> same actor, but for different types of objects(Quote,Trade, RefData with
>> same Symbol),  it doesn't work as i expected, they were routed to different
>> actors
>> eg:
>> Quote quote1,quote2;
>> quote1.setSymbol("001");            // quote1 & quote2 were routed to
>> same actor(h0)        -- no issue
>> quote2.setSymbol("001");
>>
>> Trade trade;
>> trade.setSymbol("001");      // trade was sent to different actor (h1)
>>                  -- problem
>>
>> RefData refData;
>> refData.setSymbol("001");    // refData was sent to different actor (h2)
>>           -- problem
>>
>> Can anyone provide any suggestion to route them into same actor ? Thanks
>> a lot.
>>
>> int nOfHisActor = 5;
>>
>> actorSystem = ActorSystem.create("lunar");
>> String[] pathArray = new String[nOfHisActor];
>> for (int i = 0; i < nOfHisActor; i++) {
>>     String path = "h" + i;
>>     pathArray[i] = "/user/" + path;
>>     actorSystem.actorOf(Props.create(HisMarketDataActor.class), path);
>> }
>> List<String> paths = Arrays.asList(pathArray);
>> hisInfoActor = actorSystem.actorOf(new 
>> ConsistentHashingGroup(paths).withHashMapper(o -> {
>>     if (o instanceof Quote) {
>>         return ((Quote) o).getSymbol();
>>     } else if (o instanceof Trade) {
>>         return ((Trade) o).getSymbol();
>>     } else if (o instanceof RefData) {
>>         return ((RefData) o).getSymbol();
>>     }
>>     return null;
>> }).props(), "price");
>>
>> --
>> >>>>>>>>>> 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 https://groups.google.com/group/akka-user.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
Thanks for your quick reply, Justin
In fact, the reason i use Akka here is just to replace the mode in our one
exiting Java-muti-thread project, I'll also look it Akka Cluster Sharding

And I just got an idea to solve this issue, I created the other object(with
key to save symbol, Object to save different type of class) to wrap these
different object, in each actor, parse each object with instanceof, which
solves my problem, event it is not good solution

// ConsistentHashingLogic

hisInfoActor = actorSystem.actorOf(new
ConsistentHashingGroup(paths).withHashMapper(o -> {
    if (o instanceof ActorMessage) {
        return ((ActorMessage) o).getKey();
    }
    return null;
}).props(), "price");



// new class to wrap these types of class

public ActorMessage(String symbol, Object object) {
    this.key = symbol;
    this.object = object;
}


// filter them in Actor

@Override
public Receive createReceive() {
    return receiveBuilder().
            match(ActorMessage.class, actorMessage ->
filterActorMessage(actorMessage)).
            match(MarketSessionInfo.class, marketSessionInfo ->
onMarketSessionInfo(marketSessionInfo)).
            build();
}

private void filterActorMessage(ActorMessage actorMessage) {
    Object actorObj = actorMessage.getObject();
    if (actorObj instanceof RefData) {
        onRefData((RefData) actorObj);
    } else if (actorObj instanceof Quote) {
        onQuote((Quote) actorObj);
    } else if (actorObj instanceof DataObject) {
        onQuoteExt((DataObject) actorObj);
    } else if (actorObj instanceof Trade) {
        onTrade((Trade) actorObj);
    }
}




--
>>>>>>>>>> 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 https://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].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to