Java

Java is a set of computer software and specifications developed by Sun Microsystems, which was later acquired by the Oracle Corporation, that provides a system for developing application software and deploying it in a cross-platform computing environment. Java is used in a wide variety of computing platforms from embedded devices and mobile phones to enterprise servers and supercomputers.

Spring Logo

Spring Framework

The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key element of Spring is infrastructural support at the application level: Spring focuses on the "plumbing" of enterprise applications so that teams can focus on application-level business logic, without unnecessary ties to specific deployment environments.

Hibernate Logo

Hibernate Framework

Hibernate ORM is an object-relational mapping framework for the Java language. It provides a framework for mapping an object-oriented domain model to a relational database.

Showing posts with label Play Framework. Show all posts
Showing posts with label Play Framework. Show all posts

Sunday, August 2, 2015

Changing the default port number(9000) of a Play Application

Changing the default port number  9000 of Play Application:

In order to change the default port number, we can do it in two ways:

Way 1: In the conf/application.conf file change the default port number value.
             http.port=2015

Way 2: Passing the port number value while running the application in command line or terminal.

Play 1.x:  

play run --http.port=2015

Play 2.x: 

activator "run 2015"  - browser-reload mode
activator "~run 2015" - continuous-reload mode
activator debug "run 2015" - debug mode

Reference:

  1. https://www.playframework.com/documentation/2.2.x/ProductionConfiguration
  2. https://stackoverflow.com/questions/8205067/how-do-i-change-the-default-port-9000-that-play-uses-when-i-execute-the-run?rq=1
Happy Learning!

Saturday, August 1, 2015

Play Application Structure

The Structure of a Play Application:

