Thursday, October 30, 2014

Upgrading spring from 3.0.6. to 4.1.1.RELEASE[Respective hibernate mapping files]

Here is the changes:

Below are the artifacts which are used in the 3.0.6 release


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.6.RELEASE</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>

<!-- Hibernate dependencies -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.5.6-Final</version>
</dependency>

Now I am updating this with spring version:
<org.springframework.version>4.1.1.RELEASE</org.springframework.version>

and the hibernate core version with  4.2.4.Final


After upgrading to this,When I started my application I got the below exception.

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.VerifyError: (class: org/springframework/context/annotation/ConfigurationClassEnhancer$BeanMethodInterceptor, method: intercept signature: (Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;Lorg/springframework/cglib/proxy/MethodProxy;)Ljava/lang/Object;) Incompatible argument to function
at org.springframework.context.annotation.ConfigurationClassEnhancer.<clinit>(ConfigurationClassEnhancer.java:67)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:377)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:259)


Issue because of JReble.. , I run my server with out JReble got different issue.

Issue 2#
java.lang.NoClassDefFoundError: org/hibernate/util/DTDEntityResolve


Solution :

When I got the above exception I removed the below dependency.

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>

Hibernate 3.5 and onward contains Hibernate Annotations.

Issue 3#

NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given,

Solution:
Added the below property in hibernate.cfg.xml file

<property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property>

Now I am ready :-)

Monday, October 27, 2014

ConfigurationHolder in grails 2.4.2

ConfigurationHolder class has been removed from grails 2.4.2 

It has been replaced with Holder class

Sample code
def baseUrl = ConfigurationHolder.config.icann.base.url 

is replaced with 
def baseUrl = Holders.config.icann.base.url

Refer :http://grails.org/doc/2.4.2/guide/upgradingFrom23.html  for more information

Wednesday, October 22, 2014

Maven commands

To clean and create target folder:

mvn clean install

To set the eclipse class path:

mvn eclipse:eclipse

To clean the eclipse class path

mvn eclipse:clean

Monday, October 20, 2014

Reading a custom properties or default message properties in spring service/controller..etc


Custom properties file
Need to configure the property files in this format.

Make sure you keep the properties in your class path.

<?xml version="1.0" encoding="UTF-8" ?>

 <beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
  <util:properties id="npAdminProperties" location="classpath:npadmin.properties" />
 </beans>

Example:

npadmin.properties file:

environment.code=DEV


To read the values from the properties files in service/controller you can inject like

private @Value("#{npAdminProperties['environment.code']}") String environment;


To read a Message properties in controller or service :

One way of doing:

@Autowired
private MessageSource messageSource;

messageSource.getMessage("domaindetector.builder.email.subject",null, Locale.ENGLISH)

Other way:
@Value("${domaindetector.builder.email.subject}")

private String domainDetecorEmailSubject;

Spring controller- Return to a specific view

Return type of a controller method should be ModelAndView

ModelAndView retryBuildersStart(Model model,
final HttpServletRequest request,..etc) {

new ModelAndView("viewName", "formName", form);
}

Wednesday, October 15, 2014

Wednesday, October 8, 2014

Springs (Profiling a method) using AOP.(logging the method execution time)

Dependencies:

pom.xml file add the below jar's:
          <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>

Creating the pro-filer :

package com.cscinfo.npadmin.aspect;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

/**
 * @author kvennamp
 *
 */
@Aspect
public class BusinessProfiler {

private static final Logger logger = Logger.getLogger(BusinessProfiler.class);

@Pointcut("execution(* com.cscinfo.npadmin.service.impl.*.*(..))")
    public void serviceMethods() { }

    @Around("serviceMethods()")
    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
            long start = System.currentTimeMillis();
            logger.info("Going to call the method "+pjp.getSignature().getName());
            Object output = pjp.proceed();
            logger.info(pjp.getSignature().getName()+"Method execution completed.");
            long elapsedTime = System.currentTimeMillis() - start;
            logger.info(pjp.getSignature().getName()+" Method execution time: " + elapsedTime + " milliseconds.");
            return output;
    }
}


In application context.xml file write

<aop:aspectj-autoproxy />
<bean id="businessProfiler" class="com.cscinfo.npadmin.aspect.BusinessProfiler"></bean>

Here all the services classes and method are inside the
com.cscinfo.npadmin.service.impl package.

The Above pro-filer will log the method name followed by how much time it took to process.

Thursday, October 2, 2014

Set external jar in class path when using the POM file.

This is how you can add the system path jar file in pom.xml here scope must be system level

<dependency>
<groupId>com.nameprotect.tm.pronto.webservice</groupId>
<artifactId>TmProntoWebService</artifactId>
<scope>system</scope>
<systemPath>C:/work/Projects/old-np-trunk/TmProntoWebService/dist/TmProntoWebService.jar</systemPath>
<version>1.2</version>

</dependency>

Get the performance time of a method in java.

This will return the total time in milliseconds

long startTime = System.currentTimeMillis();
for(String id :ids) {
client.detail("US", id);
}
long endTime   = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println(totalTime+"<--- total time");