Shortlink

Revisiting JEE after a long gap

After a long gap, there was an opportunity to jump into core JEE last week for a client. Though lots have been written about this here’re are some observations about it.

1) To begin with, after working with Groovy and Grails playing with JEE6 gave a stone-age feel :)
2) Servlets with annotations is an absolute breeze now. No more web.xml configuration required.
3) The tag libraries .tag files for creating custom composite components is a welcome feature in JSP.
4) The dependency injection feature using @Resource, @EJB, @PersistenceUnit annotations save lot of code. It will remind you of Spring framework though.
5) Not many changes in the way EJBs are created using annotations and it was still making developers scratch their head, however.
5) JMS still has the same old boring piece of code to produce and consume messages. Create connection factory, connection, session, publisher, message objects etc. a long list of tasks.
6) Creating SOAP-based web services is incredibly easy, however the RESTful approach still needs some plumbing work.
7) Eclipse Indigo and Juno(the latest version) have reduced the effort of working with JEE6 tremendously.
8 ) The JPA implementation of Eclipse, EclipseLink helps you get started working with ORM asap, as you don’t have to configure framework like Hibernate for playing with the API.
9) JBoss AS7 has undergone a complete revamp. In fact, the modular(modules) structure, took lots of time to get a hang of it.
10) JBoss Tools plugin for Eclipse made us craving for more RAM and processor speed.
11) All of us started feeling the need for Spring framework halfway through using any API.

On a personal note, I missed Groovy a lot, because had to write so many lines of code to even implement a simple piece of functionality like inserting a record in the database.

Shortlink

Callbacks() in jQuery

There are different operations that you want to perform on some data. The functionalities that you want to perform will depend on the user input or actions. Now, the implementation looks like normal function calls based on some conditions. Things can get tedious if the number of operations increases, though. The Callbacks() in jQuery provides a sophisticated approach for handling this.

jQuery provides a $.Callbacks() method that can be used to hold a list of callback functions. The functions can be added and removed at will. You can fire the functions at one shot by passing in the data.

Say you have three functions add, subtract and multiply as shown below.

function add(a,b){
	console.log(a+b);
}
function subtract(a,b){
	console.log(a-b);
}
function multiply(a,b){
	console.log(a*b);
}

You can add these function to $.Callbacks() object using add() method and fire them all by passing the numbers using fire() method as shown below.

var operations = $.Callbacks();
operations.add(add);
operations.add(multiply);
operations.add(subtract);
operations.fire(20,30);//invokes all the methods

Callbacks() also has few more useful methods like remove(), fireWith() etc that you can read from the jQuery documentation.

You can view this example with UI and conditions here. Play with this example by only providing valid numerical data :)

Shortlink

Few useful utilities in GORM

You will always learn something new, whenever you work with Grails. It doesn’t fail to surprise you everytime. Last week’s client visit made me sit up and notice few more useful methods in GORM.

failOnError:true.An useful property when applied while saving/updating an domain instance gives you the error messages in the console. Say you have a Person class that you want to instantiate and save to the database. You can write

p1 = new Person(name:"Sam")
p1.save(failOnError:true)

addToXXX method.An useful method to add collections in a domain class. Say Person class has a dogs collection and you can add Dog objects by using addToDogs method like shown below.

	p1 = new Person(name:"Sam")
	p1.addToDogs(new Dog(name:"Snoopy"))
	p1.save(failOnError:true)

These two options though very simple come in very handy in applications

Shortlink

Interesting article on Java 8

Ever since dynamic languages like Groovy started making so much noise accompanied with JavaScript, the pressure has been on Java language to revamp itself. C# has been doing that pretty smartly over the years. C# started off as a pale copy of Java, has grown tremendously with features like lambda expressions, closure support, LINQ etc.,

Now that it’s time for the Java language to come of age, Java 8 has been showing a lot of promise. infoQ has come up with an interesting article on Java 8, which compares it with the Scala language. Looks like Java 8 takes a cue from Scala. The promised lambda expressions and closures in Java 8 will atleast make C# programmers smile scornfully.

You can read the article from this link.

Shortlink

DSL with Groovy, another one

In one of the earlier posts we created a simple query using Groovy DSL. Let’s implement another example as shown below using the DSL capabilities of Groovy. Let’s create User.groovy with the code shown below.

//User.groovy
Calculator.compute {
			addition 2,3
			subtraction 10,4
			multiplication 100,40	
		  }

You want to add two numbers, subtract 2 numbers, multiply 2 numbers and finally print the result of all these computations.

Understanding this code involves demystifying the Groovy syntax here. The decoded syntax is shown below.

Calculator.compute({
			addition(2,3)
			subtraction(10,4)
			multiplication(100,40)	
		  })

compute is a static method in Calculator class that accepts a closure as argument. The closure has calls to addition, subtraction and multiplication methods. Now let’s implement the Calculator class.

class Calculator {
	static results = []
	static compute(block){
		def calc = new Calculator()
		calc.with block//Key
		results.each {println it}
	}
	def addition(a,b){
		results << "Addition of ${a} and ${b} is ${a+b}"
	}
	def subtraction(a,b){
		results << "Subtraction of ${a} and ${b} is ${a-b}"
	}
	def multiplication(a,b){
		results << "Multiplication of ${a} and ${b} is ${a*b}"
	}
}

Calculator class has the addition, subtraction and multiplication methods that add the result to a collection. The compute method accepts the block of code as the argument. You can invoke the block of code by writing block(), but it would look for the addition… methods in the invoking class. In other words, the “delegate” for this closure would be the “User” class. Therefore it would look for addition… methods in the User class.

In order to change the delegate to Calculator instance, we create an instance of Calculator, clone the closure and invoke it with that Calculator instance. All these things can be implemented using one line of code using “with“.

calc.with block

The closure would now look for the addition… methods in the delegate class ie., the Calculator.