# Sharding entity custom Stop msg and HashCodeNoEnvelopeMsgExtractor

**URL:** <https://discuss.akka.io/t/sharding-entity-custom-stop-msg-and-hashcodenoenvelopemsgextractor/8423>\
**Category:** Actors\
**Tags:** akka, akka-typed, akka-cluster\
**Created:** [June 29, 2021, 7:02am UTC](https://discuss.akka.io/t/sharding-entity-custom-stop-msg-and-hashcodenoenvelopemsgextractor/8423 "2021-06-29T07:02:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pawelkaczor](https://avatars.discourse-cdn.com/v4/letter/p/9dc877/32.png) [@pawelkaczor](https://discuss.akka.io/u/pawelkaczor)\
**Post date:** [June 29, 2021, 7:02am UTC](https://discuss.akka.io/t/sharding-entity-custom-stop-msg-and-hashcodenoenvelopemsgextractor/8423/1 "2021-06-29T07:02:24Z")

</div>

It seems that it is not possible to use custom Stop message together with HashCodeNoEnvelopeMessageExtractor:

```auto

sealed trait Message {
    def mpId: String
}

case class Stop() extends Message {
  def mpId: String = throw new UnsupportedOperationException()
}

Entity(MyEntity.typeKey("MyEntity")) { entityCtx =>
  MyEntity.behavior(entityCtx) 
}
.withStopMessage(Stop())
.withMessageExtractor(new HashCodeNoEnvelopeMessageExtractor[Message](config.numberOfShards) {
        def entityId(message: Message): String = message.mpId
})

java.lang.UnsupportedOperationException: null
	at MyEntityProtocol$Stop.mpId(MyEntity.scala:21)
	at MyService$$anon$1.entityId(MyService.scala:37)
	at MyService$$anon$1.entityId(MyService.scala:36)
	at akka.cluster.sharding.typed.internal.ExtractorAdapter.entityId(ClusterShardingImpl.scala:58)
	at akka.cluster.sharding.typed.internal.ClusterShardingImpl$$anonfun$1.isDefinedAt(ClusterShardingImpl.scala:168)
	at akka.cluster.sharding.ShardRegion$$anonfun$receive$2.applyOrElse(ShardRegion.scala:731)

```

---

<div class="post-metadata">

**Author:** ![pawelkaczor](https://avatars.discourse-cdn.com/v4/letter/p/9dc877/32.png) [@pawelkaczor](https://discuss.akka.io/u/pawelkaczor)\
**Post date:** [June 29, 2021, 8:36am UTC](https://discuss.akka.io/t/sharding-entity-custom-stop-msg-and-hashcodenoenvelopemsgextractor/8423/2 "2021-06-29T08:36:38Z")

</div>

I ended up passing invalid Stop("") msg instance to withStopMessage method just to test manual passivation.

---

<div class="post-metadata">

**Author:** ![johanandren](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/johanandren/32/85_2.png) [@johanandren](https://discuss.akka.io/u/johanandren)\
**Post date:** [June 30, 2021, 1:42pm UTC](https://discuss.akka.io/t/sharding-entity-custom-stop-msg-and-hashcodenoenvelopemsgextractor/8423/3 "2021-06-30T13:42:32Z")

</div>

Hmm, yeah, tricky corner case, but can’t think of a way around this somewhat awkward logic when not using envelopes.

The stop message is not actually ever sent between nodes, so does not need an entity id to be directed to, it is sent by the local shard to one of its children, but since it must be a part of the sharded actor protocol you can easily end up like this.

---

<div class="post-metadata">

**Author:** ![pawelkaczor](https://avatars.discourse-cdn.com/v4/letter/p/9dc877/32.png) [@pawelkaczor](https://discuss.akka.io/u/pawelkaczor)\
**Post date:** [June 30, 2021, 1:56pm UTC](https://discuss.akka.io/t/sharding-entity-custom-stop-msg-and-hashcodenoenvelopemsgextractor/8423/4 "2021-06-30T13:56:28Z")

</div>

Thanks for the explanation. I suppose it would be beneficial to add this explanation to the docs.

---

<div class="post-metadata">

**Author:** ![johanandren](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/johanandren/32/85_2.png) [@johanandren](https://discuss.akka.io/u/johanandren)\
**Post date:** [June 30, 2021, 2:07pm UTC](https://discuss.akka.io/t/sharding-entity-custom-stop-msg-and-hashcodenoenvelopemsgextractor/8423/5 "2021-06-30T14:07:11Z")

</div>

Good suggestion!

> <https://github.com/akka/akka/pull/30351>
>
> References https://discuss.akka.io/t/sharding-entity-custom-stop-msg-and-h…ashcodenoenvelopemsgextractor/8423
