# Message Reliability - At-Least-Once-Delivery - Sending message in TodoList pattern

**URL:** <https://discuss.akka.io/t/message-reliability-at-least-once-delivery-sending-message-in-todolist-pattern/7069>\
**Category:** Akka Libraries\
**Created:** [August 26, 2020, 5:23am UTC](https://discuss.akka.io/t/message-reliability-at-least-once-delivery-sending-message-in-todolist-pattern/7069 "2020-08-26T05:23:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![agapic](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/agapic/32/2003_2.png) [@agapic](https://discuss.akka.io/u/agapic)\
**Post date:** [August 26, 2020, 5:23am UTC](https://discuss.akka.io/t/message-reliability-at-least-once-delivery-sending-message-in-todolist-pattern/7069/1 "2020-08-26T05:23:10Z")

</div>

How do I actually send a message to the TodoService actor in the Message Reliability example?

```auto
 EntityTypeKey<TodoService.Command> entityTypeKey1 =
          EntityTypeKey.create(TodoService.Command.class, "todo");

      EntityRef<TodoService.Command> entityRef1 =
          ClusterSharding.get(system).entityRefFor(entityTypeKey1, "123");
      CompletionStage<Response> r = entityRef1.ask(replyTo -> new UpdateTodo("123", "tire", false, replyTo),
                                                   Duration.ofSeconds(3));

```

I get the below error:

```auto
java.lang.ClassCastException: com.agapic.TodoService$UpdateTodo cannot be cast to akka.actor.typed.delivery.ConsumerController$Command

```

Source: [Reliable delivery in Typed, #20984 by patriknw · Pull Request #28155 · akka/akka · GitHub](https://github.com/akka/akka/pull/28155/files#r477031068)

@patriknw: you mentioned the below: could you explain what I was trying to do vs. how it is intended to be used?

“However, I think you misunderstand how it is intended to be used. You try to use it with two levels of Sharding, one for the TodoList and one for the TodoService. The docs say " A single ShardingProducerController per ActorSystem (node) can be shared for sending to all entities of a certain entity type.” [Reliable delivery • Akka Documentation](https://doc.akka.io/docs/akka/current/typed/reliable-delivery.html#sharding)"

---

<div class="post-metadata">

**Author:** ![patriknw](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/patriknw/32/89_2.png) [@patriknw](https://discuss.akka.io/u/patriknw)\
**Post date:** [August 27, 2020, 8:56am UTC](https://discuss.akka.io/t/message-reliability-at-least-once-delivery-sending-message-in-todolist-pattern/7069/2 "2020-08-27T08:56:43Z")

</div>

That is the example shown in [Reliable delivery • Akka Documentation](https://doc.akka.io/docs/akka/current/typed/reliable-delivery.html#sharding)

That example is using an EntityTypeKey with identifier “todo” for the `TodoList`.

```auto
EntityTypeKey<ConsumerController.SequencedMessage<TodoList.Command>> entityTypeKey =
    EntityTypeKey.create(ShardingConsumerController.entityTypeKeyClass(), "todo");

```

The identifier of the EntityTypeKey must be unique so you can’t reuse “todo” for `TodoService`. The EntityTypeKey for the TodoService should be:

```auto
EntityTypeKey<TodoService.Command> entityTypeKey1 =
          EntityTypeKey.create(TodoService.Command.class, "todoService");

```

I think that is the reason for the ClassCastException.

However, I don’t think you should access the `TodoService` via ClusterSharding.

The documentation says:

> The producer and `ShardingProducerController` actors are supposed to be local so that these messages are fast and not lost. This is enforced by a runtime check.

At the example that would be:

```auto
// Start a single instance of the TodoService actor
ActorRef<TodoService.Command> todoService = context.spawn(TodoService.create(producerController), "producer");

```

That ActorRef can then be used for sending messages to the TodoService. For example, from a HTTP route or similar

```auto
import akka.actor.typed.scaladsl.AskPattern;

CompletionStage<TodoService.Response> response = AskPattern.ask(todoService, 
  (ActorRef<TodoService.Response> replyTo) -> new TodoService.UpdateTodo("123", "tire", false, replyTo), 
  Duration.ofSeconds(3), system.scheduler());

```
