Thursday 23 July 2015

Variable Shadowing/Overriding : Java vs Groovy

As Groovy is really powerful and offers lots of features that makes life of a developer easy, it has become really popular and as it is "Java-like", learning curve from Java to Groovy is not very steep. As most fundamentals are same as Java, you just have to get familiar with Groovy offerings to do a certain thing that is quite laborious in Java.

For a Java developer moving to Groovy, there could be surprises, at times, in terms of what you expect(like Java) and what you get as output in Groovy. I am discussing one such use case here.

Let us consider a very simple use case - One parent class, one child class and a variable that is present in both.


If you execute this code in Java, output will be 10 and in case of Groovy it will be 50.

In Java, variables are bound to reference and only methods are overriden, but the case is not the same for variable in Groovy.

My initial thought was that using parent.a would ultimately call parent.getA() in Groovy as there are implicit getters and setters for all the variables. So I tried accessing the variable directly using parent.@a . Surprisingly this made no change in the output produced.

Still not sure whether this is actually a feature of Groovy or something that should be changed.

Would love to know more thoughts on this.. :)


Saturday 18 July 2015

AEM : Implement Queue Using RabbitMQ

Nowadays having an application with high availability has become a need rather than an add-on to our infrastructure.
Queuing is one way of achieving high availability.  With help of a queue, you can push data to the queue and consumer would read message from the queue and do necessary processing. Even if the instance that pushed message to queue(the producer) goes down, other instance can read message(consumer) from queue and process it further.
There are various queuing options available - ActiveMQ, RabbitMQ, Kafka and lots more. For now I have used RabbitMQ for this.
To know more about RabbitMQ, you can refer documentation. Once you have RabbitMQ server up and running(default password for server is guest/guest), you can follow these steps to integrate it with AEM:
  • Add an entry to your POM

There are some transitive dependencies that need to be taken care of. For that make following changes to maven-bundle-plugin in the pom:


  • Using RabbitMQ API, you can push messages to the queue.
  • Create a consumer that listens to the queue
Refer my github repo for sample application for this. In this sample app, I have utilized RabbitMQ for "Asynchronous Email Sending".

So queue things up and ensure that your application has high availability. :)

P.S. : You can comment here or on github in case you need help.

Thanks!!!
Vivek Sachdeva
AEM Certified Expert
Certified_Expert_Adobe_Experience_Manager_Developer_badge