grails services « Intelligrape Groovy & Grails Blogs

Posts Tagged ‘ grails services ’

Using PostConstruct annotation with Grails Services

Posted by on August 27th, 2012

We can use PostConstruct with Grails Services and injected Spring Beans. This PostConstruct annotation can be used to annotate a method which needs to be executed after dependency injection to perform any initialization.

import javax.annotation.PostConstruct

class PostConstructDemoService {

    @PostConstruct
    private void init() {
          println "Initializing"
          //your initialization code goes here. e.g connect to some Messaging Service
     }
}

Check this for more details. We also have PreDestroy annotation.

Regards,
Ankur Tripathi
ankur@intelligrape.com

Posted in Design Pattern, Grails

Discovering grails goodness: Scoped Services

Posted by on December 29th, 2011

Recently, I got to learn about the scoped services in grails and I found it worth sharing. For instance: A Service marked as ‘session’ scoped would be instantiated once for a session and remains there throughout the lifetime of the session. This can be used to store user specific data in this service bean class.

class SessionDataService {
     static scope = 'session'
     Date lastLogin =new Date()
    String getInfo()
    {
        "lastLogin: ${lastLogin.format('HH:mm:ss')}"
    }
}

So, wherever I intend to use session for storing user data, I would rather use session scoped service.

Hope that helps.

Best Regards
Mohd Farid

Posted in Design Pattern, Grails

Using bindData in services

Posted by on August 11th, 2010

In one of my projects, I was working on rest calls, and one of the requirement was such that I had to pass the XML to service and then parse it and bind the relevant details to an instance of a domain object. In essence I had to call bindData in service after creating a Map from the XML. I soon realised we could not call bindData in service, its a method injected by grails only in controllers. After searching for stuff on google.. I found the way out, it was simple All we have to do is

import org.codehaus.groovy.grails.web.metaclass.BindDynamicMethod

and then in your method do

BindDynamicMethod bind = new BindDynamicMethod()
args =  [objectInstance, map, [exclude:['prop1', 'prop2']]]
        bind.invoke(objectInstance,'bind',(Object[]) args)

Here is the Link which I referred JT’s Blog it will give you a little more detail regarding using bindData in services.

Hope it helps.

With Regards
Sachin Anand
sachin@intelligrape.com

Posted in Grails

Overriding default Transactional Behaviour in Grails Services using Annotations

Posted by on April 28th, 2010

In a project, we needed to have fine tune the transaction behaviour on our Service classes. One service method was calling the a method in another service. The calling service method A was to happen in one transaction and the called service method B in its own transaction. This was required because we needed to persist the error log from the B() in a domain using A().

Even though an exception was thrown from B() and it was caught in A() and handled gracefully, some exceptions were thrown, which meant that the changes made in A() were also rolled back, which was not the behavior we required. After some investigation and taking cue from the mailing list , it was decided that the best way to go about it was to run B() in its own transaction. This was possible using the Spring Transaction Annotation, a property of which creates a new session for B().

This was done by adding the annotation

   import org.springframework.transaction.annotation.*
   class SampleService{
        static transactional = true
        @Transactional(propagation = Propagation.REQUIRES_NEW)
         def B(){
                   //..implementation which can throw exceptions. The logic in this method is run in a single transaction
        }
  }
 

And in A(), we have try catch blocks to handle the exception.

Hope this helps.

Posted in Grails