Today’s Rapid Java EE Development: Live Coding from Scratch to Deployment


Aslak and I are happy to be kicking off JavaOne 2012 here in San Francisco, and this entry is to detail (and accompany) the talk we’ll soon be giving on Monday morning, 1st October, at 8:30AM Pacific Time at the Parc55 Hotel in Cyril Magnin II/III.

Because not everyone can be joining us in person, and because this is a technical talk we’d like to better enable our attendees to follow, this entry should serve as a guide for the live coding and demos we’ll be covering.  Session details may be found via the JavaOne site and on Lanyrd.

The abstract reads:

The myth of Java EE as a cumbersome platform is easily dispelled in this session, which aims to create a working application, from a blank repository to a live cloud deployment, in real time. It:

• Covers tools that bootstrap project creation, freeing you from mucking around with Maven POM boilerplate
* Generates a domain model and reverse-engineers JPA entities from it
* Automatically creates the scaffolding for tests that run in a real Java EE container, launched from the IDE
* Pushes it all to production on a public site

Using a variety of projects from the JBoss Community adhering to and building upon open standards, this presentation can create real enterprise apps in the time it takes other sessions to click through some slides. Let’s get building!

Presentation (intentionally-minimal) slides are available here, and I’ve posted some scripts and other human-readable instructions on GitHub.  The content we cover today is more or less adapted from an early chapter of the upcoming Continuous Enterprise Development in Java, now in Early Access Release and available for pre-sale.

The name of the game here is to take a blank repository (or filesystem), make a minimal, but fully-functioning data-based project, and get it online for the world to use before the JavaOne guys at the back of the room cut off our microphones.  Given the typical unreliability of conference network connections, we’ll pray extra hard to the Demo Gods that all goes smoothly.

Here we go.

I’ll be the first to admit that I’ve never much been a “tools” guy.  Sure, I use an IDE, but until recently I never saw much need to even dig too deeply into its featureset or gather many extra Plugins.  I used to catch Ruby on Rails demonstrations build an app from scratch and was typically left as one of the dissenters in the audience who believed that an application which directly mirrored the CRUD operations of a domain model was essentially worthless.  I’m well-versed in Maven configuration and can copy XML with the best of them, so that’s primarily how I’ve been coding my projects.

And I continue to believe in Java EE as a component model, especially as we’ve seen the Platform get less verbose and implementations break new ground in speed and efficiency.  Over the past few years we continually get feedback that figuring out exactly how to piece all the EE techs together into a cohesive application is difficult; documentation is scattered and may vary heavily from vendor to vendor.  At JBoss we’ve been working hard the past year or so on initiatives to address this very problem.

The first steps in setting up a new project are to lay out the structure, define the required dependencies, and get a working build in place.  As has become widely-accepted, I’ve been filling these via Maven for the past several years, and the process typically involves heavy use of copying an existing project, searching Google for relevant XML snippits, and wading through the Maven Central Repository index to find the coordinates of  whatever libraries you’ll depend upon.  Let’s see if we can take a different tack now.

JBoss Forge

JBoss Forge

Forge is an incremental project construction tool, and it’s implemented as a handy shell.  Through it, we can set up a Maven POM, quickly stub out our domain layer, reverse-engineer view components, get a head start on our test cases, manage servers and deployments, and much more through its extensible API.  This becomes especially handy for scripting.  Additionally, it’s well-integrated into:

JBoss Developer Studio

…JBoss Developer Studio, an integrated set of plugins atop the Eclipse Platform geared towards Java EE, and specifically, JBoss project development.

Once we fire up JBDS, we’re set with an empty slate.  By invoking the Forge shell within the IDE (Control-4), we’re met with a prompt ready to take our input.  From here we can start to put our project together.  Each action we take will be automatically reflected in the Project Explorer.  For this application, we’ll execute the following Forge commands:

new-project --named feedback --topLevelPackage org.cedj.javaone2012.feedback --projectFolder feedback;

That’ll make us a new project. Now we’ll enable JPA and Bean Validation:

persistence setup --provider HIBERNATE --container JBOSS_AS7
validation setup --provider HIBERNATE_VALIDATOR

With that behind us, we can create some Entities and set their properties and constraints:

entity --named FeedbackEntry
field string --named twitterHandle
constraint NotNull --onProperty twitterHandle
field string --named feedback
constraint NotNull --onProperty feedback

We’ll also let Forge generate some JSF View pages for us to play with later.

scaffold setup
scaffold from-entity org.cedj.javaone2012.feedback.model.*

Those who have been following this space should be aware of my involvement with the Arquillian Test Platform, so it’ll probably come as no surprise that I’d be interested in setting up the POM with Arquillian dependencies (to be executed against JBossAS7) and generating a test case upon which to build:

