Video: Create and Run Play Framework Apps in IntelliJ

The Play Framework community voted heavily to add support for Play Framework 2 in IntelliJ IDEA and JetBrains came through! Here is a short (3min) screencast that shows you how to create and run a Play 2 app in IntelliJ 12 (Ultimate Edition) with the Play Framework plugin:

If you’d like to learn more, check out the detailed tutorial from JetBrains.

Posted in IntelliJ, Play Framework | 6 Responses

CodeMash 2013: Client/Server Apps with HTML5, Play, CoffeeScript

Today I’ll be presenting at CodeMash 2013 about Client/Server Apps with HTML5, Play, CoffeeScript, and More. Here is the session description:

The web application landscape is rapidly shifting back to a Client/Server architecture. This time around the Client is JavaScript, HTML, and CSS in the browser. The tools and deployment techniques for these types of applications are abundant and fragmented. This session will teach you how to pull together jQuery, LESS, Twitter Bootstrap, and some CoffeeScript to build the Client. The Server could be anything that talks HTTP but this session will use the Play Framework. You will also learn how to deploy Client/Server web apps on the cloud using a Content Delivery Network (Amazon CloudFront) for the Client and a Cloud Application Provider (Heroku) for the Server.

The session is today at 4:50 PM in Rosewood. Hope to see you there!

Posted in Conferences, HTML5, Play Framework | 5 Responses

Presenting in Dallas: Play Framework, HTML5 and Java

Tomorrow (December 12, 2012) I will be presenting in Dallas at the JavaMUG about Client/Server Apps with Play Framework, HTML5 and Java. Here is the session abstract:

The web application landscape is rapidly shifting back to a Client/Server architecture. This time around, the Client is JavaScript, HTML, and CSS in the browser. The tools and deployment techniques for these types of applications are abundant and fragmented.

This session will teach you how to pull together jQuery, LESS, Twitter, Bootstrap, and some CoffeeScript to build the Client. The Server could be anything that talks HTTP, but this session will use the Play Framework.

I hope to see you there!

Posted in HTML5, Java, Play Framework | 4 Responses

WebJars Officially Launched!

Check it out: webjars.org

Back in April I started an experiment called “WebJars” to see if it would be useful to package web libraries (JavaScript, CSS, etc) as Jar files. WebJars allow you to declaratively set client-side application dependencies just like we do for the server-side. A nice side effect of this is transitive dependencies. For instance, if you specify Bootstrap 2.2.1 as a dependency, then you automatically also get jQuery 1.8.2 as well. WebJars also make it easy to know what versions of web libraries are being used since not all web libraries use versions in their naming conventions.

Today WebJars are moving from an experiment to something real! I’ve launched the new webjars.org site and released all of the WebJars to Maven Central. The new site has a list of all the WebJars and documentation on how to use WebJars in Play 2, Servlet 3, Dropwizard, and Spring MVC apps.

I hope that WebJars help you build modern web applications! Let me know what you think of WebJars. Thanks!

Posted in WebJars | 13 Responses

Presenting at GraphConnect 2012: Building & Deploying Graph-based Web Apps

On November 6th I will be presenting Building & Deploying Graph-based Web Apps at the GraphConnect 2012 conference in San Francisco. Here is the session description:

This session will teach you how to build a Graph-based web application with Java, Play Framework, and Neo4j. You will also lean how to deploy the application on the cloud with Heroku. The session will primarily be code and live demos.

I hope to see you there!

Posted in Conferences, Java, Neo4j, Play Framework | 1 Response

JavaOne Video: Introduction to Play Framework

Check out the recording of my Introduction to Play Framework at JavaOne 2012. If you want to try Play Framework on your own you might want to start with my Play2torial for Java. There you will walk through all the major parts of building a modern web app: Models, Controllers, Views, Tests, JavaScript/CoffeeScript, Twitter Bootstrap, and Cloud Deployment. Let me know how it goes!

Posted in Java, Play Framework | Leave a comment

JavaOne Video: Client/Server Apps with HTML5 & Java

The recording of my JavaOne 2012 presentation, “Client/Server Apps with HTML5 and Java”, has been posted! In this presentation I walk through the modern web application architecture and how to build Client/Server style web apps with HTML5 and Java. Check it out and let me know what you think!

Posted in HTML5, Java | 2 Responses

New Adventures with Play, Scala, and Akka at Typesafe

Today I’m heading out on a new adventure at Typesafe, the company behind Play Framework, Scala, and Akka!

The past year and a half at Heroku have been really amazing. Not only have I enjoyed teaching others about Heroku, I’ve enjoyed my own frequent use of Heroku. It says something when a technology switch makes one never want to go back to the way it was done before. This is the experience that I (and many others) have had with Heroku. I can’t imagine going back to managing servers and painful deployments. I’m certainly a Heroku Evangelist for Life, but it’s time for a new adventure.

Typesafe is assembling a platform that is quickly becoming the foundation for next generation software. Working with the Typesafe technologies has been a delight and has opened my eyes to the future of scalable software. To build more scalable applications we need better programming models for asynchronous and concurrent code. Web applications are also transitioning to a modern Client/Server architecture where the Client is executed in the browser via JavaScript and the Server is RESTful JSON services.

The Typesafe Stack brings together a scalable foundation and a modern web framework to form a platform that is perfect for the next generation of software. I’m incredibly excited to continue learning Play, Scala, and Akka while also helping other developers do the same!

Working at Heroku has been a magnificent adventure and I’m sad to see it end. I will certainly continue showing off Heroku and using it myself, especially combined with the Typesafe Stack!

Posted in Heroku, Typesafe | 15 Responses

