Happy Coding with Vinay Kumar in techartifact…
Latest tip and information on Java and Oracle Fusion Middleware/Weblogic
Add caching layer on your web application, with out modifying the code.
Ehcache has an out-of-box of solution for this ‘SimplePageCachingFilter’.
SimplePageCachingFilter is caching filter can be use full for html or any other response type
e.g JSON or xml. It uses singleton Ehcache manager to store contents. Cache keys are calculated
using the URI and query string. /exampleData?user=admin&role=admin.
There is another variant of ‘SimplePageCachingFilter’ based on headers ‘SimpleCachingHeadersPageCachingFilter’.
This filter take account headers for cache key. It take three account of headers
Configuration
cacheName
Cache used by filter for storing content
blockingTimeoutMillis
the time, in milliseconds, to wait for the filter chain to return with a response on a cache miss. This is useful to fail fast in the event of an infrastructure failure.
varyHeader
set to true to set Vary:Accept-Encoding in the response when doing Gzip. This header is needed to support HTTP proxies however it is off by default.
Web.xml
<filter> <filter-name>SimplePageCachingFilter</filter-name> <filter-class>net.sf.ehcache.constructs.web.filter. SimplePageFragmentCachingFilter </filter-class> <init-param> <param-name>cacheName</param-name> <param-value>simplePageCache</param-value> </init-param> </filter> <filter-mapping> <filter-name>SimplePageCachingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Ehcache configuration
<Ehcachexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../main/config/ehcache.xsd"> <diskStorepath="java.io.tmpdir"/> <defaultCache maxEntriesLocalHeap="10" eternal="false" timeToIdleSeconds="5" timeToLiveSeconds="10" overflowToDisk="true" /> <!-- Page and Page Fragment Caches --> <cachename="simplePageCache" maxEntriesLocalHeap="10" eternal="false" timeToIdleSeconds="10000" timeToLiveSeconds="10000" overflowToDisk="true"> </cache> </ehcache>
Pros
Cons
In recent times there is lot of growth in RESTFul services. I thought it would be nice to talk about it.
This example is using Apache CXF and Spring. There are some other frameworks e.g Jersey (Reference Sun implementation), RestEasy, the JBoss choice and Apache CXF.
Here is bacic web configuration adding Spring context and CXF tranport servlet.
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/services.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>/rest/*</url-pattern> </servlet-mapping>
There is Cities service using Spring annotations, this has been configured using Spring annotation
Second annottatiions is of CXF to define the mount point REST service
@Service("timeService") @Path("cities") public class CitiesListingService { @GET @Produces({"application/json", "application/xml"}) public RestFulCities getCities() { List<City> cities = new LinkedList<City>(); cities.add(new City("New Delhi", "011", "19M")); cities.add(new City("Mumbai", "022", "21M")); cities.add(new City("Chennai", "044", "10M")); RestFulCities restFulCities = new RestFulCities(); restFulCities.setRestFulCityList(getCities(cities)); return restFulCities; }
Here is Spring configuration for connection cxf and spring beans together.
<context:component-scan base-package="com.techartifact.example.spring"/> <import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <jaxrs:server id="restContainer" address="/"> <jaxrs:serviceBeans> <ref bean="timeService"/> </jaxrs:serviceBeans> <jaxrs:extensionMappings> <entry key="json" value="application/json"/> <entry key="xml" value="application/xml"/> </jaxrs:extensionMappings> <jaxrs:providers> <ref bean="jaxbXmlProvider"/> </jaxrs:providers> </jaxrs:server> <!-- Webservice message handlers --> <bean id="jaxbXmlProvider" class="org.apache.cxf.jaxrs.provider.JAXBElementProvider"> <property name="jaxbElementClassNames" ref="elements"/> </bean> <util:list id="elements"> <value>com.techartifact.example.spring.model.RestFulCity</value> <value>com.techartifact.example.spring.model.RestFulCities</value> </util:list>
Yes that’s it. Now build the code and run it.
Download full example code from here spring-RESTapplication
Go to project directory [spring-RESTapplication] in command shell and run following command using maven
mvn clean package mvn -Pcargo-run
http://localhost:8080/springrest/rest/cities
CXF can also produces output in json format, use following url to see out put in json
http://localhost:8080/springrest/rest/cities.json