Skip to content

Instantly share code, notes, and snippets.

getContext().watch(_eventSourceActor);
@Override
public Receive createReceive() {
return receiveBuilder()
.match(
Terminated.class,
terminated -> {
if (terminated.getActor().equals(_eventSourceActor)) {
getContext().stop(self());
_eventSourceActor.tell(new Status.Success("close"), getSelf())
_eventSourceActor.tell(event(Json.toJson(new ClientMessage(_connectionId, "heartbeat"))), getSelf())
return ok().chunked(eventSource.via(EventSource.flow())).as(Http.MimeTypes.EVENT_STREAM);
String connectionIdentifier = UUID.randomUUID().toString();
_actorSystem.actorOf(
ClientConnection.getProps(
connectionIdentifier, eventSourceActor, _actorSystem.getScheduler()
),
connectionIdentifier
);
Pair<ActorRef, Source<EventSource.Event, NotUsed>> actorRefEventSourcePair =
actorRefPoweredEventSource.preMaterialize(_materializer);
ActorRef eventSourceActor = actorRefEventSourcePair.first();
Source<EventSource.Event, ?> eventSource = actorRefEventSourcePair.second();
Source<EventSource.Event, ActorRef> actorRefPoweredEventSource =
Source.actorRef(100, OverflowStrategy.fail());
"Hashed wheel timer #11327" #27780 prio=5 os_prio=0 tid=0x00007f73a8bba000 nid=0x27f4 sleeping[0x00007f7329d23000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at org.jboss.netty.util.HashedWheelTimer$Worker.waitForNextTick(HashedWheelTimer.java:445)
at org.jboss.netty.util.HashedWheelTimer$Worker.run(HashedWheelTimer.java:364)
at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)
at java.lang.Thread.run(Thread.java:745)
@akgupta
akgupta / client_connection_actor.java
Created September 18, 2016 19:00
Client Connection Actor forwards a message on the EventSource connection
public class ClientConnectionActor extends UntypedActor {
public static Props props(String connectionId, EventSource eventSource) {
return Props.create(ClientConnectionActor.class, () -> new ClientConnectionActor(connectionId, eventSource));
}
public void onReceive(Object msg) throws Exception {
if (msg instanceof ClientMessage) {
eventSource.send(event(Json.toJson(clientMessage)));
}
}
@akgupta
akgupta / actor_connection_send_message.java
Created September 18, 2016 18:54
Send a message to the Actor managing an EventSource connection
// User B sends a message to User A
// We identify the Actor which manages the connection on which User A is connected (connectionIdA)
ActorSelection actorSelection = Akka.system().actorSelection("akka://application/user/" + connectionIdA);
// Send B's message to A's Actor
actorSelection.tell(new ClientMessage(data), ActorRef.noSender());