Archive for Java

Getting Started with the Java Play Framework and Building a Test App

This is Step B, the final setup stage in our Building a Microservices Project series before we actually start building our application.

By the end of this post we will have a working Java development environment, installed Activator, will have created a test application, and learned how to compile and run the application locally.

Activator is a build tool that makes it much easier to pull in required libraries, run a Java web server,  and create skeleton projects. A skeleton project gives us the bones needed to build new applications more quickly.

Before we get started, please make sure you already have the latest Java runtime environment installed.

Please also install the IntelliJ IDE (Integrated Development Environment):

The free community version of IntelliJ is sufficient. If you prefer to use another IDE, go for it! Unfortunately, you’ll have to figure out how to configure your project in that IDE, though I am sure there are plenty of helpful tutorials out there. I will be mentioning configuration steps specific to IntelliJ.

Newb Tip: If this is your first time installing IntelliJ, there is a first-time setup for plugins, etc. You can simply leave all the defaults and click Next a few times.

Next, install the latest Java JDK:

Make a note of the JDK install location. You can customize this however you wish, just remember it.

Windows users extra step

If you do not use Windows, you can skip this short section.

In order for java and javac to work properly in our Cygwin terminal, we need to add the JDK bin to our system PATH.

  • Go to Control Panel -> System -> Advanced system settings
  • Select the Advanced tab
  • Click Environment Variables
  • Underneath System variables, select Path and click Edit
  • Click Browse
  • Navigate to where you installed the JDK, and select the bin directory within it
  • Click OK to exit all the windows
  • Open up your terminal and run:
    • $ javac -help
  • Make sure you get a lengthy usage description and not an error

Setup Activator

When you visit the link below, look for the section at the top, “Alternative downloads”, and click Offline distribution.

After the download completes, extract its contents. Move activator into your home directory, and add it to your PATH.

Mac OS X / Linux

  • Edit ~/.profile
  • Add or change the PATH line to include the activator location:
export PATH=/path/to/activator-x.x.x:$PATH
  • Additionally, ensure activator is executable:
    • $ chmod u+x /path/to/activator-x.x.x/activator

Windows

  • Move the extracted activator directory into your Cygwin home directory. For me this looked like: D:\cygwin64\home\Elliot\activator-dist-1.3.10
  • Add the activator bin to your system PATH:
    • Control Panel -> System -> Advanced system settings
    • Advanced tab, Environment variables
    • Underneath System variables, click Path and then click Edit
    • Click Browse, and locate the new activator home as described above, but be sure to select the bin directory. For me, this was D:\cygwin64\home\Elliot\activator-dist-1.3.10\bin
    • Click OK to close all windows

Now, ensure that activator is ready. Open a new terminal, and type:

$ activator -h

If you don’t see any errors, you are good to proceed.

Create your first Play app

From the terminal, cd to your home directory. Feel free to create a subdirectory within here, called “code” or “projects”.

Newb Tip:

  • $ mkdir ~/projects
  • $ cd ~/projects

Now let’s create a new Play applcation, called little-app:

  • $ activator new little-app play-java

This tells activator to create a new project using the Play framework and Java, called little-app. It creates a new directory for you, little-app, which you can now cd to.

  • $ cd little-app

Now let’s launch the web service and make sure we have our first demo page:

  • $ activator run

Upon first run, activator will download many dependencies. This gets quicker the next time you call run for the project.

At some point, you will see the downloading come to a conclusion, and some text saying “Running the application … Listening for HTTP on … :9000”. Let’s see if this is really true. Open your browser, and go to: http://127.0.0.1:9000/

There may be a few second delay before you see the demo Play page. Your sources were compiling.

So, if you do see your first demo page, then excellent! We have a working development environment and are ready to begin building the first microservice Java Play application.

To stop the Play application from running, simply type Ctrl+D (Ctrl+C also works).

The Big Microservices Project with Java Play and React

In order to motivate myself in adding some new blog posts to this site, I have come up with a fun project. That is very relevant, yet tricky for newbs–building out a microservices application.

Additionally, I want to learn a new Java framework, so for the most part we will write each service using Java Play, which is built on top of Netty.

Each post will tackle a specific and targeted need of this application. And hopefully be easy enough to follow for those new to web development and infrastructure operations.

What this series will not do: teach programming. I will not spend too much time explaining basic programming concepts. So make sure you follow a beginner’s tutorial, in Java or any OOP language, before jumping into this set of how-to’s.

However, we will discuss advanced programming concepts–those that directly impact our application design. Powerful concepts like concurrency, non-blocking I/O, RESTful API’s, tracing, message queues, and many more goodies!

Although we will mostly use Java Play, we will use React for the front-end JavaScript piece, and possibly Go for a small proxy service. This is going to be a big project, probably take me a couple months to complete; I imagine the result will change from some of these initial thoughts. This is called Agile Architecture: commit to decisions until the last responsible moment.

What is our application going to do? As of now, I am thinking the best way to teach the importance around concurrent connections, message queues, and microservices is to build out a social media data aggregation service.

Why? It is not necessarily going to be that useful. However, social media API’s provide us lots of data to play with right from the start. Additionally, you’ll find that 3rd party API’s can never be trusted. We need to build services that won’t fail just because Facebook is slow or down.

Let’s build 3 adapters for data aggregation– Facebook, Twitter, and Pinterest. Hopefully we can do something interesting to look at with this.

Below is the initial diagram for our microservice stack. Don’t let this intimidate you– there are quite a few components. However, when you’re done with each post in this series, you should walk away with a solid understanding of each piece and its importance to the big picture.

Oh, the last point I should make is that we will be securing our single-page application with OAuth 2.0. We’re going to build out our own OAuth 2.0 implementation, most likely incorporating it into the gateway component.

Lastly, we should ensure our application is easy to scale, maintain & change, and debug. I’ll make sure to hit on this criteria several times over the course of this project.

Let’s get started!

Infra layout map