Friday 24 May 2013

Delete Hibernate Criteria/Query

0 comments
Criteria
 Student student = (Student ) session.createCriteria(Student.class)
                    .add(Restrictions.eq("classId", classId)).uniqueResult();
  session.delete(student);
HQL query:
String hql = "delete from Student where classId= :classId";
session.createQuery(hql).setString("classId", classId).executeUpdate();


Wednesday 15 May 2013

Rest Template WebService Call & Map Json Response In To Java Entity

0 comments
Step 1. Make a RestTemplate instance
private RestTemplate restTemplate = new RestTemplate();

Step 2. Call Post method of RestTemplate to hit the service

String jsonString = "{"
      + "\"dataBeginIndex\":\"1\"" + ","
         + "\"userId\":\"jFB425S0Rv0=\"" + ","
         + "\"countryId\":\"96\"" +","
         + "\"dataEndIndex\":\"2\""
         + "}";
      
     
  HttpHeaders headers = new HttpHeaders();
         headers.setContentType( MediaType.APPLICATION_JSON );
         
         HttpEntity customReq = new HttpEntity( jsonString, headers );
         String returnedString = restTemplate.postForObject( "http://URL", customReq, String.class );
         log.info("Response Json: "+returnedString);
         
         CustomUtil obj = new CustomUtil();
         CustomBean customBean = obj.jsonConvertor(returnedString);
Step 3. jsonConvertor(String json)
public CustomBean jsonConvertor(String jsonString)
 {
  JsonFactory factory = new JsonFactory();
  ObjectMapper mapper = new ObjectMapper(factory);
  try 
  {
   CustomBean customBean = mapper.readValue(jsonString, CustomBean.class);
   
   return customBean;
  } catch (JsonParseException e) {
   e.printStackTrace();
  } catch (JsonMappingException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
   return new CustomBean();
 }
Step 4. Add this bean in applicationcontext

  
      
       
           
            
           
       
      
    

Thursday 9 May 2013

Spring MVC Internationalization

0 comments
In Spring MVC application, comes with few “LocaleResolver” to support the internationalization or multiple languages features.


Properties file

welcome_en.properties

index.springmvc = hello eng

welcome_jp.properties

index.springmvc = hello Japan




Spring Configuration
To make Spring MVC application supports the internationalization, register two beans :

1. SessionLocaleResolver
Register a “SessionLocaleResolver” bean, named it exactly the same characters “localeResolver“. It resolves the locales by getting the predefined attribute from user’s session.

If you do not register any “localeResolver”, the default AcceptHeaderLocaleResolver will be used, which resolves the locale by checking the accept-language header in the HTTP request.

2. LocaleChangeInterceptor
Register a “LocaleChangeInterceptor” interceptor and reference it to any handler mapping that need to supports the multiple languages. The “paramName” is the parameter value that’s used to set the locale.

In this case,

index.htm?language=en – Get the message from English properties file.
index.htm?language=jp – Get the message from Japan properties file.

spring-servlet.xml



   
  
  
 
 
  
 
 
 
  
     
   
     
  
 

 
  
   
    /WEB-INF/resourcebundle/messages
   
  
  
  
 
 
   
index.jsp


Wednesday 1 May 2013

Schedule tasks in Spring 3 Using @Scheduled

0 comments

Explaining @Scheduled annotation

This annotation is used for task scheduling. The trigger information needs to be provided along with this annotation. You can use the properties fixedDelay/fixedRate/cron to provide the triggering information.
  1. fixedRate makes Spring run the task on periodic intervals even if the last invocation may be still running.
  2. fixedDelay specifically controls the next execution time when the last execution finishes.
  3. cron is a feature originating from Unix cron utility and has various options based on your requirements.
Example usage can be as below:

@Scheduled(fixedDelay =30000)
public void demoServiceMethod () {... }

@Scheduled(fixedRate=30000)
public void demoServiceMethod () {... }

@Scheduled(cron="0 0 * * * *")
public void demoServiceMethod () {... }

To use @Scheduled in your spring application, you must first define below xml namespace and schema location definition in your application-config.xml file.
 
xmlns:task="http://www.springframework.org/schema/task"

http://www.springframework.org/schema/task

http://www.springframework.org/schema/task/spring-task-3.0.xsd

Above additions are necessary because we will be using annotation based configurations. Now add below definition to enable annotations.

 



Next step is to create a class and a method inside the class like below:
public class DemoService
{
 @Scheduled(cron="*/5 * * * * ?")
 public void demoServiceMethod()
 {
  System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date());
 }
}
Using @Scheduled annotation would in turn make Spring container understand that the method underneath this annotation would run as a job. Remember that the methods annotated with @Scheduled should not have parameters passed to them. They should not return any values too. If you want the external objects to be used within your @Scheduled methods, you should inject them into the DemoService class using autowiring rather than passing them as parameters to the @Scheduled methods.

And application configuration will look like this:
 
< ?xml  version="1.0" encoding="UTF-8"?>


    
    

Related Posts Plugin for WordPress, Blogger...