Tuesday, 15 March 2011

Basics of Java's Garbage Collection

Garbage collection:

Every java program runs as a process and is allocated some memory. The memory allocated to the java process is called process heap.

The java heap i.e. the java object heap, is part of the process heap. The java object heap (also called just java heap) store the java objects i.e. object instances. The java heap can be configured using the several command line parameters.

Garbage collection is the mechanism used by java to reclaim or recollect the space occupied by objects that are not reachable from the program. This is necessary to ensure that the application does not run out of memory.

Friday, 4 March 2011

Custom error page for your grails application

I recently added a custom error page to my application, so that the users don't see the stacktrace and are shown a friendly message in case of a runtime error.

Thursday, 3 March 2011

Get started with Spring JMS using ActiveMQ

Messaging is a mechanism to create and exchange data/information/objects within/between applications. An application that generates some useful data can send it's messages (objects) to an agent and these messages (objects) can then be read from another application. JMS is a Java API that enables applications to be able to communicate with each other using the mechanism described above.

You can read more about JMS here.

In this article, we will see how you can quickly start using JMS using the Spring JMS framework. Spring provides template based APIs which greatly simplify the use of JMS. A lot of work is done behind the scenes which saves the developer time to concentrate on the business requirement.

We will take a look at the important Spring JMS classes and apis as we go through an example. So let's get started!

Friday, 25 February 2011

Integrating Grails and Hudson

The best way to ensure consistent code quality and system stability is to integrate your application with a Continuous Integration system. Hudson is one such CI system.

In this tutorial, i will give a small demo on how to integrate Grails with Hudson. I will assume you are familiar with the grails technology and have some basic idea of what continuous integration systems are.
Note: This is not a tutorial on Hudson!

Thursday, 24 February 2011

Exploring BootStrap.groovy

You may come across a scenario where you want to carry out a task every time your application starts or stops.

You just need to peep inside your grails-app/conf folder to find a groovy file called BootStrap.groovy.
BootStrap.groovy is a simple plain groovy script with two important closures : 'init' and 'destroy'. These closures are called upon when the application starts up and shuts down respectively.

Monday, 21 February 2011

Problem setting the grails proxy ?


If you are using grails on a windows box, you may face an issue while setting the grails proxy using the 'grails add-proxy' command. This is because of the way windows treats any paramters that you pass using the -- option and sets the host name and the port to boolean values.

Thursday, 10 February 2011

JDBCException due to stale connections in Grails

If your grails application remains idle for too long or the database connections remain idle for too long or the database is restarted you may see the following exceptions:

ERROR org.hibernate.util.JDBCExceptionReporter - No more data to read from socket
ERROR org.hibernate.util.JDBCExceptionReporter - OALL8 is in an inconsistent state
ERROR org.hibernate.util.JDBCExceptionReporter - Io exception: Broken pipe
ERROR org.hibernate.util.JDBCExceptionReporter - Already closed.
These issues are intermittent, that is, they may disappear when you try hitting again.
This issue occurs because the application tries to use a database connection that has become stale and should have been evicted.

java.lang.NoClassDefFoundError when sending async emails in Grails

Exception while sending emails in production environment.

If you are trying to send emails from your application asynchronously, using the Mail plugin, in the production environment you may see an exception like:
java.lang.NoClassDefFoundError: org/springframework/mock/web/MockHttpServletRequest 
This is because of a bug in the Mail plugin. This plugin uses the MockHttpServletRequest class from the spring framework test jar which is not bundled when you run the 'grails prod war' command.

Wednesday, 9 February 2011

RESTful webservices with Spring

REST (Representational State Transfer) is a stateless architectural style wherein resources are identified and manipulated. by their URIs and transferred over HTTP. A REST web service sends any parameters, data needed by the server within the HTTP headers and body.
In this article, we will build REST webservices with Spring and a client using the Spring RestTemplate to consume the webservice. I will not be going into the details of these technologies, the aim is to rapidly come up with a basic CRUD web-service.

I will be assuming, that you are familiar with the basics of Spring, DI and IoC, REST, and the basic web-application structure in java.

Without much ado, let's begin!

Monday, 7 February 2011

