Posts Tagged ‘groovy’

Groovy: The new buzz in the Java world

October 30th, 2010 No comments

Groovy is the dynamic language for the Java Virtual Machine.  Inspired from Java, Ruby and Python, Groovy is an object-oriented language written in Java. Groovy is designed to help you get things done on the Java platform in a quicker, more concise and fun way – bringing the power of Python and Ruby inside the Java platform

Groovy’s addition to the application is as of enhancing the power and capabilities of java.

Like Java, everything is an object in Groovy.

The java virtual machine has no idea that its running the groovy code. Groovy is written in Java. As java library is called by the name JDK, the groovy library is enclosed and called by the name GDK – Groovy Development Kit.

It complements the productivity of the java programmers through its powerful features. Read more…

Categories: Uncategorized Tags:

Groovy Tutorial – Calling a stored procedure

December 2nd, 2009 No comments

Calling a stored procedure from a Sql server MySQL database or any other database in groovy is simple. First we need a datasource which spring could provide for us. Do the necessary datasource mapping in the resources.xml ( spring folder) in your grails folder &/Or datasources.groovy.

In service, use the datasource to call a store procedure as below example

import groovy.sql.Sql
class TestService{
def dataSource // using the datasource we define in the spring's resources.xml

/**Calling Procedure with in-parameters only- use sql.query**/
def abcList = { params ->
Sql sql = new Sql(dataSource)
def clientkey = (SecurityUtils.getSecureClientKey(params) !=
//calling proc that returns resultset
sql.query("{call svr_RetrieveClientPreferences(?)}",[clientkey]) { rs ->
if (rs != null) {
int i = 0
while ( {
/ *all functionality ...... */

/**Calling Procedure with in & Out parameters - use **/
def abcCalc = { params ->
Sql sql = new Sql(dataSource)
guid =
userKey = params.user
//calling proc that returns out parameters"{call gdx_ReadSessionData(?,?,?,?,?,?)}",
[guid, userKey, Sql.out(Sql.INTEGER.type),
{ date,userKeyCode,msgCode,retCode -> /* The out parameters will be returned in the same order it passed to procedure */
userKey = userKeyCode
println(" return code" + retCode)

in-Out Param can be passed as Sql.out(Sql.<DATATYPE>.type) OR Sql.<DATATYPE> .
See more for DATATYPE here –
Some issues with groovy sql; where we want to use out parameters & resultset both as a return of proc execution.

In this case we have to use the jdbc way (callable stmt) where stored procedure that return ResultSet and OUT parameter.

Categories: Uncategorized Tags:

Groovy interview questions

October 2nd, 2009 1 comment

Groovy Guru has  added groovy mock test / groovy interview questions on gr0ovy at skill-guru.  Take the test and do give us the feedback

Categories: Uncategorized Tags:

Groovy Closures

August 31st, 2009 No comments

Groovy supports closures and they are very useful when we create Groovy applications.

The most important difference between a normal function/method and a Closure is that Closures can be passed onto other functions as arguments and they serve as Callbacks to the calling function.

For example we can pass closures as arguments to methods to execute them. We can create closures ourselves, but we can also convert a method to a closure with the .& operator. And we can use the converted method just like a normal closure. Because Groovy can use Java objects we can also convert a Java method into a closure.

syntax to write Closure-

def closure1 = {params ->
//statements & logic
//type 1  - closure without any parameter
def closure2 = {
    println "Test Only"
//type 2 – closure with two param
def closure3 = { String name, int age ->
    println "My name is : $name and age is: $age"

A Closure is usually defined within the braces {… }. Parameters defines the list of parameters that are to be passed for the closure. The symbol '->' is used to separate the Arguments with the set of statements in the Closure Definition.
call a Closure Definition- args )
closure1(args )

Test Only
Test Only
closure3("sadhna", 30)"saisha", 3)

My name is : sadhna and age is: 30
My name is : saisha and age is: 3

Like ‘this’ keyword which refers to the current object, there is ‘it’ keyword which, when used within a Closure Definition refers to the default first parameter being passed to the method. The following code will prove that,

def closure4 = {
    println it
}"Test Again")

Test Again

Java  method  to  groovy closure

public class Test {
 public static void javaTest(final String s) {
  System.out.println("Java --" + s + "!");

With the following script we use this Java class and convert the javaTest method to a closure:
// Simple list with names.
def names = ['groovy', 'grails', 'closure']
// Simple closure.
names.each { println 'Closure --' + it + '!' }
// Groovy method to convert to closure.
def groovySays(s) {
 "Groovy --${s}!"
// Use .& syntax to convert method to closure.
// Convert Java method to closure and use it.
def javaTest = Test.&javaTest
names.each javaTest

Closure --groovy!
Closure --grails!
Closure --closure!
Groovy --groovy!
Groovy --grails!
Groovy --closure!
Java --groovy!
Java --grails!
Java --closure!
Categories: Uncategorized Tags:

Grails – A breath of fresh air for Java people

June 26th, 2009 1 comment

I’ve been working in J2ee/java technologies from 8 years and really loved it. Never wanted to switch or look anything beyond java but started working on Groovy & Grails recently, few months ago, and found that in terms of developer productivity it seems to be light years ahead of other Java solutions (spring, Struts, EJB, JSF, JSP).

What is Groovy

Groovy is a dynamic language which runs on the Java platform. It has Java-like syntax, although it’s not fully compatible with Java. (i.e. many Java programs are valid Groovy code, but not all.)Consider Groovy if you’re looking for a scripting language with dynamic language features and must deploy your applications on a JVM.

What is Grails

Grails is an open source web framework for the Java platform. However, unlike other MVC model framework-

  • Grails domain classes are automatically persist able and can even generate the underlying database schema.
  • Services and other classes can be automatically injected using dependency injection based on naming conventions.
  • Grails controllers are request-scoped, which means a new instance is created for each request.
  • The default view for Grails is Groovy Server Pages (GSP) and typically renders HTML. The view layer also includes a flexible layout, a templating feature, and simple tag libraries.

How is Grails different from java

  • Groovy is not evolution of Java but groovy compliments java.
  • Grails offers some of the best features with seamless Java, Spring & Hibernate integration allowing you to scale in terms of complexity as your application grows.
  • Groovy and Grails feel very natural to a Java programmer
  • Grails definitely has its own unique features such as dynamic tag libraries, a view technology that is not scriptlet based, the ability to use dynamic finders and persistence methods on any Groovy or Java class and its tight integration with Java, Spring & Hibernate.
  • You can develop a Grails application without knowing anything about Spring, Hibernate or SiteMesh
  • A Java development like Struts & EJB is a very configuration heavy approach, with Grails you can build an entire application and only ever have to configure the data source.
  • Groovy & Grails are very easy on the surface and allow you to be expressive and creative as a developer. They don’t, however, take away the underlying power of the JVM and frameworks like Spring & Hibernate.

Grails and in particular Groovy are very close to Java. A few quick lessons in Groovy and a Java developer and quickly feel at home.

The only real pitfall is that it’s based on Groovy and not on Java are –

  • less powerful IDE support.
  • some errors caught by Java at compile-time appear only at runtime in Groovy – this is by design and not a fault of Groovy.

Want to get started with grails ? Here is the Introduction to grails tutorial

Categories: Uncategorized Tags: