Ehcache With CXF Web Service

Create a Service Interface

This service interface will defines which methods of restful service, to be invoked by the client

Here we implement the service interface created on the previous step

Note

Here we are using @Cacheable annotation like below

@Cacheable(value="student", key="#student.name")

value="student" is the cache name specified in cxf.xml

student.name is the property available on Student object, it will be the key

package com.student;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.springframework.cache.annotation.Cacheable;

@Consumes("application/json")
@Produces("application/json")
public class ChangeStudentDetailsImpl{
 
@Cacheable(value="student", key="#student.name")
 
@POST
  @Path
("/changeName")
 
public Student changeName(Student student) {
   
System.out.println("Inside changeName method");
    student.setName
("HELLO " + student.getName());
   
return student;
 
}
}

Create a cxf.xml

You need to add

<cache:annotation-driven />

for working on annotations and also need to specify which cacheManager we are using

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:cache="http://www.springframework.org/schema/cache" xmlns:cxf="http://cxf.apache.org/core"
	xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd  
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<cache:annotation-driven />
	<cxf:bus>
		<cxf:features>
			<cxf:logging />
		</cxf:features>
	</cxf:bus>
	<jaxrs:server address="/rest" id="base">
		<jaxrs:serviceBeans>
			<ref bean="StudentService" />
		</jaxrs:serviceBeans>
	</jaxrs:server>
	<bean class="com.student.ChangeStudentDetailsImpl" id="StudentService" />
	<!-- Ehcache library setup -->
	<bean class="org.springframework.cache.ehcache.EhCacheCacheManager"
		id="cacheManager" p:cache-manager-ref="ehcache" />
	<bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
		id="ehcache" p:config-location="WEB-INF/ehcache.xml" />
</beans>

Create a ehcache.xml

We are spacifying cache configurations on ehcache.xml

<ehcache>
	<diskStore path="java.io.tmpdir" />
	<cache name="student" maxElementsInMemory="100" eternal="false"
		timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
		maxElementsOnDisk="10000000" diskPersistent="false"
		diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
</ehcache>

Change web.xml

Change the web.xml file to find CXF servlet and cxf.xml

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/cxf.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>




Previous 1 2 3 Next








1 Responses to "Ehcache With CXF Web Service"
  1. Jeevan 2012-04-25 07:48:04.0

Your email address will not be published. Required fields are marked *