Read from a database and write into an xml using Groovy

You may come across a use-case where you need to read some data from a database and generate an xml report or file that needs to be sent across to be processed by some other system.

Doing this using a java program could be relatively overwhelming; with Groovy you can do this in a matter of minutes!

Let's take a look at how you can convert data returned from a sql query into an xml file.

Add search to your grails application using the searchable plugin

If you're building a web application, you're most certainly going to need some 'search' capabilities in your application. The searchable plugin is an amazing plugin that is built using Compass search engine and lucene. This plugin enables you to make your domain classes without writing almost any code. In this article we will be discussing how you can install and use this plugin, and also discuss some issues around it.

We will be creating a simple grails application, a couple of domain classes, and then we will explore the 'searchable' plugin. (find the source code of the sample application at the end of the article)

Let's get started!

Tuesday, 1 February 2011

Spring Hibernate Integration

In this article, I will be demonstrating how you can integrate hibernate with your Spring application. We will not be discussing hibernate and Spring in any detail. The focus is to get you started with an Spring application that persists data into a database using the hibernate ORM layer.

I will safely assume that you are familiar with Spring, hibernate, SQL, maven. For the purpose of this demo, i will be using the MySQL database, STS (SpringSource Tool Suite) or Eclipse with Spring support plugins installed. If you are not using any IDE, you just need to create some folders and config files yourself. But i see no reason why you shouldn't since it will really speed your development process.
(find the source code of the sample application at the end of the page)

So Let's get started!

Get started with Spring MVC

Spring MVC, one of the modules of the core Spring framework,  is a web-application development framework. As the name suggests, Spring MVC is an implementation of the Model(M), View(V), Controller(C) design pattern, which is based on request delegation and separation-of-concerns.
I am assuming, that if you have reached this article and are about to use it, you would be familiar with the basics of web-application development and the Spring framework in general. And Java of course ;)

I will briefly introduce you to the basics of how Spring MVC works and then will build a simple web-application (find the sample application source code for download at the end of the page).

Monday, 24 January 2011

Build your first grails application

Grails is an open source web application framework, that has been built with the aim to enable rapid application development (RAD). Grails uses groovy as it's primary programming language. It uses Convention over Configuration to speed up the process of web application development. Grails takes its inspiration from Ruby on Rails but for the java world. Grails runs on the JVM and brings the power of the scripting world since it uses groovy which is a dynamic language built on top of java. Read more about grails and it's features here.

In this article we will quickly build an application using Grails. You will be surprised to see that you have your web-app running within 15 minutes, save data to the database, even deploy you application on your favourite application server! Enough talking, let's get into action.
( find a download link for the source code of the sample plugin at the bottom of the page)

Thursday, 20 January 2011

Welcome Grails to your life!

We all know how painful web development in java can be! Maintaing endless configuration xml files, writing hibernate layers & complex build scripts for compiling & packaging can make your experience of developing a web app in java nightmarish. But we would not want to sacrifice the capabilities and power of the Java platform for the sake of ease and agility.

And this is where web frameworks like Grails fit in. These framework take care of the overhead tasks such as configuration, builds, integration, and enable us to concentrate on our business requirement. 
If you are looking for a framework that supports agile development and enables RAD (Rapid application Development), Grails could be your first choice!

Monday, 17 January 2011

Create your first grails plugin

Crucial to the success of grails, is its support for plugins. Grails was designed keeping pluggability in mind right from the start. Ease in development, testing, building and distribution of plugins has led to a larger community interest in the framework, with loads of plugins available for developers to choose from.

If you’ve worked with grails, chances are you’ve used at least one grails plugin such as hibernate, logging, acegi, grails UI, mail, searchable etc. Grails maintains a plugin repository that you can search to find a plugin most suitable for your requirements. But have you ever found yourself in a situation where you’re rewriting the same code in multiple views, or duplicating code between controllers and services and even between multiple grails applications? If yes, this could be your chance of trying your hand at a plugin!

I’m presenting a very basic tutorial to create a very simple grails plugin, with a service and a taglib that can be reused across grails applications.( find a download link for the source code of the sample plugin at the bottom of the page)