=doc #24435 copy mutable maps before sharing them as messages in java (#24447)

* =doc #24435 copy mutable maps before sharing them as messages in java

* fix rename

* Update DeviceGroup.java

* Update DeviceGroup.java
This commit is contained in:
Konrad `ktoso` Malawski 2018-01-30 23:20:15 +09:00 committed by GitHub
parent cdecd8b6fe
commit c023d51367
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View file

@ -139,8 +139,14 @@ public class DeviceGroup extends AbstractActor {
//#query-added
private void onAllTemperatures(RequestAllTemperatures r) {
// since Java collections are mutable, we want to avoid sharing them between actors (since multiple Actors (threads)
// modifying the same mutable data-structure is not safe), and perform a defensive copy of the mutable map:
//
// Feel free to use your favourite immutable data-structures library with Akka in Java applications!
Map<ActorRef, String> actorToDeviceIdCopy = new HashMap<>(this.actorToDeviceId);
getContext().actorOf(DeviceGroupQuery.props(
actorToDeviceId, r.requestId, getSender(), new FiniteDuration(3, TimeUnit.SECONDS)));
actorToDeviceIdCopy, r.requestId, getSender(), new FiniteDuration(3, TimeUnit.SECONDS)));
}
@Override