forge install-plugin arquillian
arquillian setup --container JBOSS_AS_REMOTE_7.X
arquillian create-test --class org.cedj.javaone2012.feedback.model.FeedbackEntry.java

Before we continue, let’s change the default in persistence.xml to allow us to preserve data between deployments:

<property name="hibernate.hbm2ddl.auto" value="update"/>

We’re now ready to build our application; Forge can execute the Maven lifecycle for us:

build --notest --profile JBOSS_AS_REMOTE_7.X;

We may also equip Forge and our project with the ability to interact with the application server:

forge install-plugin jboss-as-7
as7 setup
as7 start
as7 deploy

Assuming all went to plan, our application should be available in the browser at http://localhost:8080/feedback. Let’s put some data into our application for us to test. Pick whatever values tickle you and save them through the web interface.

When done, we can undeploy the application from the app server:

as7 undeploy

Now let’s change our generated FeedbackEntryTest to something more reasonable. Something like this:

@RunWith(Arquillian.class)
public class FeedbackEntryTest {
	@PersistenceContext
	private EntityManager em;

	@Deployment
	public static WebArchive createDeployment() {
		return ShrinkWrap.createFromZipFile(WebArchive.class, new File(
				"target/feedback.war"));
	}

	@Test
	public void canFindFeedbackByUser() {
		final FeedbackEntry feedback = em.createQuery(
				"from " + FeedbackEntry.class.getSimpleName()
						+ " where twitterHandle='@ALRubinger'",
				FeedbackEntry.class).getSingleResult();
		Assert.assertNotNull(feedback);
	}

	@Test
	public void testIsDeployed() {
		Assert.assertNotNull(em);
	}
}

This will deploy our application into the running server and ensure that we may find a user by the specified Twitter handle. If you’ve populated the database via the web interface above and are looking for the right handle in “canFindFeedbackByUser”, this should pass when executed from the IDE in “Run As > JUnit”.

So we’re managed to set up a minimal application, run it, and test it locally.  In the spirit of “release early, release often”, might as well make this public.

OpenShift

OpenShift is the Cloud-based PaaS offering from Red Hat.  It’s free to sign up, and provides hosting for a series of applications, Java EE apps on JBossAS7 included.

Once signed up, it’s easy to create a Domain in the JBoss Developer Studio “OpenShift Explorer”.  From there, “Create New Application…”

After logging in, you may set up the new JBossAS7 application on OpenShift via the IDE:

Once the new OpenShift Application is made, link it to the existing project in your workspace:

You’ll be asked if you’d like to connect your project to the OpenShift application’s Git settings (this is the version control system which backs the OpenShift deployment mechanism).  Agree.

Once your server is set up, you can find it in the “Servers” tab.  Right-clicking your application under the server will afford you the opportunity to “Publish”.

Do it.

After some pause while OpenShift uploads your application, builds it remotely, and deploys it, you’ll be able to open via the browser and share the link with the world.

With any luck, things will go just this smoothly at JavaOne. :)

S,

ALR

About these ads

5 comments

  1. [...] Rapid Java EE Development – Go from Scratch to Cloud by Andrew Lee Rubinger [...]

  2. ” I used to catch Ruby on Rails demonstrations build an app from scratch and was typically left as one of the dissenters in the audience who believed that an application which directly mirrored the CRUD operations of a domain model was essentially worthless.”

    I wholeheartedly agree. I tools like Forge need to provide the ground work, but they need to provide easy customization. Minor apps can exists with just CRUD functions. But most apps need more like custom screens, workflow, custom services, forms generated from Data, not just tables/columns, etc. In that respect, the domain entities seem like a better starting point, for most things, rather than a relational data structure that provides only part of the picture.

  3. Unlike the previous commentator, I think that Forge has a huge place in the tool belt of a developer that needs to initiate a project and does not want to slog through the error prone task of creating and configuring the project manually. Apache Maven archetypes was a step in the right direction in this regard and now Forge completes the picture. Of course each project is different but there is the 10% which is common and Forge automates this.

  4. While I previously thought that an autogenerated CRUD app had little merit, I’ve more recently begun to see it as a sensible starting point, especially for those still learning how the layers of Java EE are hooked together.

  5. Ruchir Rathore · · Reply

    Hi guys I am a beginner and received an email regarding JavaOne conference 2013 to be held in Hyderabad..
    Did anyone else receive it?? Any suggestions on the conference??
    I am hoping to attend several sessions about Core Java Platform; JavaFX, Java Embedded, and Java Card and many more. Fingers crossed nd waiting for JavaOne India 2013 Hyderabad May 8th and 9th. Be part of it, register today: http://bit.ly/YMPeJ8

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: