Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Play 2.3 から 2.4 へアップデートした話

Play 2.3 から 2.4 へアップデートした話

Play meetup 2 で発表した内容です。https://playframeworkja.doorkeeper.jp/events/27023

tsuyoshi yoshizawa

July 05, 2015
Tweet

More Decks by tsuyoshi yoshizawa

Other Decks in Technology

Transcript

  1. Play 2.4 ʹͯ͠ ຊ൪؀ڥͰ͸໰୊ͳ͘Քಇ ࡉ͔͍όά͸ग़͍ͯͯɺطʹ 2.4.2 ·ͰϦϦʔε [2.4] Still a

    thread leak in development environment https://github.com/playframework/playframework/issues/4649
  2. 2.4 ͰԿ͕มΘͬͨͷ͔ • Dependency Injection ͷαϙʔτ • Plugin ͔Β Module

    ΁ • i18n ͷ࢓૊Έ͕มߋ • ઃఆΩʔͷมߋ What’s new in Play 2.4 https://playframework.com/documentation/2.4.x/Highlights24 Play 2.4 Migration Guide https://playframework.com/documentation/2.4.x/Migration24
  3. Dependency Injection Controller ʹίϯϙʔωϯτΛ DI ͢Δʹ͸ɺΠϯε λϯελΠϓΛ object ͔Β class

    ʹมߋ͢Δඞཁ͋Γ Guice Λ࢖ͬͯ DI ʢJSR 330 ϕʔεʣ import  javax.inject._   import  play.api.libs.ws._   class  MyComponent  @Inject()  (ws:  WSClient)  {      //  ...   }
  4. Module Plugin ͸ඇਪ঑ʹͳΓɺ DI ϕʔεͳ Module ʹ Ҡߦ͍ͯ͘͠ඞཁ͋Γ Play 2.4

    ͷ Module ͷ࡞Γํͱ Plugin ͔ΒͷҠߦ ʹ͍ͭͯ http://tototoshi.hatenablog.com/entry/2014/12/14/170745
  5. i18n Messages ʹ౉͢ܕ͕ Lang ͔Β Messages ࠃࡍԽରԠͯ͠ΔͱόʔδϣϯΞοϓ͕໘౗ case  class  Messages(lang:

     Lang,  messages:   MessagesApi) object  Messages  {      def  apply(key:  String,  args:  Any*)(implicit   messages:  Messages):  String
  6. ݱঢ়ͷ֬ೝ • ෳ਺ਓͰ Git Λ࢖ͬͯฒߦ։ൃΛ͍ͯ͠Δ • ͋Δఔ౓େ͖͍ϒϥϯν΋͋Δ • ϓϩδΣΫτ͸தن໛ఔ౓ •

    ͦΕͳΓʹमਖ਼ൣғ͸େ͖͘ͳΓͦ͏ • Play 2.4 ͸࢑ఆҠߦͰ͖Δ΋ͷΛఏڙ͍ͯ͠Δ
  7. ࣗ࡞ϓϥάΠϯͷҠߦ Redis ΛΩϟογϡʹ࢖͏ͨΊʹ Plugin Λࣗ࡞ (scala-redis Λར༻) class  RedisModule  extends

     Module  {      def  bindings(environment:  Environment,  configuration:  Configuration)   =  Seq(          bind[CacheApi].to[RedisCacheApi]      )   } play.modules.enabled  +=  "modules.RedisModule"   play.modules.disabled  +=  "play.api.cache.EhCacheModule" build.sbt
  8. ScalikeJDBC Play Support play.modules.enabled  +=  "scalikejdbc.PlayDBApiAdapterModule"   db  {  

       default  {          driver  =  org.postgresql.Driver          url  =  "jdbc:postgresql://localhost/test"          username  =  test          password  =  test          hikaricp  {              poolName  =  "play"              registerMbeans  =  true          }      }   } HikariCP ͷઃఆΛར༻͔ͨͬͨ͠ͷͰ scalikejdbc-play-dbapi-adapter Λར༻ 2.4 ͔Β HikariCP
  9. Thread pool configuration akka  {      actor  {  

           default-­‐dispatcher  {              fork-­‐join-­‐executor  {                  parallelism-­‐min  =  300                  parallelism-­‐max  =  300              }          }      }   } play  {      akka  {          actor  {              default-­‐dispatcher  =  {                  fork-­‐join-­‐executor  {                      parallelism-­‐min  =  300                      parallelism-­‐max  =  300                  }              }          }      }   } 2.3 2.4 Akka ͱಉ͡هड़ʹͳͬͨ http://doc.akka.io/docs/akka/2.3.11/general/configuration.html
  10. Remote address औಘ trustxforwarded=true 2.3 ·Ͱ͸ϦόʔεϓϩΩγΛհ͢Δͱ͖ʹɺҎ ԼͷઃఆΛ͢Δඞཁ͕͕͋ͬͨෆཁʹ 2.4 ͔Β x-forwarded

    ʹՃ͑ͯ RFC7239 ΋αϙʔ τɻ play.http.forwarded.version Ͱ੾Γସ͑Մೳ https://www.playframework.com/documentation/2.4.x/HTTPServer
  11. i18n ରԠ Controller ͷΠϯελϯελΠϓ͕ object ͷ৔߹ s/implicit lang: Lang/implicit messages:

    Messages/g import  play.api.play.current   import  play.api.i18n.Messages.Implicits._ Lang ͔Β Messages ʹ҉໧తม׵Λߦͬͯ͘ΕΔ Controller ʹҎԼͷ 2 ߦΛ௥Ճ͢Δ
  12. withLang/clearingLang object  FooController  extends  Controller  with  LegacyI18nSupport val  lang  =

     user.toLang   Ok(Messages("hello.world")).withLang(lang) trait  LegacyI18nSupport  {      implicit  class  ResultWithLang(result:  Result)(implicit  messagesApi:  MessagesApi)  {          def  withLang(lang:  Lang):  Result  =  messagesApi.setLang(result,  lang)          def  clearingLang:  Result  =  messagesApi.clearLang(result)      }   } ҉໧తʹ messagesApi Λݺͼग़ͯ͘͠ΕΔ Result ͔Β MessagesApi ʹҠಈ͍ͯ͠Δ
  13. ໨ࢦ͢΂͖ 2.4 style import  javax.inject.Inject   import  play.api.i18n.{MessagesApi,  I18nSupport}  

    import  play.api.mvc.Controller   class  FooController  @Inject()  (val  messagesApi:  MessagesApi)      extends  Controller  with  I18nSupport  {   } Controller ʹ MessagesApi Λ DI ͯ͠ i18nSupport Λ mixin RequestHeader Λ Messages ʹ҉໧తʹม׵͢Δ
  14. class Controller GET  /            

                 @controllers.Application.index 2.3 ํࣜͰ΋ @ Λ͚ͭΔ͜ͱͰಛఆͷ Controller ͔ Β class Խ͍ͯ͘͜͠ͱ͕Մೳ routesGenerator  :=  InjectedRoutesGenerator routes build.sbt 2.4 ඪ४ͷ΍Γํɻ Controller ͸͢΂ͯ class
  15. ApplicationLifecyle @Singleton   class  IntervalJobs  @Inject()  (      system:

     ActorSystem,      lifecycle:  ApplicationLifecycle)  {      val  minuteJob  =  system.scheduler.schedule(1.minutes,  1.minutes)  {          //  …      }      lifecycle.addStopHook  {  ()  =>          Future.successful(minuteJob.cancel())      }   } • GlobalSettings#onStart • DI ର৅ΫϥεͷίϯετϥΫλ΁ • GlobalSettings#onStop • ApplicationLifecycle#addStopHook ΁
  16. HttpErrorHandler class  ErrorHandler  @Inject()  (      env:  Environment,  

       config:  Configuration,      sourceMapper:  OptionalSourceMapper,      router:  Provider[Router],      val  messagesApi:  MessagesApi      )  extends  DefaultHttpErrorHandler(env,  config,  sourceMapper,  router)  with   I18nSupport  {          def  onBadRequest(request:  RequestHeader,  message:  String)  =  ???          def  onForbidden(request:  RequestHeader,  message:  String)  =  ???          def  onNotFound(request:  RequestHeader,  message:  String)  =  ???          def  onOtherClientError(request:  RequestHeader,  statusCode:  Int,  message:   String)  =  ???          def  onServerError(request:  RequestHeader,  exception:  Throwable)  =  ???      } DefaultHttpErrorHandler Λܧঝ͢ΔͱҠߦ͠΍͍͢ I18nSupport Λ࢖ͬͯΤϥʔը໘ͷࠃࡍԽରԠ΋Մ
  17. Play Roadmap 3.0 Play 3 Implementation Plan https://docs.google.com/document/d/1OejviUX5TDlRVeKg1JD9L4qh2Tn21giItmyxRXCydN0/edit Play 3.0.0

    1st June 2016 Remove global state Play 2.5.0 1st October 2015 Deprecate every API that relies on global state for which there exists a DI alternative.
  18. ·ͱΊ • վળ͞Ε͍ͯ͘ Play • Play 2.4 ͸ 3.0 ʹ޲͚ͨελʔτ஍఺

    • Play νʔϜ͸ҠߦΛߟྀͯ͘͠Ε͓ͯΓɺஈ֊ తʹΞοϓάϨʔυ΋Մೳ • Play 2.4 ͔Β࢝ΊΔਓ͸ݹ͍৘ใʹ࿭Θ͞Εͳ͍ Α͏ʹ