NoSQL Inside SQL with Java, Spring, Hibernate, and PostgreSQL

There are many benefits to schema-less NoSQL datastores, but there are always trade-offs. The primary gift the NoSQL movement has given us is the variety of options we now have for data persistence. With NoSQL we no longer must try to shoehorn everything into a relational model. Now the challenge is in deciding which persistence model fits best with each domain in a system and then combining those models in a cohesive way. The general term to describe this is Polyglot Persistence and there are many ways to accomplish it. Lets walk through how you can combine a regular SQL model with a key-value NoSQL model using Java, Spring, Hibernate, and PostgreSQL.

This article covers the pieces of a simple web application which uses regular SQL and PostgreSQL’s hstore for key value pairs. This method is a mix of NoSQL inside SQL. One benefit of this approach is that the same datastore can be used for both the SQL and the NoSQL data.

In this example the server technologies will be Java, Spring, and Hibernate. (The same thing can also be done with Rails, Django, and many other technologies.) To add Hibernate support for hstore I found a fantastic blog about “Storing sets of key/value pairs in a single db column with Hibernate using PostgreSQL hstore type“. I won’t go through that code here but you can find everything in the GitHub repo for my demo project.

This demo app uses Maven to define the dependencies. Embedded Jetty is started via a plain ‘ole Java application that sets up Spring MVC. Spring is configured via Java Config for the main stuff, the web stuff, and the database stuff.

The client technologies will be jQuery and Bootstrap and there is a strict seperation between the client and server via RESTful JSON services. The whole client-side is in a plain ‘ole HTML file. Via jQuery / Ajax the client communicates to JSON services exposed via a Spring MVC Controller.

Ok. Now onto the NoSQL inside SQL stuff. This application stores “Contacts” that have a name but also can have many “Contact Methods” (e.g. phone numbers and email addresses). The “Contact Methods” are a good use of a schema-less, key-value pair column because it avoids the cumbersome alternatives: putting that information into a separate table or trying to create a model object that has all of the possible “Contact Methods”. So lets take a look at the simple Contact Entity:

package com.jamesward.model;
 
import net.backtothefront.HstoreUserType;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.HashMap;
import java.util.Map;
 
@Entity
@TypeDef(name = "hstore", typeClass = HstoreUserType.class)
public class Contact {
 
    @Id
    @GeneratedValue
    public Integer id;
 
    @Column(nullable = false)
    public String name;
 
    @Type(type = "hstore")
    @Column(columnDefinition = "hstore")
    public Map<String, String> contactMethods = new HashMap<String, String>();
 
}

If you are familiar with Hibernate / JPA then most of this should look pretty familiar to you. The new / interesting stuff is the contactMethods property. It is a Map<String, String> and it uses PostgreSQL’s hstore datatype. In order for that to work, the type has to be defined and the columnDefinition set. Thanks again to Jakub Głuszecki for putting together the HstoreHelper and HstoreUserType that make this possible.

Now the rest is simple because it’s just plain Hibernate / JPA. Here is the ContactService that does the basic query and updates:

package com.jamesward.service;
 
import com.jamesward.model.Contact;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaQuery;
 
import java.util.List;
 
@Service
@Transactional
public class ContactServiceImpl implements ContactService {
 
    @PersistenceContext
    EntityManager em;
 
    @Override
    public void addContact(Contact contact) {
        em.persist(contact);
    }
 
    @Override
    public List<Contact> getAllContacts() {
        CriteriaQuery<Contact> c = em.getCriteriaBuilder().createQuery(Contact.class);
        c.from(Contact.class);
        return em.createQuery(c).getResultList();
    }
 
    public Contact getContact(Integer id) {
        return em.find(Contact.class, id);
    }
 
    @Override
    public void addContactMethod(Integer contactId, String name, String value) {
        Contact contact = getContact(contactId);
        contact.contactMethods.put(name, value);
    }
 
}

Now that you understand how it all works, check out a live demo on Heroku.

If you want to run this app locally or on Heroku, then first you need to grab the source code and continue working inside the newly created spring_hibernate_hstore_demo directory:

$ git clone https://github.com/jamesward/spring_hibernate_hstore_demo.git
$ cd spring_hibernate_hstore_demo

To run locally:

  1. Setup your PostgreSQL database to support hstore by opening a psql connection to it:
    $ psql -U username -W -h localhost database
  2. Then enable hstore:
    => create extension hstore;
    => \q
  3. Build the app (depends on having Maven installed):
    $ mvn package
  4. Set the DATABASE_URL environment variable to point to your PostgreSQL server:
    $ export DATABASE_URL=postgres://username:password@localhost/databasename
  5. Start the app:
    $ java -cp target/classes:target/dependency/* com.jamesward.Webapp
  6. Try it out

Cool! Now you can run it on the cloud with Heroku. Here is what you need to do:

  1. Install the Heroku Toolbelt
  2. Login to Heroku:
    $ heroku login
  3. Create a new app:
    $ heroku create
  4. Add Heroku Postgres:
    $ heroku addons:add heroku-postgresql:dev
  5. Tell Heroku to set the DATABASE_URL environment variable based on the database that was just added (replace YOUR_HEROKU_POSTGRESQL_COLOR_URL with your own):
    $ heroku pg:promote YOUR_HEROKU_POSTGRESQL_COLOR_URL
  6. Open a psql connection to the database:
    $ heroku pg:psql
  7. Enable hstore support in your database:
    => create extension hstore;
    => \q
  8. Deploy the app:
    $ git push heroku master
  9. View the app on the cloud:
    $ heroku open

Fantastic! Let me know if you have any questions.

Posted in Hibernate, Java, NoSQL, PostgreSQL, Spring, SQL | 15 Responses