Gradle, WildFly 8 with Eclipse

Hello World,
Searching for the web it’s hard to find a good ready to use initial project structure, so I made my own project set up. A little bit of copy paste, a little bit of research and some of it I learn from mistakes in the past. I call the project Ugly, because nothing is smooth when you combine more then one component-oriented software (a.k.a frameworks) in one project.
Project Structure (classical approach)

  • ugly-web, is for web presentation (a.k.a web pages)
  • ugly-ejb-services, is for ejb business components
  • ugly-dao-services, is for your dao objects
  • ugly-entities, is for your domain objects a.k.a database tables mapped in classes
  • ugly-common,  is for common classes used by all application layers
  • ugly-data-generator is for generating test data and also proving the correctness of your domain model

Gradle is a new build tool, which in couple of years will replace Maven as de-facto build tool.It’s easy to use even if you don’t know Groovy language (like me) and setup a project structure and start working 🙂
Whole build.gradle script

description = "Ugly project because awesome is too fucking mainstream"
apply from: ''

allprojects {
	apply plugin: "java"
	apply plugin: "eclipse"
	apply plugin: 'eclipse-wtp'
	sourceCompatibility = 1.7
	targetCompatibility = 1.7
	version = '1.0'
	repositories {
			url ""
		mavenRepo name: 'jboss-nexus', url: ""

	configurations {
	sourceSets {
	    main.compileClasspath += configurations.provided
	    test.compileClasspath += configurations.provided
	    test.runtimeClasspath += configurations.provided
	eclipse {
	  classpath {
	    plusConfigurations += configurations.provided


// Domain projects
project(":ugly-entities") {
	dependencies {
	  provided "org.hibernate:hibernate-core:4+"
	  provided "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1+"
	  provided "javax.validation:validation-api:1+"
	  provided ""
	  compile project(":ugly-common")

project(":ugly-data-generator") {

	dependencies {
	  // Dependencies for all entities
	  compile project(":ugly-entities")
	  compile project(":ugly-dao-services")
	  compile "org.fluttercode.datafactory:datafactory:0.8"
	  compile "org.hibernate:hibernate-core:4+"
	  compile "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1+"
	  compile "javax.validation:validation-api:1+"
	  compile ""
	  compile "org.hibernate:hibernate-entitymanager:4+"
	  compile "postgresql:postgresql:9+"

project(":ugly-common") {
	dependencies {
	  // Dependencies for all entities

// EJB Projects
project(":ugly-ejb-services") {
	dependencies {
	  // Dependencies for ejb services
	  compile project(":ugly-dao-services")
	  provided "org.jboss.spec.javax.ejb:jboss-ejb-api_3.1_spec:1+"
	  provided "javax.enterprise:cdi-api:1+"
	  provided "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1+"

project(":ugly-dao-services") {
	dependencies {
	  // Dependencies for daos
	  provided "org.hibernate:hibernate-core:4+"
	  provided "org.hibernate:hibernate-entitymanager:4+"
	  provided "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1+"
	  provided "postgresql:postgresql:9+"
	  provided "org.jboss.spec.javax.ejb:jboss-ejb-api_3.1_spec:1+"
	  provided "javax.enterprise:cdi-api:1+"
	  compile project(":ugly-entities")

// WEB Projects
project(":ugly-web") {
	apply plugin: 'eclipse-wtp'
	apply plugin: 'war'

	eclipse {
	  wtp {
	    facet {
	      facet name: '', version: '1.7'
	      facet name: 'jst.web', version: '3.1'

	dependencies {
	  // Dependencies for web app
	  compile project(":ugly-ejb-services")
	  provided ""
	  provided "javax.enterprise:cdi-api:1+"
	  provided "org.jboss.spec.javax.ejb:jboss-ejb-api_3.1_spec:1+"
	  provided "org.jboss.resteasy:resteasy-jaxrs:3+"
	  provided "com.googlecode.htmleasy:htmleasy:0.7"
	  provided "javax.servlet:jstl:1+"
	  provided "jstl:jstl:1+"
	  provided "taglibs:standard:1+"
	  provided "javax.servlet:servlet-api:3+"
	  provided "javax.servlet:jsp-api:2+"

	  provided "org.hibernate:hibernate-core:4+"
	  provided "org.hibernate:hibernate-entitymanager:4+"
	  provided "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1+"


How let examine the script step by step.
First we apply gradle plug-in for easy creating couple of projects like Java, Java Web, Scala, Groovy. It’s nice plug-in if you don’t want to use IDE support for creating projects

apply from: ''

Next section is called allprojects and here we define all common properties for all projects. In this section we apply plug-ins for java, eclipse and eclipse-wtp. The last plug-in eclipse-wtp provides useful features for customizing the nature (Project Facets) of eclipse project. After that we override value on default properties, which in our case are important and need to set on every project. These properties are sourceCompatibility, targetCompatibility and version. There names are self-explanatory and in my case they are set to the latest java 1.7 and the version is for project version which is by default 1.0 🙂 The next thing we setup the repositories for dependency management and enable provided feature for jars to all projects. Provided isn’t available in Gradle by default, so we need to implement which is easy (if you Google currently). In configuration section, we declare provided then in sourceSets we added provided dependency to classpaths. This is only for java plug-in, which in most cases isn’t good because not everybody use text editors to develop java, most of developers use IDE, which in my case is Eclipse, so we append the same in eclipse plug-in. With this step we finished with building foundation for our ugly projects. Now it’s time to define them in ground-up fashion.
First project that we explore is ugly-common and its is share by all projects. The purpose of this project is to put classes which can be share from all project like Convertors, Utilities, Exceptions etc. This project in my case doesn’t have any jar dependency, but when your project grow in size it’s nice to have some utility jars like Apache Common or other similar jars.

Next project is ugly-entities which serves as separate logical unit for our domain objects. The dependency is for rs bindings like @Form and @FormParam, because our domain object will be reused in web-ui.
Project ugly-dao-services contains dao services, which are part from dao layer and these services execute database operations.
Project ugly-ejb-services contains ejb services and project ugly-web contains web UI for the application.In our case for MVC pattern we use resteasy (Controler), htmleasy (View dispatcher) and jsp (View renderer).

In most cases, developers will use Spring MVC, but if you don’t use Spring eco-system like in my case, adding Spring MVC to your project will add half of spring core projects :). In order to emulate MVC pattern (use similar clean programming API) like in spring MVC, we only need htmleasy as View dispatcher, because other things are provided by wildfly standard distribution. Even without htmleasy we can emulate spring MVC programming api, but in that case we need to make 5-6 classes, which is already done by htmleasy (only 20k in size).

At last, we need to start coding our ugly project in eclipse, so for that purpose we have four command, which you need to execute in order to import gradle project to eclipse

  • gradle clean
  • gradle build
  • gradle cleanEclipse
  • gradle eclipse

After this will import project to eclipse, as gradle projects as shown in the picture below

Here is the link to svn source repository
Ugly project svn repo
In next posts we will explore ugly sub-projects in more detail way and there are some interesting concepts for reducing complexity of the building software.


Why “Agile” and especially Scrum are terrible

Michael O. Church

It’s probably not a secret that I dislike the “Agile” fad that has infested programming. One of the worst varieties of it, Scrum, is a nightmare that I’ve seen actually kill companies. By “kill” I don’t mean “the culture wasn’t as good afterward”; I mean a drop in the stock’s value of more than 85 percent. This shit is toxic and it needs to die yesterday. For those unfamiliar, let’s first define our terms. Then I’ll get into why this stuff is terrible and often detrimental to actual agility. Then I’ll discuss a single, temporary use case under which “Agile” development actually is a good idea, and from there explain why it is so harmful as a permanent arrangement.

So what is Agile?

The “Agile” fad grew up in web consulting, where it had a certain amount of value: when dealing with finicky clients who don’t know what they…

View original post 3,300 more words

EAGER fetching is a code smell

Vlad Mihalcea


Hibernate fetching strategies can really make a difference between an application that barely crawls and a highly responsive one. In this post I’ll explain why you should prefer query based fetching instead of global fetch plans.

Fetching 101

Hibernate defines four association retrieving strategies:

Fetching Strategy Description
Join The association is OUTER JOINED in the original SELECT statement
Select An additional SELECT statement is used to retrieve the associated entity(entities)
Subselect An additional SELECT statement is used to retrieve the whole associated collection. This mode is meant for to-many associations
Batch An additional number of SELECT statements is used to retrieve the whole associated collection. Each additional SELECT will retrieve a fixed number of associated entities. This mode is meant for to-many associations

These fetching strategies might be applied in the following scenarios:

  • the association is always initialized along with its owner (e.g. EAGER FetchType)
  • the uninitialized association…

View original post 717 more words

Logical vs physical clock optimistic locking

Vlad Mihalcea


In my previous post I demonstrated why optimistic locking is the only viable solution for application-level transactions. Optimistic locking requires a version column that can be represented as:

  • a physical clock (a timestamp value taken from the system clock)
  • a logical clock (an incrementing numeric value)

This article will demonstrate why logical clocks are better suited for optimistic locking mechanisms.

System time

The system time is provided by the operating system internal clocking algorithm. The programmable interval timer periodically sends an interrupt signal (with a frequency of 1.193182 MHz). The CPU receives the time interruption and increments a tick counter.

Both Unix and Window record time as the number of ticks since a predefined absolute time reference (an epoch). The operating system clock resolution varies from 1ms (Android) to 100ns (Windows) and to 1ns (Unix).

Monotonic time

To order events, the version must advance monotonically. While incrementing…

View original post 638 more words

A beginner’s guide to JPA/Hibernate flush strategies

Vlad Mihalcea


In my previous post I introduced the entity state transitionsObject-relational mapping paradigm.

All managed entity state transitions are translated to associated database statements when the current Persistence Context gets flushed. Hibernate’s flush behavior is not always as obvious as one might think.


Hibernate tries to defer the Persistence Context flushing up until the last possible moment. This strategy has been traditionally known as transactional write-behind.

The write-behind is more related to Hibernate flushing rather than any logical or physical transaction. During a transaction, the flush may occur multiple times.

The flushed changes are visible only for the current database transaction. Until the current transaction is committed, no change is visible by other concurrent transactions.

The persistence context, also known as the first level cache, acts as a buffer between the current entity state transitions and the database.

In caching theory, the write-behind synchronization…

View original post 414 more words

Setting up Wildfly8 Cluster in 5 minutes with Chef and Vagrant

Hi! This is my first post of this blog and I would like start with my last cookbook “wildfly-clu”.   We will create a simple Wildlfy cluster(domain mode) composed by 3 servers. For this test I will use CentOS release 6.3 (Final).

Final result:

You can reach the app directly from the nodes:



Quick HowTo:
check if the vagrant-berkshelf plugin is installed. If not, launch ” vagrant plugin install vagrant-berkshelf”
git clone
cd wildfly-clu
vagrant up

Detailed Description:

Node1 (myserver1) =>Domain controller –  Application Server – Reverse proxy

Node2 (myserver2) => Slave – Application Server

Node3 (myserver3) => Slave – Application Server

Prerequisites:  Virtualbox 4.3.10 ,Vagrant 1.4.3 , Ruby 1.9.3,Git

Let’s start..

1. Configure your /etc/hosts in order to resolve locally the name of…

View original post 733 more words

Caching best practices

Vlad Mihalcea


There is an irresistible attraction to writing custom caching solutions, since it seems to be the easiest path to “improving” the overall application performance. Well, caching is a great technique, but there are few steps to consider before even considering it.

Best practices

  1. A key/value collection is not a Cache

    Almost all projects I worked on have been using some sort of custom caching solutions, built on top of Java Maps. A Map is not an out-of-the-box Caching solution, since a Cache is more than a key/value store. A Cache also requires:

    • eviction policies
    • max size limit
    • persistent store
    • weak references keys
    • statistics

    A Java Map doesn’t offer these features and you shouldn’t spend your customer’s money to write a custom cache solution either. You should choose a professional cache like EHCache or Guava Cache, which are both powerful and simple to use. Those tools are constantly tested…

View original post 476 more words

Hibernate Facts: Multi level fetching

Vlad Mihalcea

It’s quite common to retrieve a root entity along with its children associations on multiple levels.

In our example we need to load a Forest with its Trees and Branches and Leaves, and we will try to see have Hibernate behaves for three collection types: Sets, Indexed Lists, and Bags.

This is how our class hierarchy looks like:


Using Sets and Indexed Lists is straight forward since we can load all entities by running the following JPA-QL query:

and the executed SQL query is:

But when our children associations are mapped as Bags, the same JPS-QL query throws a “org.hibernate.loader.MultipleBagFetchException”.

In case you can’t alter your mappings (replacing the Bags with Sets or Indexed Lists) you might be tempted to try the something like:

But this is inefficient generating a plethora of SQL queries:

So, my solution is to simply get the lowest level children and fetch all needed associations…

View original post 281 more words