Java or Scala – Web development
with Playframework 2.5.x?
Krešimir Dujmić
kresimir.dujmic@etna.hr
A Laginjina 7a, 10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr
• Lover introductory cost
– Open Source
– Doesn’t enforce expensive infrastructure(Commodity Hardware)
• Free of any vendor lock-in
• Commercial support 24x7
Open source based
Powered by
A Laginjina 7a, 10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr
What is Scala?
• Statically typed language on JVM
• Functional style programming - not enforced
• case classes, traits, monads, implicits, lazy val, high order
• checked exceptions gone, semicolons gone, brackets
almost gone, pattern matching
A Laginjina 7a, 10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr
What is PlayFramework?
• lightweight, stateless, web-friendly architecture
• make your changes and simply hit refresh! All you need
is a browser and a text editor.
• IDE support for Eclipse and IntelliJ IDEA
• support for Java or Scala
A Laginjina 7a, 10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr
Activator
• activator ui – in browser templates and starters
• http://127.0.0.1:8888/home
A Laginjina 7a, 10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr
Play application structure
• convention over configuration
• build.sbt – Scala build tool
• app folder – controllers and views
• conf folder – all configuration files
– application.conf
– logback.xml
– routes
• test folder – unit test folder
• public – static assests ( .js, images,
css)
A Laginjina 7a, 10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr
build.sbt
A Laginjina 7a, 10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr
HTTP request & responses
A Laginjina 7a, 10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr
conf/routes
HTTP
method
URL path Controller method
A Laginjina 7a, 10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr
Action, Controllers, Result
• Routes – conf/routes.xml:
• Controllers: controllers/HomeController.scala or .java
A Laginjina 7a, 10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr
Javantura speaker database -
model
case class Company(id: Long, name: String)
case class Speaker(id: Long,
name: String,
session: String,
registrated: Option[Date] ,
companyId: Option[Long])
A Laginjina 7a, 10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr
Evolutions
• H2 database & browser included
• can be any other database
• db.default.url="jdbc:h2:mem:play;MODE=MYSQL"
A Laginjina 7a, 10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr
Working with database
• Anorm, Slick, Ebeans
• HikariCP– JDBC connection pools
A Laginjina 7a, 10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr
Update example - anorm
A Laginjina 7a, 10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr
Working with JSON
• Automatic conversion to and from case classes with
minimal boilerplate.
• Custom validation while parsing.
• Create JSON from object:
A Laginjina 7a, 10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr
Testing
• Scala: integration with ScalaTest, specs2
• Java: integration with Junit
• Mockito library included
• natural languge mimicing
• Testing with a server
• Testing with a browser – Selenium WebDriver,
FluentLenium
• Integration test
• Model test
• activator test
A Laginjina 7a, 10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr
Javantura Speakers Test
A Laginjina 7a, 10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr
Installation & running
• activator dist
• standalone version of application
• ready to run zip (start.bat or BASH script included)
• only JAVA JRE is needed on server
A Laginjina 7a, 10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr
Javantura speakers database
A Laginjina 7a, 10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr
Javantura speakers database - DEMO
• Code available at:
https://github.com/kdujmic/javanturaplay
Links:
• https://playframework.com
• http://www.scalatest.org/
• https://etorreborre.github.io/specs2/
• https://www.scala-lang.org/
A Laginjina 7a, 10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr
Tnx!

Javantura v4 - Java or Scala – Web development with Playframework 2.5.x - Krešimir Dujmić

  • 1.
    Java or Scala– Web development with Playframework 2.5.x? Krešimir Dujmić kresimir.dujmic@etna.hr
  • 2.
    A Laginjina 7a,10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr • Lover introductory cost – Open Source – Doesn’t enforce expensive infrastructure(Commodity Hardware) • Free of any vendor lock-in • Commercial support 24x7 Open source based Powered by
  • 3.
    A Laginjina 7a,10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr What is Scala? • Statically typed language on JVM • Functional style programming - not enforced • case classes, traits, monads, implicits, lazy val, high order • checked exceptions gone, semicolons gone, brackets almost gone, pattern matching
  • 4.
    A Laginjina 7a,10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr What is PlayFramework? • lightweight, stateless, web-friendly architecture • make your changes and simply hit refresh! All you need is a browser and a text editor. • IDE support for Eclipse and IntelliJ IDEA • support for Java or Scala
  • 5.
    A Laginjina 7a,10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr Activator • activator ui – in browser templates and starters • http://127.0.0.1:8888/home
  • 6.
    A Laginjina 7a,10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr Play application structure • convention over configuration • build.sbt – Scala build tool • app folder – controllers and views • conf folder – all configuration files – application.conf – logback.xml – routes • test folder – unit test folder • public – static assests ( .js, images, css)
  • 7.
    A Laginjina 7a,10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr build.sbt
  • 8.
    A Laginjina 7a,10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr HTTP request & responses
  • 9.
    A Laginjina 7a,10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr conf/routes HTTP method URL path Controller method
  • 10.
    A Laginjina 7a,10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr Action, Controllers, Result • Routes – conf/routes.xml: • Controllers: controllers/HomeController.scala or .java
  • 11.
    A Laginjina 7a,10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr Javantura speaker database - model case class Company(id: Long, name: String) case class Speaker(id: Long, name: String, session: String, registrated: Option[Date] , companyId: Option[Long])
  • 12.
    A Laginjina 7a,10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr Evolutions • H2 database & browser included • can be any other database • db.default.url="jdbc:h2:mem:play;MODE=MYSQL"
  • 13.
    A Laginjina 7a,10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr Working with database • Anorm, Slick, Ebeans • HikariCP– JDBC connection pools
  • 14.
    A Laginjina 7a,10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr Update example - anorm
  • 15.
    A Laginjina 7a,10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr Working with JSON • Automatic conversion to and from case classes with minimal boilerplate. • Custom validation while parsing. • Create JSON from object:
  • 16.
    A Laginjina 7a,10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr Testing • Scala: integration with ScalaTest, specs2 • Java: integration with Junit • Mockito library included • natural languge mimicing • Testing with a server • Testing with a browser – Selenium WebDriver, FluentLenium • Integration test • Model test • activator test
  • 17.
    A Laginjina 7a,10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr Javantura Speakers Test
  • 18.
    A Laginjina 7a,10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr Installation & running • activator dist • standalone version of application • ready to run zip (start.bat or BASH script included) • only JAVA JRE is needed on server
  • 19.
    A Laginjina 7a,10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr Javantura speakers database
  • 20.
    A Laginjina 7a,10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr Javantura speakers database - DEMO • Code available at: https://github.com/kdujmic/javanturaplay Links: • https://playframework.com • http://www.scalatest.org/ • https://etorreborre.github.io/specs2/ • https://www.scala-lang.org/
  • 21.
    A Laginjina 7a,10 000 Zagreb T + 385 (0)1 461 11 11 F + 385 (0)1 461 27 22 E info@etna.hr W www.etna.hr Tnx!