Java example code for Getting Started
* WiP on Java version of new Hello World Done: - implementation for first 3 sections - tests for first 2 sections To do: - formatting - tests for section 3 - section 4 and 5 - hooking the fragments into the docs themselves * Finish tutorial_03 conversion * Converted tutorial 04 * Convert step 5 scala->java * Reformat Java code (thx IntelliJ IDEA) * Format using 2 spaces Really it'd be neat to have something like scalariform for the Java code, too * Make fields 'private final' * Make message classes 'public static final' * Use primitive long/double where possible * sender() -> getSender() * Static import of org.junit.Assert.... * Final actor fields * println() the instruction to hit ENTER to quit * Use jdocs package for quickstart, too * Use helper methods for message handling * Compare doubles specifying a delta * Keep tutorial steps independent
This commit is contained in:
parent
20b5e6b955
commit
f064d1321a
30 changed files with 2848 additions and 0 deletions
119
akka-docs-new/src/test/java/jdocs/tutorial_3/DeviceGroup.java
Normal file
119
akka-docs-new/src/test/java/jdocs/tutorial_3/DeviceGroup.java
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
|
||||
*/
|
||||
package jdocs.tutorial_3;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
|
||||
import akka.actor.AbstractActor;
|
||||
import akka.actor.ActorRef;
|
||||
import akka.actor.Props;
|
||||
import akka.actor.Terminated;
|
||||
import akka.event.Logging;
|
||||
import akka.event.LoggingAdapter;
|
||||
|
||||
import jdocs.tutorial_3.Device;
|
||||
import jdocs.tutorial_3.DeviceManager;
|
||||
|
||||
//#device-group-full
|
||||
public class DeviceGroup extends AbstractActor {
|
||||
private final LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);
|
||||
|
||||
final String groupId;
|
||||
|
||||
public DeviceGroup(String groupId) {
|
||||
this.groupId = groupId;
|
||||
}
|
||||
|
||||
//#device-group-register
|
||||
public static Props props(String groupId) {
|
||||
return Props.create(DeviceGroup.class, groupId);
|
||||
}
|
||||
//#device-group-register
|
||||
|
||||
public static final class RequestDeviceList {
|
||||
final long requestId;
|
||||
|
||||
public RequestDeviceList(long requestId) {
|
||||
this.requestId = requestId;
|
||||
}
|
||||
}
|
||||
|
||||
public static final class ReplyDeviceList {
|
||||
final long requestId;
|
||||
final Set<String> ids;
|
||||
|
||||
public ReplyDeviceList(long requestId, Set<String> ids) {
|
||||
this.requestId = requestId;
|
||||
this.ids = ids;
|
||||
}
|
||||
}
|
||||
//#device-group-register
|
||||
//#device-group-register
|
||||
//#device-group-register
|
||||
//#device-group-remove
|
||||
|
||||
final Map<String, ActorRef> deviceIdToActor = new HashMap<>();
|
||||
//#device-group-register
|
||||
final Map<ActorRef, String> actorToDeviceId = new HashMap<>();
|
||||
//#device-group-register
|
||||
|
||||
@Override
|
||||
public void preStart() {
|
||||
log.info("DeviceGroup {} started", groupId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postStop() {
|
||||
log.info("DeviceGroup {} stopped", groupId);
|
||||
}
|
||||
|
||||
private void onTrackDevice(DeviceManager.RequestTrackDevice trackMsg) {
|
||||
if (this.groupId.equals(trackMsg.groupId)) {
|
||||
ActorRef deviceActor = deviceIdToActor.get(trackMsg.deviceId);
|
||||
if (deviceActor != null) {
|
||||
deviceActor.forward(trackMsg, getContext());
|
||||
} else {
|
||||
log.info("Creating device actor for {}", trackMsg.deviceId);
|
||||
deviceActor = getContext().actorOf(Device.props(groupId, trackMsg.deviceId), "device-" + trackMsg.deviceId);
|
||||
//#device-group-register
|
||||
getContext().watch(deviceActor);
|
||||
actorToDeviceId.put(deviceActor, trackMsg.deviceId);
|
||||
//#device-group-register
|
||||
deviceIdToActor.put(trackMsg.deviceId, deviceActor);
|
||||
deviceActor.forward(trackMsg, getContext());
|
||||
}
|
||||
} else {
|
||||
log.warning(
|
||||
"Ignoring TrackDevice request for {}. This actor is responsible for {}.",
|
||||
groupId, this.groupId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private void onDeviceList(RequestDeviceList r) {
|
||||
getSender().tell(new ReplyDeviceList(r.requestId, deviceIdToActor.keySet()), getSelf());
|
||||
}
|
||||
|
||||
private void onTerminated(Terminated t) {
|
||||
ActorRef deviceActor = t.getActor();
|
||||
String deviceId = actorToDeviceId.get(deviceActor);
|
||||
log.info("Device actor for {} has been terminated", deviceId);
|
||||
actorToDeviceId.remove(deviceActor);
|
||||
deviceIdToActor.remove(deviceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(DeviceManager.RequestTrackDevice.class, this::onTrackDevice)
|
||||
.match(RequestDeviceList.class, this::onDeviceList)
|
||||
.match(Terminated.class, this::onTerminated)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
//#device-group-remove
|
||||
//#device-group-register
|
||||
//#device-group-full
|
||||
Loading…
Add table
Add a link
Reference in a new issue