Skip to content

Instantly share code, notes, and snippets.

View mathieuancelin's full-sized avatar

Mathieu ANCELIN mathieuancelin

View GitHub Profile
object Streams extends Controller {
def stream = Action {
val operations: Enumerator[JsValue] = Enumerator.generateM[JsValue] {
Promise.timeout(
Some( Json.obj(
"id" -> UUID.randomUUID().toString(),
"amount" -> Random.nextInt(1000),
"access" -> if (Random.nextBoolean) "public" else "private"
) ),
case class Operation(id: String, amount: Int, access: String)
object Application extends Controller {
val format = Json.format[Operation]
def userFeed(role: String, lower: Int, high: Int) = Action {
val enumerator: Enumerator[Array[Byte]] = WS.url("http://host:port/stream").withTimeout(-1).getStream
package controllers
import play.api.mvc.{Action, Controller}
import play.api.libs.iteratee.{Concurrent, Enumeratee}
import play.api.libs.json.{Json, JsValue}
import play.api.libs.EventSource
import play.api.libs.ws.WSEnumerator
import models._
import play.api.libs.concurrent.Execution.Implicits._
@mathieuancelin
mathieuancelin / WSEnumerator.scala
Last active December 21, 2015 10:48
Scala class to transform an HTTP GET stream into Enumerator[Whatever]
package play.api.libs.ws
import play.api.libs.iteratee.{Enumeratee, Concurrent, Enumerator}
import play.api.libs.concurrent.Execution.Implicits._
import com.ning.http.client._
import com.ning.http.client.AsyncHandler.STATE
import play.api.Logger
import scala.concurrent.{Future, Promise}
import static reactor.Fn.$;
@Inject
Service service;
Reactor reactor = R.create();
reactor.on($("parse"), new Consumer<Event<String>>() {
public void call(Event<String> ev) {
service.handleEvent(ev);
import sbt._
import Keys._
import play.Project._
object ApplicationBuild extends Build {
val appName = "test"
val appVersion = "1.0-SNAPSHOT"
val appDependencies = Seq(
@mathieuancelin
mathieuancelin / Couch.scala
Last active December 17, 2015 14:09
Couchbase client
package controllers
import collection.JavaConversions._
import collection.mutable.ArrayBuffer
import java.net._
import com.couchbase.client.CouchbaseClient
import play.api.mvc._
import play.api._
public void lookup() {
while (Thread.currentThread() == m_logTestThread) {
ServiceReference logServiceRef =
m_context.getServiceReference(LogService.class.getName());
if (logServiceRef != null) {
try {
LogService logService =
(LogService) m_context.getService(logServiceRef);
if (logService != null) {
logService.log(LogService.LOG_INFO, "ping");
import org.junit.Assert;
import java.lang.ref.WeakReference;
public class MemoryLeakChecker<T> {
private static final int MAXGC = 50;
private final WeakReference<T> reference;
private final int calls;
Modules.create('ModuleA', function(ModuleA) {
ModuleA.hello = function() {
console.log("Hello from module A");
};
ModuleA.setupModule = function() {
console.log("Setup ModuleA");
};
ModuleA.messageReceived = function(msg) {
console.log("Received in ModuleA %s", msg);
};