Created
September 20, 2012 13:02
-
-
Save mathieuancelin/3755757 to your computer and use it in GitHub Desktop.
Using CDI in Play2 app (from http://mathieuancelin.github.com/Dev/2012/09/19/play2-cdi/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package controllers; | |
| import play.*; | |
| import play.mvc.*; | |
| import views.html.*; | |
| import services.*; | |
| import javax.enterprise.inject.*; | |
| import javax.enterprise.context.*; | |
| import javax.inject.*; | |
| import javax.enterprise.event.*; | |
| import play.data.*; | |
| @ApplicationScoped | |
| public class Application extends Controller { | |
| public static class MessageForm { | |
| public String message; | |
| } | |
| @Inject HelloService helloService; | |
| @Inject Event<String> messageEvent; | |
| public Result index() { | |
| return ok(index.render(helloService.hello())); | |
| } | |
| public Result sendMessage() { | |
| Form<MessageForm> form = form(MessageForm.class); | |
| Form<MessageForm> filledForm = form.bindFromRequest(); | |
| if(form.hasErrors()) { | |
| return badRequest("You need to pass a 'message' value!"); | |
| } else { | |
| messageEvent.fire( "{\"message\" : \"" + filledForm.get().message + "\"}" ); | |
| return ok(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sbt._ | |
| import Keys._ | |
| import PlayProject._ | |
| object ApplicationBuild extends Build { | |
| val appName = "play2-cdi" | |
| val appVersion = "1.0-SNAPSHOT" | |
| val appDependencies = Seq( | |
| // Add your project dependencies here, | |
| "org.jboss.weld.se" % "weld-se" % "1.1.9.Final" | |
| ) | |
| val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings( | |
| // Add your own project settings here | |
| ) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import play.*; | |
| import org.jboss.weld.environment.se.ShutdownManager; | |
| import org.jboss.weld.environment.se.Weld; | |
| import org.jboss.weld.environment.se.WeldContainer; | |
| public class Global extends GlobalSettings { | |
| private WeldContainer weld; | |
| @Override | |
| public void onStart(Application app) { | |
| weld = new Weld().initialize(); | |
| } | |
| @Override | |
| public void onStop(Application app) { | |
| shutdown(weld); | |
| } | |
| @Override | |
| public <A> A getControllerInstance(Class<A> clazz) { | |
| return weld.instance().select(clazz).get(); | |
| } | |
| private void shutdown(WeldContainer weld) { | |
| ShutdownManager shutdownManager = weld.instance().select(ShutdownManager.class).get(); | |
| shutdownManager.shutdown(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package services; | |
| import javax.enterprise.inject.*; | |
| import javax.enterprise.context.*; | |
| @ApplicationScoped | |
| public class HelloService { | |
| public String hello() { | |
| return "Hello world!"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @(message: String) | |
| @main("Yeah") { | |
| <h1>@message</h1> | |
| <form id="filter"> | |
| Message: <input type="text" value="" id="message"> | |
| <input type="submit" value="Send"> | |
| </form> | |
| <div id="events"></div> | |
| <script type="text/javascript" charset="utf-8"> | |
| $(window).ready(function() { | |
| var feed = new EventSource('@routes.StreamController.stream()') | |
| feed.onmessage = function(e) { | |
| var data = JSON.parse(e.data) | |
| $('#events').prepend("<p>" + data.message + "</p>") | |
| } | |
| $('#filter').submit( function(e) { | |
| e.preventDefault() | |
| $.post('@routes.Application.sendMessage()', {message: $('#message').val()}) | |
| }) | |
| }) | |
| </script> | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Routes | |
| # This file defines all application routes (Higher priority routes first) | |
| # ~~~~ | |
| # Home page | |
| GET / @controllers.Application.index() | |
| GET /scala @controllers.ScalaController.index() | |
| POST /post @controllers.Application.sendMessage() | |
| POST /postfromscala @controllers.ScalaController.sendMessage() | |
| GET /stream @controllers.StreamController.stream() | |
| # Map static resources from the /public folder to the /assets URL path | |
| GET /assets/*file controllers.Assets.at(path="/public", file) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package controllers | |
| import play.api._ | |
| import play.api.mvc._ | |
| import play.api.data.Forms._ | |
| import play.api.data._ | |
| import javax.inject._ | |
| import javax.enterprise.context._ | |
| import javax.enterprise.event._ | |
| import services._ | |
| class ScalaController extends Controller { | |
| val form = Form( "message" -> text ) | |
| @Inject var helloService: HelloService = _ | |
| @Inject var evt: Event[String] = _ | |
| def index() = Action { implicit request => | |
| Ok( views.html.index( helloService.hello() ) ) | |
| } | |
| def sendMessage() = Action { implicit request => | |
| form.bindFromRequest.fold ( | |
| formWithErrors => BadRequest( "You need to pass a 'message' value!" ), | |
| { maybeValue => | |
| evt.fire( "{\"message\" : \"" + maybeValue + "\"}" ); | |
| Ok | |
| } | |
| ) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package services | |
| import play.api.Play.current | |
| import play.api.libs._ | |
| import play.api.libs.iteratee._ | |
| import play.api.libs.concurrent._ | |
| import java.util.concurrent._ | |
| import scala.concurrent.stm._ | |
| import play.api.libs.json._ | |
| import javax.enterprise.context._ | |
| import javax.enterprise.event ._ | |
| @ApplicationScoped | |
| class SseService { | |
| val noise: Enumerator[JsValue] = Enumerator.generateM[JsValue] { | |
| Promise.timeout( | |
| Some( JsObject( List( "message" -> JsString( "System message : " + System.currentTimeMillis() ) ) ) ), | |
| 2000 | |
| ) | |
| } | |
| val hubEnumerator = Enumerator.imperative[JsValue]() | |
| val hub = Concurrent.hub[JsValue]( hubEnumerator >- noise ) | |
| def listenJsEvent( @Observes evt: JsValue ) = { | |
| hubEnumerator.push( evt ) | |
| } | |
| def listenStringEvent( @Observes evt: String ) = { | |
| hubEnumerator.push( Json.parse( evt ) ) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package controllers | |
| import play.api._ | |
| import play.api.mvc._ | |
| import play.api.data.Forms._ | |
| import play.api.data._ | |
| import javax.inject._ | |
| import javax.enterprise.context._ | |
| import services._ | |
| import play.api.Play.current | |
| import play.api.libs._ | |
| import play.api.libs.iteratee._ | |
| import play.api.libs.concurrent._ | |
| class StreamController extends Controller { | |
| @Inject var sseService: SseService = _ | |
| def stream() = Action { implicit request => | |
| Ok.feed( sseService.hub.getPatchCord().through( EventSource( ) ) ).as( "text/event-stream" ) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment