Advertisement
Guest User

Untitled

a guest
Mar 24th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.24 KB | None | 0 0
  1. Hi, I would like to persist portlet's model. I use Spring MVC and data of portlet's model is result of another portlet. Data can take more than 50MB and I want to make portle's model as a non-singleton. If I don't want to create a database for portlet's model, I can persist data of portlet's model in a cache or session, but with more than 50MB session would be overloaded. I want to use ehcache, but when i use Cacheable annotation, empty data file is generated. Do you know another way to persist portlet's model or how can i use ehcache?
  2.  
  3. [b]SocialGraphUI-portlet.xml[/b]
  4.  
  5. [code]<beans xmlns="http://www.springframework.org/schema/beans"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  8. xmlns:mvc="http://www.springframework.org/schema/mvc"
  9. xmlns:p="http://www.springframework.org/schema/p"
  10. xmlns:cache="http://www.springframework.org/schema/cache"
  11. xsi:schemaLocation="
  12. http://www.springframework.org/schema/beans
  13. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  14. http://www.springframework.org/schema/mvc
  15. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  16. http://www.springframework.org/schema/context
  17. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  18. http://www.springframework.org/schema/cache
  19. http://www.springframework.org/schema/cache/spring-cache.xsd">
  20.  
  21. <context:component-scan base-package="socialgraphui" />
  22.  
  23. <cache:annotation-driven />
  24.  
  25. <cache:annotation-driven cache-manager="cacheManager" mode="proxy" proxy-target-class="true" />
  26. <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="/WEB-INF/ehcache.xml" p:shared="true" />
  27. <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehcache" />
  28.  
  29. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  30. <property name="prefix" value="/WEB-INF/jsp/" />
  31. <property name="suffix" value=".jsp" />
  32. <property name="viewClass"
  33. value="org.springframework.web.servlet.view.JstlView" />
  34. </bean>
  35.  
  36. <mvc:annotation-driven>
  37. <mvc:message-converters>
  38. <bean class="org.springframework.http.converter.json.GsonHttpMessageConverter" />
  39. </mvc:message-converters>
  40. </mvc:annotation-driven>
  41.  
  42. <bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
  43. <bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
  44.  
  45. <!-- Spring MVC Message Source -->
  46. <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  47. <property name="useCodeAsDefaultMessage" value="true"/>
  48. <property name="basenames">
  49. <list>
  50. <value>content.socialGraph</value>
  51. </list>
  52. </property>
  53. </bean>
  54.  
  55. </beans>[/code]
  56.  
  57. [b]ehcache.xml[/b]
  58.  
  59. [code]<?xml version="1.0" encoding="UTF-8"?>
  60. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
  61. <diskStore path="user.dir"/>
  62. <defaultCache eternal="true" overflowToDisk="true" diskPersistent="true" />
  63. <cache name="socialGraphCache" eternal="true" overflowToDisk="true" diskPersistent="true" />
  64. </ehcache>[/code]
  65.  
  66. [b]SocialGraphViewController.java[/b]
  67.  
  68. [code] package socialgraphui.controller;
  69.  
  70. import com.google.gson.Gson;
  71. import com.google.gson.GsonBuilder;
  72. import com.liferay.portal.kernel.util.ParamUtil;
  73. import socialgraphui.model.Edge;
  74. import socialgraphui.model.Message;
  75. import socialgraphui.model.Node;
  76. import socialgraphui.model.SocialGraph;
  77. import socialgraphui.service.SocialGraphService;
  78. import java.io.IOException;
  79. import java.io.PrintWriter;
  80. import java.util.ArrayList;
  81. import java.util.Arrays;
  82. import java.util.LinkedList;
  83. import java.util.List;
  84. import java.util.Locale;
  85. import org.apache.log4j.Logger;
  86. import javax.portlet.ActionRequest;
  87. import javax.portlet.ActionResponse;
  88. import javax.portlet.PortletSession;
  89. import javax.portlet.RenderRequest;
  90. import javax.portlet.RenderResponse;
  91. import javax.portlet.ResourceRequest;
  92. import javax.portlet.ResourceResponse;
  93. import org.springframework.beans.factory.annotation.Autowired;
  94. import org.springframework.beans.factory.annotation.Qualifier;
  95. import org.springframework.stereotype.Controller;
  96. import org.springframework.ui.ModelMap;
  97. import org.springframework.web.bind.WebDataBinder;
  98. import org.springframework.web.bind.annotation.InitBinder;
  99. import org.springframework.web.bind.annotation.ModelAttribute;
  100. import org.springframework.web.bind.annotation.RequestMapping;
  101. import org.springframework.web.portlet.bind.annotation.ActionMapping;
  102. import org.springframework.web.portlet.bind.annotation.RenderMapping;
  103. import org.springframework.web.portlet.bind.annotation.ResourceMapping;
  104.  
  105. /**
  106. *
  107. * Controller for VIEW mode of portlet
  108. */
  109. @Controller("socialGraphViewController")
  110. @RequestMapping(value = "VIEW")
  111. public class SocialGraphViewController{
  112.  
  113. Gson gson = new GsonBuilder().setPrettyPrinting().create();
  114. private static final Logger logger = Logger.getLogger(SocialGraphViewController.class);
  115.  
  116. // -- auto-wiring of service dependency
  117. @Autowired
  118. @Qualifier("SGService")
  119. private SocialGraphService socialGraphService;
  120.  
  121. public void setSocialGraphService(SocialGraphService service){
  122. this.socialGraphService = service;
  123. }
  124.  
  125. @ModelAttribute(value="socialgraph")
  126. public SocialGraph getSocialGraph(){
  127. return socialGraphService.getSocialGraph();
  128. }
  129.  
  130. @InitBinder
  131. public void initBinder(WebDataBinder binder) {
  132. binder.registerCustomEditor(PersonDefinitionTypeList.class, new PersonDefinitionTypeListEditor());
  133. }
  134.  
  135. @ActionMapping(SocialGraphPortletConstants.SUBMIT_FILTER)
  136. public void handleActionRequest(ActionRequest request, ActionResponse response, PortletSession session)throws Exception {
  137.  
  138. logger.info("handleActionRequest in was executed");
  139.  
  140. ...
  141. }
  142.  
  143. @RenderMapping
  144. public String handleRenderRequest(RenderRequest request, RenderResponse response, ModelMap model, Locale locale, PortletSession session) {
  145.  
  146. logger.info("handleRenderRequest was executed");
  147.  
  148. ...
  149.  
  150. return SocialGraphPortletConstants.VIEW;
  151. }
  152.  
  153. }[/code]
  154.  
  155. In this example I want to cache result of service constructor
  156.  
  157. [b]SocialGraphServiceImpl.java[/b]
  158.  
  159. [code]import com.google.common.base.Function;
  160. import com.google.common.collect.ArrayListMultimap;
  161. import com.google.common.collect.Iterables;
  162. import com.google.common.collect.Lists;
  163. import com.google.common.collect.Multimap;
  164. import com.google.common.collect.Multimaps;
  165. import com.google.common.collect.Sets;
  166. import socialgraphui.controller.SocialGraphViewController;
  167. import socialgraphui.model.Discussion;
  168. import socialgraphui.model.Edge;
  169. import socialgraphui.model.Email;
  170. import socialgraphui.model.Message;
  171. import socialgraphui.model.Node;
  172. import socialgraphui.model.PhoneCall;
  173. import socialgraphui.model.SocialGraph;
  174. import java.text.DateFormat;
  175. import java.text.ParseException;
  176. import java.text.SimpleDateFormat;
  177. import java.util.ArrayList;
  178. import java.util.Arrays;
  179. import java.util.Calendar;
  180. import java.util.Collection;
  181. import java.util.HashSet;
  182. import java.util.Iterator;
  183. import java.util.List;
  184. import java.util.Set;
  185. import java.util.logging.Level;
  186. import java.util.logging.Logger;
  187. import org.springframework.cache.annotation.CachePut;
  188. import org.springframework.cache.annotation.Cacheable;
  189. import org.springframework.stereotype.Service;
  190.  
  191. /**
  192. *
  193. */
  194. @Service(value="SGService")
  195. public class SocialGraphServiceImpl implements SocialGraphService{
  196.  
  197. private SocialGraph socialgraph = new SocialGraph();
  198. private CreateObjects crObjects = new CreateObjects();
  199.  
  200. public SocialGraphServiceImpl(){
  201. this.fillGraph();
  202. }
  203.  
  204. @Override
  205. @Cacheable(value = "socialGraphCache")
  206. public SocialGraph fillGraph(){
  207. this.socialgraph = crObjects.createObjects();
  208. return this.socialgraph;
  209. }
  210. }[/code]
  211.  
  212. Example of data which I want to cache.
  213.  
  214. [b]SocialGraph.java[/b]
  215.  
  216. [code]package socialgraphui.model;
  217.  
  218. import java.io.Serializable;
  219. import java.util.List;
  220.  
  221. public class SocialGraph implements Serializable{
  222.  
  223. private static final long serialVersionUID = -6977846672304367384L;
  224.  
  225. private List<Node> nodes;
  226. private List<Edge> edges;
  227.  
  228. public List<Node> getNodes() {
  229. return nodes;
  230. }
  231.  
  232. public void setNodes(List<Node> nodes) {
  233. this.nodes = nodes;
  234. }
  235.  
  236. public List<Edge> getEdges() {
  237. return edges;
  238. }
  239.  
  240. public void setEdges(List<Edge> edges) {
  241. this.edges = edges;
  242. }
  243.  
  244. }[/code]
  245.  
  246. When I deploy the portlet, I don't get any errors, but generated cache file is empty.
  247.  
  248. [url=http://stackoverflow.com/questions/29079527/ehcache-generates-empty-data-file]SO[/url]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement