How to create a custom Spring Boot FailureAnalyzer

Today, I was working with a Spring Boot application that does local JVM cache warming on the server start up. Application was calling a global Redis cache and storing state that does not change often in an in-memory JVM cache. It is a common pattern that many applications use. In our case, application not only just warm the cache but it also first process some data and then cache the result in the local JVM cache.

Many times junior developers forget to start redis or any other depending service and then application fails to start on their local machine. Then, they need to spend few minutes reading the long Java stack trace to find the problem. These stack trace can be quite long. And, it is difficult to find needle in this haystack.

Recently, I learnt about a Spring Boot feature called FailureAnalyzer. FailureAnalyzer allows you to intercept exceptions that occur at the start-up of an application causing an application startup failure. Using FailureAnalyzer you can replace the stack trace of the exception with a more human readable message. The best example of this is when your code has cyclic dependencies. A common example of cyclic dependency is a bean A depending on bean B and vice versa as shown below.

Continue reading “How to create a custom Spring Boot FailureAnalyzer”

Day 22: Developing Single Page Applications with Spring, MongoDB, and AngularJS

Today for my 30 day challenge, I decided to develop a single page web application using the Spring frameworkMongoDB, and AngularJS. I have a good understanding of Spring and MongoDB but I have never used AngularJS with the Spring framework. So, in today’s blog post we will develop a social bookmarking application like the one we developed with EmberJS a few days ago. I have already covered AngularJS basics on day 2 so please refer to my blog for more information. This blog will cover the latest version of the Spring framework i.e. 3.2.5.RELEASE and will use no XML approach(not even web.xml). We will configure everything using Spring annotation support. The Spring MVC(along with Spring framework) will be used to create the RESTful backend. AngularJS will be used as the client side MVC framework to develop the frond-end of the application. Read full blog here https://www.openshift.com/blogs/day-22-developing-single-page-applications-with-spring-mongodb-and-angularjs

Day 13: DropWizard–The Awesome Java REST Server Stack

I have mainly been a Java guy throughout my 8 years as a software developer. For most of the applications I have written, I used the Spring framework or Java EE. Lately, I am spending time learning web development in Python, and one thing that has impressed me a lot is the Flask framework. The Flask framework is a micro-framework which makes it very easy to write REST backends. Today for my 30 day challenge, I decided to find a Java alternative to Python’s Flask framework. After doing some research, I discovered that the DropWizard framework can help me achieve the same productivity as the Flask framework. In this blog, we will learn how to build a RESTful Java MongoDB application using DropWizard. Read the full blog here https://www.openshift.com/blogs/day-13-dropwizard-the-awesome-java-rest-server-stack

DevFest Austria 2013 Talk : Thinking Beyond RDBMS — Building Polyglot Persistence Java Applications

Couple of weeks back I gave a talk at DevFest Austria event on how to use different nosql datastores with OpenShift.  You can view the video here.

Spring Test Failing with A ServletContext is required to configure default servlet handling

If you are getting exception shown below in your Spring test

Caused by: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.(DefaultServletHandlerConfigurer.java:54)
at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping(WebMvcConfigurationSupport.java:330)
at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerByCGLIB$$c733f512.CGLIB$defaultServletHandlerMapping$22()
at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerByCGLIB$$c733f512$$FastClassByCGLIB$$b76778f9.invoke()
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:285)
at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerByCGLIB$$c733f512.defaultServletHandlerMapping()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:160)

Then to fix this error migrate to Spring 3.2 release. Change the maven dependency as shown below

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>3.2.0.RELEASE</version>
</dependency>

Deploy war on tomcat running on OpenShift

In this short blog, I will show you how you can deploy war file on Tomcat running on OpenShift. For Java, OpenShift supports three deployment options – JbossAS-7, JBossEAP-6, and JBossEWS-1.0 or Tomcat. In all of the three options it supports both binary deployment (war file) and source code deployment(maven project). I have talked about how you can do source code deployment on tomcat in my previous blog.

Step 1: Sign up for an OpenShift Account

If you don’t already have an OpenShift account, head on over to the website and signup with promo code ews. It is completely free and Red Hat gives every user three free Gears on which to run your applications. At the time of this writing, the combined resources allocated for each user is 1.5 GB of memory and 3 GB of disk space.

Step 2: Install the client tools on your machine

Note: If you would rather watch a screencast of this step, check out the following videos where I demo how to install the client tools.

Windows

Linux Ubuntu

Linux Fedora

OSX

The OpenShift client tools are written in a very popular programming language called Ruby. With OSX 10.6 or later and most Linux distributions, ruby is installed by default so installing the client tools is a snap. Simply issue the following command on your terminal application:

sudo gem install rhc

Step 3 : Setting up OpenShift

The rhc client tool makes it very easy to setup your openshift instance with ssh keys, git and your applications namespace. The namespace is a unique name per user which becomes part of your application url. For example, if your namespace is cix and application name is bookshop then url of the application will be https://bookshop-cix.rhcloud.com/. The command is shown below.

rhc setup -l openshift_login

Step 4 : Creating Tomcat Application

After installing the client tools and setting up OpenShift account, next step is to create the bookshop application. This is a very simple Spring JPA application which has only one entity called Book. So, a user can do CRUD operations on book entity. To create a book entity, execute the command shown below.

rhc app create -a bookshop -t jbossews-1.0

This will create an application container for us, called a gear, and setup all of the required SELinux policies and cgroup configuration. OpenShift will also setup a private git repository for you and propagate your DNS out world wide.

Step 5 : Download the WAR file

Because we are doing binary deployment in this blog, we have to remove the src folder and pom.xml file from git repository and add the war file.

git rm -rf src/ pom.xml
git commit -am &amp;quot;removing default files&amp;quot;

Next download the war file from http://bookshop-demo.googlecode.com/files/ROOT.war and copy the war file in webapps directory. The name ROOT for war makes sure that the application is accessible at the root context i.e. http://bookshop-cix.rhcloud.com/.

git add .
git commit -am &amp;quot;committing bookshop war file&amp;quot;

Step 6 : Pushing the code to OpenShift

Finally push the code to OpenShift using git.

git push

The git push command upload the binary to the application gear and runs the action hooks specified in .openshift/action_hooks folder.

IBM DeveloperWorks Introducing Spring Roo, Part 4: Rapid application development in cloud with Spring Roo and Cloud Foundry

Take the rapid development of Roo a step further by creating applications to work in the cloud with Cloud Foundry, the first open platform as a service project created by VMWare. Learn more about the environment and then deploy an application into Cloud Foundry using the Roo shell. Read about it here http://www.ibm.com/developerworks/opensource/library/os-springroo4/index.html

Introducing Spring Roo, Part 3: Developing Spring Roo add-ons

Spring Roo is a RAD tool that lets you build applications (mainly web) quickly and easily. Under the hood, Spring Roo is based on OSGI add-on architecture, which makes it easy to extend Spring Roo by adding add-ons. Spring Roo provides commands to create add-ons that can be very easily made available to the Spring Roo user community. In this article, we first talk about Spring Roo architecture, talking about how Spring Roo leverages its own add-on architecture to provide different features, then we will create add-ons using the Roo shell and modify them to suit our needs. Please read the third article at IBM DeveloperWorks.

How to exclude a sub package from Spring component scanning?

Suppose you are using Spring component scanning and you don’t want to scan classes in the sub-package you should use <context:exclude-filter> subtag of <context:component-scan> tag as shown below

<context:component-scan base-package="com.test.ws">
	<context:exclude-filter type="aspectj" expression="com.test.ws.client.*" />
</context:component-scan>

Here we have defined a aspectj pointcut which will exclude all the classes in the sub-package.