Skip to content

Instantly share code, notes, and snippets.

XLNT.getBucket('mobile.endorsements.profilepromo', 'memberId:23', function(err, bucket) {
if(bucket === 'action_promo') {
// serve view based JSON for action_promo
} else if(bucket === 'hero_promo') {
// serve view based JSON for hero_promo
} else {
// no call to action
}
}
@akgupta
akgupta / event_source_connection_actor_handling.java
Last active April 18, 2020 09:38
Use of Play’s EventSource API to accept an incoming connection in the application controller and assigning it to be managed by an Akka Actor
// Client A connects to the server and is assigned connectionIdA
public Result listen() {
return ok(EventSource.whenConnected(eventSource -> {
String connectionId = UUID.randomUUID().toString();
// construct an Akka Actor with the new EventSource connection identified by a random connection identifier
Akka.system().actorOf(
ClientConnectionActor.props(connectionId, eventSource),
connectionId);
}));
}
@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());
@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)));
}
}
"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)
Source<EventSource.Event, ActorRef> actorRefPoweredEventSource =
Source.actorRef(100, OverflowStrategy.fail());
Pair<ActorRef, Source<EventSource.Event, NotUsed>> actorRefEventSourcePair =
actorRefPoweredEventSource.preMaterialize(_materializer);
ActorRef eventSourceActor = actorRefEventSourcePair.first();
Source<EventSource.Event, ?> eventSource = actorRefEventSourcePair.second();
String connectionIdentifier = UUID.randomUUID().toString();
_actorSystem.actorOf(
ClientConnection.getProps(
connectionIdentifier, eventSourceActor, _actorSystem.getScheduler()
),
connectionIdentifier
);
return ok().chunked(eventSource.via(EventSource.flow())).as(Http.MimeTypes.EVENT_STREAM);
_eventSourceActor.tell(event(Json.toJson(new ClientMessage(_connectionId, "heartbeat"))), getSelf())