The structure of a Play Application is very similar to a other MVC application. There are Models, Views and Controllers folder under the app directory.  In addition to app directory it will contain some other directories like conf, log, public, project, lib, test etc.


    app                      → Application sources
    └ controllers → Application controllers
    └ models → Application business layer
    └ views → Templates
    conf
    → Configurations files
    └ application.conf → Main configuration file
    └ routes → Routes definition
    public → Public assets
    └ stylesheets → CSS files
    └ javascripts → Javascript files
    └ images → Image files
    project
    → sbt configuration files
    └ build.properties → Marker for sbt project
    └ Build.scala → Application build script
    └ plugins.sbt → sbt plugins
    lib
    → Unmanaged libraries dependencies
    logs
    → Standard logs folder
    └ application.log → Default log file
    target
    → Generated stuff
    └ scala-2.9.1
    └ cache
    └ classes → Compiled class files
    └ classes_managed → Managed class files (templates, ...)
    └ resource_managed → Managed resources (less, ...)
    └ src_managed → Generated sources (templates, ...)
    test
    → source folder for unit or functional tests

    The app directory

    The app directory contains all executable artifacts: Java and Scala source code, templates and compiled asset's sources.
    There are three packages in the app directory, one for each component of the MVC architectural pattern:
    • app/controllers
    • app/models (for default application this folder is optional)
    • app/views
    We can add our packages, for example app/utility.

    The conf directory

    The conf directory will contain the configuration files of the Play Application. There are two main configuration files:
    • application.conf - the main configuration file for the application, which contains configuration parameters.  
    • routes - the routes definition file
    The public directory
    All resources stored in the public directory are static assets that are served directly by the Web server. This directory is split into three sub-directories for Images, Css stylesheets and JavaScript files.
    In a newly-created application, the /public directory is mapped to the /assets URL path, but you can easily change that, or even use several directories for your static assets.

    The project directory

    The project directory contains the sbt build definitions:
    • plugins.sbt defines sbt plugins used by this project
    • build.properties contains the sbt version to use to build your app.

    The lib directory

    The lib directory is optional and contains unmanaged library dependencies, ie. all JAR files you want to manually manage outside the build system. If we want to use any JAR file then drop on the JAR file here and they will be added to your application classpath.

    The build.sbt file

    The build.sbt file contains the project main build declarations. The .scala files in the project/ directory can also be used to declare your project build.

    The target directory

    The target directory contains everything which is generated by the build system. It can be useful to know what is generated in this directory.
    • classes/ contains all compiled classes (from both Java and Scala sources).
    • classes_managed/ contains only the classes that are managed by the framework.
    • resource_managed/ contains generated resources, typically compiled assets such as LESS CSS and CoffeeScript compilation results.
    • src_managed/ contains generated sources, such as the Scala sources generated by the template system.
    • web/ contains assets processed by sbt-web such as those from the app/assets and public folders.

    The .gitignore files

    The generated folders should be ignored by your version control system. Here is the typical .gitignore file for the play application.
    logs
    project
    /project
    project
    /target
    target
    tmp
    dist
    .cache



    References:

    1. https://www.playframework.com/documentation/2.4.x/Anatomy
    2. http://jstricks.com/analysis-play-application-framework/
    3. http://richardstudynotes.blogspot.in/2013/01/v-behaviorurldefaultvmlo.html

    First application using Play Framework

    First application using Play Framework:

    Before going to implement the First play application, we need to know some thing about activator and it's commands:

    Activator:  
    • Typesafe Activator is a browser based or command line tool that helps developers get started with the Typesafe Reactive Platform.
    • Activator includes the sbt build tool, a quick-start GUI, and a catalog of template applications. 
    • Activator allows us to select a template that our new application should be based off.  The names of the templates are play-scala for Scala based Play applications and play-java for Java based Play applications.
    The following are the important commands of activator:
    Command name Description
    activator To run the Console
    activator ui Start the Activator UI
    activator new [name] [template-id] Create a new project with [name] using template [template-id]
    activator list-templates Print all available template names
    activator help Print this message

    Creating the new application:
    To create a new applications we need to use the activator new command.

    D:\Play\My_Practise>activator new HelloWorld
    Fetching the latest list of templates...

    Browse the list of templates: http://typesafe.com/activator/templates
    Choose from these featured templates or enter a template name:
    1) minimal-akka-java-seed
    2) minimal-akka-scala-seed
    3) minimal-java
    4) minimal-scala
    5) play-java
    6) play-scala

    (hit tab to see a list of all templates)
    > 5
    OK, application "HelloWorld" is being created using the "play-java" template.

    To run "HelloWorld" from the command line, "cd HelloWorld" then:
    D:\Play\My_Practise\HelloWorld/activator run

    To run the test for "HelloWorld" from the command line, "cd HelloWorld" then:
    D:\Play\My_Practise\HelloWorld/activator test

    To run the Activator UI for "HelloWorld" from the command line, "cd HelloWorld" then:

    D:\Play\My_Practise\HelloWorld/activator ui

    Project Structure:



    Compiling the Project:
    By using activator compile command, we can compile our application without running the server.

    D:\Play\My_Practise\HelloWorld>activator compile
    [info] Loading project definition from D:\Play\My_Practise\HelloWorld\project
    [info] Updating {file:/D:/Play/My_Practise/HelloWorld/project/}helloworld-build...
    [info] Resolving org.fusesource.jansi#jansi;1.4 ...
    [info] Done updating.
    [info] Set current project to HelloWorld (in build file:/D:/Play/My_Practise/HelloWorld/)
    [info] Updating {file:/D:/Play/My_Practise/HelloWorld/}root...
    [info] Resolving jline#jline;2.12.1 ...
    [info] Done updating.
    [info] Compiling 6 Scala sources and 2 Java sources to D:\Play\My_Practise\HelloWorld\target\scala-2.11\classes...
    [success] Total time: 135 s, completed Jul 29, 2015 11:52:10 PM

    Running the Project:
    By using activator run command, we can run the application.

    D:\Play\My_Practise\HelloWorld>activator run
    [info] Loading project definition from D:\Play\My_Practise\HelloWorld\project
    [info] Set current project to HelloWorld (in build file:/D:/Play/My_Practise/HelloWorld/)

    --- (Running the application, auto-reloading is enabled) ---

    [info] p.a.l.c.ActorSystemProvider - Starting application default Akka system: application
    [info] p.c.s.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

    (Server started, use Ctrl+D to stop and go back to the console...)


    Testing the Project:
    By using activator test command, we can test the application.

    D:\Play\My_Practise\HelloWorld>activator test
    [info] Loading project definition from D:\Play\My_Practise\HelloWorld\project
    [info] Set current project to HelloWorld (in build file:/D:/Play/My_Practise/HelloWorld/)
    Warning: node.js detection failed, sbt will use the Rhino based Trireme JavaScript engine instead to run JavaScript assets compilation, which in some cases may be orders of magnitude slower than using node.js.
    [info] Compiling 2 Java sources to D:\Play\My_Practise\HelloWorld\target\scala-2.11\test-classes...
    [info] D:\Play\My_Practise\HelloWorld\test\ApplicationTest.java: D:\Play\My_Practise\HelloWorld\test\ApplicationTest.java uses or overrides a deprecated API.
    [info] D:\Play\My_Practise\HelloWorld\test\ApplicationTest.java: Recompile with -Xlint:deprecation for details.
    [info] - application - Creating Pool for datasource 'default'
    [info] - play.api.libs.concurrent.ActorSystemProvider - Starting application default Akka system: application
    [info] - play.api.libs.concurrent.ActorSystemProvider - Shutdown application default Akka system: application
    [info] - application - Shutting down connection pool.
    [info] Passed: Total 3, Failed 0, Errors 0, Passed 3
    [success] Total time: 18 s, completed Jul 30, 2015 12:00:49 AM

    Debugging the Project:
    By using following command, we can debug the application.
    activator -jvm-debug <port> 

    $ activator -jvm-debug 9999
    When a JPDA port is available, the JVM will log this line during boot:
    Listening for transport dt_socket at address: 9999

    Happy Coding!

    Wednesday, July 29, 2015

    Play Framework Installation




    Installing Play Framework:

    The following are the steps to install a play framework:

    Step 1: In order to run Play framework , we need JAVA 6 or higher versions. To test in your system java is installed or not, use the following commands in your terminal or command prompt:
    javac and java.

    And also add the JAVA_HOME to the environment variable.

    Note: Play Framework 2.4  recommended to use JAVA 8.

    Step 2:  Download the latest Play framework( Activator) from the following site:
    https://www.playframework.com/download

    here i am downloaded Play 2.3.9 (activator)

    Note: Instead of installing latest Activator use previous versions because may be there some issues in latest activator.

    Step 3:  Extract the downloaded play framework and add it to the Class path:

    Right Click on Computer --> Properties --> Advanced System Settings --> Advanced --> Click on Environment Variable --> select PATH and click on Edit. In Variable value append the extracted play framework location.

    Step 4: Check the play framework installed properly or not using the following command in terminal or command prompt:

    C:\Users\Ranga>activator help

    Usage activator [options] [command]

    Commands:
    ui                 Start the Activator UI
    new [name] [template-id]  Create a new project with [name] using template [template-id]
    list-templates     Print all available template names
    help               Print this message

    Options:
    -jvm-debug [port]  Turn on JVM debugging, open at the given port.  Defaults to 9999 if no port given.

    Environment variables (read from context):
    JAVA_OPTS          Environment variable, if unset uses ""
    SBT_OPTS           Environment variable, if unset uses ""
    ACTIVATOR_OPTS     Environment variable, if unset uses ""

    Please note that in order for Activator to work you must have Java available on the classpath.
    ----------------------------------

    Congratulations, you are successfully installed play framework in your system.

    Happy Learning!

    About Play Framework

    About Play Framework:


    • Play is an open source modern web application framework for writing scalable web applications. It is written in both Scala and Java languages.
    • Play was created by software developer Guillaume Bort, while working at Zenexity.
    • Play is Action based MVC web framework for applications.
    • Play is based on a lightweight, stateless, web-friendly architecture. 
    • Play Framework is Built on Akka, Play provides predictable and minimal resource consumption (CPU, memory, threads) for highly-scalable applications.
    • Play Framework is a high-productivity Java and Scala web application framework that integrates the components and APIs you need for modern web application development.
    • The main aim of play framework is optimize developer productivity by using convention over configuration.
    • Play by design has a stateless and non-blocking architecture. This makes it easy to horizontally scale web applications written using the Play Framework.
    • Build and deployment was migrated to SBT(Simple Build Tool) and templates use Scala instead of Groovy.
    • Play framework has built in JBoss Netty web server. We can also create WAR file and deploy into other application frameworks like Tomcat, JBoss etc.
    • Play supports for push and pull based application controller calls. ( Push-based architecture also called "action-based". These frameworks use actions that do the required processing, and then "push" the data to the view layer to render the results. Django, Ruby on Rails, Spring MVC are good examples of this architecture.  An alternative to this is pull-based architecture, sometimes also called "component-based". These frameworks start with the view layer, which can then "pull" results from multiple controllers as needed. In this architecture, multiple controllers can be involved with a single view. Tapestry, JBoss Seam, Java Server Faces(JSF) and Apache Wicket are examples of pull-based architectures.)
    History:

    Version Released on Features
    Play 1.0 October 2009
    Play 1.1 November 2010 Migration from Apache MINA to JBoss Netty, Scala support, native GlassFish container, an asynchronous web services library, OAuth support, HTTPS support and other features.
    Play 1.2 April 2011 Dependency management with Apache Ivy, support for WebSocket, integrated database migration, a switch to the H2 databaseand other features
    Play 2.0 March 13, 2012 Typesafe Stack 2.0
    Play 2.1 February 6, 2013 Upgraded to Scala 2.10 and introduced, among other new features, modularisation, a new JSON API, filters and RequireJS support.
    Play 2.2 September 20, 2013 Upgraded support for SBT to 0.13, better support for buffering, built in support for gzip and new stage and dist tasks with support for native packaging on several platforms such as OS X (DMG), Linux (RPM, DEB), and Windows (MSI) as well as zipfiles.
    Play 2.3 May 30 2014 Introducing the activator command, Build improvements, Java improvements, Web Services enhancements, Support for Scala 2.11, Actor WebSockets, Custom SSLEngine for HTTPS, Upgrade to Netty 3.9.3
    Play 2.4 May 26 2015 Dependency Injection, Testing, Embedding Play, Aggregated reverse routers, Java 8 support, Maven/sbt standard layout, Experimental Features, Upgraded to Ebean 4, HikariCP is the default connection pool, WS supports Server Name Identification (SNI)
    Reasons to learn Play Framework: 

    We have several web application frameworks like Spring, Struts, JSF etc but still why we want to learn play framework because of the following reasons.

    1. Developer friendly: what it means if you changing any thing in java code no need to stop and start the server. Here just you need to hit browser refresh button it automatically compiled (if any errors is there automatically displayed in browser) and runs the application. Play supports reload for the Java Code, Templates etc..

    2. Supports both Java and Scala Programming: Play framework supports both Java and Scala programming language. Because of this reason, developer can select appropriate language for their development.

    3. Reactive by nature:  Play frame has built in Server called JBoss Netty. It supports the non-blocking I/O operations. It is very easy and inexpensive to make remote calls in parallel, which is important for high performance apps in SOA ( Service Oriented Architecture).

    4. REST Support: Play framework makes it very easy to write RESTful application. It has very good support for HTTP routing. HTTP routing translates HTTP requests into action calls. It has in built in JSON API for marshaling and un-marshaling so no need to add new library.

    Play Framework Architecture:



    Lets see the Pros and Cons of Spring and Play framework:

    Spring MVC:
      Pros:
         1. Good raws throughput.
         2. Type safety reduces code rot
      Cons:
         1. Not developer friendly.
         2. Threaded synchronous approach difficult to scale for lots of I/O in a SOA environment.

    Play:
        Pros:
           1. Fast for raw throughput.
           2. Non Blocking I/O at the core makes concurrency easy.
           3. Hot reloaded makes it is possible to get the things done quickly.
           4. Strong type safety throughout reduces code rot.
        Cons:
           1. Even with hot reload, a compiled statically typed language isn't quite fast as an interpreted dynamically typed language.
    References:
    1. https://www.playframework.com/documentation/2.4.x/Home
    2. https://en.wikipedia.org/wiki/Play_framework
    3. https://blog.openshift.com/day-30-play-framework-a-java-developer-dream-framework/
    4. https://stackoverflow.com/tags/playframework/info
    5. http://www.ybrikman.com/writing/2014/03/10/the-ultimate-guide-to-getting-started/
    6. https://www.playframework.com/changelog

    Happy Learning!