├── .classpath ├── .project ├── README ├── pom.xml └── src └── main ├── java └── fr │ └── dr │ └── sandbox │ ├── controller │ ├── Customer.java │ ├── CustomerComponent.java │ └── MainController.java │ └── listener │ └── CacheListener.java ├── resources ├── log4j.xml └── spring.xml └── webapp ├── META-INF └── MANIFEST.MF ├── WEB-INF ├── crash │ └── commands │ │ └── spring_cache.groovy ├── ehcache.xml ├── jsp │ ├── add.jsp │ ├── failed.jsp │ ├── index.jsp │ └── success.jsp ├── sandbox.xml └── web.xml └── resources ├── css └── design.css └── js └── script.js /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | sandbox 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.maven.ide.eclipse.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.maven.ide.eclipse.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crashub/spring-ehcache-demo/49409fc8f021a83f24988303c4918489a83c79b2/README -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | fr.dr 5 | sandbox 6 | war 7 | 1.0.0 8 | 9 | 10 | 11 | org.springframework 12 | spring-beans 13 | ${org.springframework.version} 14 | 15 | 16 | org.springframework 17 | spring-web 18 | ${org.springframework.version} 19 | 20 | 21 | org.springframework 22 | spring-webmvc 23 | ${org.springframework.version} 24 | 25 | 26 | org.springframework 27 | spring-context 28 | ${org.springframework.version} 29 | 30 | 31 | org.springframework 32 | spring-core 33 | ${org.springframework.version} 34 | 35 | 36 | junit 37 | junit 38 | 4.5 39 | runtime 40 | 41 | 42 | log4j 43 | log4j 44 | 1.2.14 45 | 46 | 47 | 48 | com.googlecode.ehcache-spring-annotations 49 | ehcache-spring-annotations 50 | 1.2.0-M1 51 | 52 | 53 | 54 | cglib 55 | cglib 56 | 2.2.2 57 | 58 | 59 | net.sf.ehcache 60 | ehcache 61 | 1.6.1 62 | 63 | 64 | javax.servlet 65 | javax.servlet-api 66 | 3.0.1 67 | jar 68 | compile 69 | 70 | 71 | 72 | org.crsh 73 | crsh.shell.embed.spring 74 | 1.1.0 75 | jar 76 | compile 77 | 78 | 79 | org.crsh 80 | crsh.shell.core 81 | 1.1.0 82 | jar 83 | compile 84 | 85 | 86 | org.crsh 87 | crsh.shell.packaging 88 | 1.1.0 89 | war 90 | spring 91 | compile 92 | 93 | 94 | 95 | 96 | 97 | org.apache.maven.plugins 98 | maven-compiler-plugin 99 | 2.3.2 100 | 101 | 1.6 102 | 1.6 103 | 104 | 105 | 106 | org.apache.maven.plugins 107 | maven-war-plugin 108 | 2.1.1 109 | 110 | demo 111 | 112 | 113 | 114 | 115 | 116 | 3.1.1.RELEASE 117 | UTF-8 118 | 119 | -------------------------------------------------------------------------------- /src/main/java/fr/dr/sandbox/controller/Customer.java: -------------------------------------------------------------------------------- 1 | package fr.dr.sandbox.controller; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Customer with id,name and address. 7 | * @author drieu 8 | * 9 | */ 10 | public class Customer implements Serializable { 11 | 12 | private static final long serialVersionUID = 1L; 13 | 14 | public String address; 15 | public String name; 16 | public String id; 17 | 18 | public Customer() { 19 | } 20 | 21 | public Customer(String id, String name, String address) { 22 | this.id = id; 23 | this.name = name; 24 | this.address = address; 25 | } 26 | 27 | public String getAddress() { 28 | return address; 29 | } 30 | 31 | public void setAddress(String address) { 32 | this.address = address; 33 | } 34 | 35 | public String getName() { 36 | return name; 37 | } 38 | 39 | public void setName(String name) { 40 | this.name = name; 41 | } 42 | 43 | public String getId() { 44 | return id; 45 | } 46 | 47 | public void setId(String id) { 48 | this.id = id; 49 | } 50 | 51 | /** 52 | * Print a message. 53 | */ 54 | public static void show() { 55 | System.out.println("Hello Customer!"); 56 | 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/fr/dr/sandbox/controller/CustomerComponent.java: -------------------------------------------------------------------------------- 1 | package fr.dr.sandbox.controller; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import net.sf.ehcache.Cache; 7 | import net.sf.ehcache.CacheManager; 8 | import net.sf.ehcache.Element; 9 | 10 | import org.apache.log4j.Logger; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.beans.factory.annotation.Qualifier; 13 | import org.springframework.stereotype.Component; 14 | 15 | import com.googlecode.ehcache.annotations.TriggersRemove; 16 | 17 | @Component("CustomerComponent") 18 | public class CustomerComponent { 19 | 20 | 21 | final Logger logger = Logger.getLogger(getClass().getName()); 22 | 23 | @Autowired 24 | public CacheManager cacheManager; 25 | 26 | @Qualifier(value="customer") 27 | public void setMyCache(final CacheManager cache) { 28 | cacheManager = cache; 29 | } 30 | 31 | /** 32 | * Save a Customer in cache. 33 | * @param customer Customer. 34 | * @return 35 | */ 36 | public boolean saveCustomer(Customer customer){ 37 | Cache cache=cacheManager.getCache("customer"); 38 | cache.put(new Element(customer.getId(),customer)); 39 | return true; 40 | } 41 | 42 | /** 43 | * Remove all data in cache. 44 | * @return boolean 45 | */ 46 | @TriggersRemove(cacheName = "customer", removeAll = true) 47 | public void clearCache(){ 48 | } 49 | 50 | /** 51 | * Retrieve a list of Customer in cache. 52 | * @return List. 53 | */ 54 | public List getCache() { 55 | List lst = new ArrayList(); 56 | Cache cache=cacheManager.getCache("customer"); 57 | 58 | List l = cache.getKeys(); 59 | Customer customer = null; 60 | for(String customerId : l) { 61 | Element el = (Element)cache.get(customerId); 62 | if (null != el) { 63 | customer = (Customer)el.getValue(); 64 | if (null != customer) { 65 | logger.info("Customer id :" + customer.getId()); 66 | logger.info("Customer name :" + customer.getName()); 67 | lst.add(customer); 68 | } 69 | } 70 | } 71 | return lst; 72 | } 73 | 74 | /** 75 | * Demo Method to Print a message. 76 | */ 77 | public static void show() { 78 | System.out.println("You call a static method of CustomerComponent !"); 79 | } 80 | } 81 | 82 | -------------------------------------------------------------------------------- /src/main/java/fr/dr/sandbox/controller/MainController.java: -------------------------------------------------------------------------------- 1 | package fr.dr.sandbox.controller; 2 | 3 | import org.apache.log4j.Logger; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.beans.factory.annotation.Qualifier; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.stereotype.Controller; 8 | import org.springframework.ui.Model; 9 | import org.springframework.ui.ModelMap; 10 | import org.springframework.web.bind.annotation.ModelAttribute; 11 | import org.springframework.web.bind.annotation.RequestMapping; 12 | import org.springframework.web.bind.annotation.RequestMethod; 13 | import org.springframework.web.bind.annotation.RequestParam; 14 | import org.springframework.web.bind.annotation.ResponseBody; 15 | import org.springframework.web.bind.annotation.ResponseStatus; 16 | 17 | /** 18 | * Controller. 19 | * @author drieu 20 | * 21 | */ 22 | @Controller 23 | public class MainController { 24 | 25 | final Logger logger = Logger.getLogger(getClass().getName()); 26 | 27 | @Autowired 28 | @Qualifier("CustomerComponent") 29 | public CustomerComponent customerComponent; 30 | 31 | /** 32 | * Main page of this application. 33 | * 34 | * @param name Use when we say Hello. 35 | * @param model ModelMap. 36 | * @return view name. 37 | */ 38 | @RequestMapping(value = "/") 39 | public String toIndex( 40 | @RequestParam(value = "name", required = false) String name, 41 | ModelMap model) { 42 | model.addAttribute("name", name); 43 | model.addAttribute("customers", customerComponent.getCache()); 44 | return "index"; 45 | } 46 | 47 | /** 48 | * Clear all data in cache. 49 | * @return String text message OK or KO. 50 | */ 51 | @RequestMapping(value = "/clearCache", method = { RequestMethod.GET }) 52 | @ResponseStatus(HttpStatus.OK) 53 | public @ResponseBody 54 | String clearCache() { 55 | customerComponent.clearCache(); 56 | return "Cache successfully Cleaned"; 57 | 58 | } 59 | 60 | /** 61 | * Access to the page to add a Customer in cache. 62 | * @param model Model 63 | * @return "add" page. 64 | */ 65 | @RequestMapping(value = "/add", method = RequestMethod.GET) 66 | public String questionnaire(final Model model) { 67 | final Customer customer = new Customer(); 68 | model.addAttribute("premierFormulaire", customer); 69 | return "add"; 70 | } 71 | 72 | /** 73 | * Save a Customer. 74 | * @param customer Customer 75 | * @param model Model 76 | * @return "success" or "failed" page. 77 | */ 78 | @RequestMapping(value = "/save", method = RequestMethod.POST) 79 | @ResponseStatus(HttpStatus.OK) 80 | public String validation( 81 | @ModelAttribute("premierFormulaire") final Customer customer, 82 | final Model model) { 83 | model.addAttribute("retour", 84 | customer.getId() + " " + customer.getName()); 85 | boolean result = customerComponent.saveCustomer(customer); 86 | if (result) { 87 | return "success"; 88 | } else { 89 | return "failed"; 90 | } 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/fr/dr/sandbox/listener/CacheListener.java: -------------------------------------------------------------------------------- 1 | package fr.dr.sandbox.listener; 2 | 3 | import javax.servlet.ServletContext; 4 | import javax.servlet.ServletContextEvent; 5 | import javax.servlet.ServletContextListener; 6 | 7 | import net.sf.ehcache.Cache; 8 | import net.sf.ehcache.CacheManager; 9 | import net.sf.ehcache.Element; 10 | 11 | import org.apache.log4j.Logger; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.context.ApplicationContext; 14 | import org.springframework.web.context.support.WebApplicationContextUtils; 15 | 16 | import fr.dr.sandbox.controller.Customer; 17 | 18 | /** 19 | * Fill the cache at startup. 20 | * It put a test value in cache (customerId:1, Name:Smith, Address:Smith Address). 21 | * @author drieu 22 | * 23 | */ 24 | public class CacheListener implements ServletContextListener { 25 | 26 | final Logger logger = Logger.getLogger(getClass().getName()); 27 | 28 | @Autowired 29 | public CacheManager cacheManager; 30 | 31 | @Override 32 | public void contextInitialized(ServletContextEvent sce) { 33 | logger.info("========> contextInitialized() : BEGIN. "); 34 | 35 | ServletContext servletContext = sce.getServletContext(); 36 | if (null == servletContext) { 37 | logger.warn("servlet context is null !"); 38 | } 39 | ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext); 40 | if (null != ctx) { 41 | 42 | CacheManager cacheManager = (CacheManager) WebApplicationContextUtils.getWebApplicationContext(servletContext).getBean("cacheManager"); 43 | Cache cache = cacheManager.getCache("customer"); 44 | 45 | Customer c = new Customer(); 46 | c.setId("1"); 47 | c.setName("Smith"); 48 | c.setAddress("Smith Address"); 49 | cache.put(new Element(c.getId(),c)); 50 | 51 | Customer customerInCache = (Customer)cache.get("1").getObjectValue(); 52 | logger.info("========> contextInitialized() : Customer " + customerInCache.getName() + " was added in cache."); 53 | 54 | } else { 55 | logger.warn("ctx is null !"); 56 | } 57 | logger.info("========> contextInitialized() : END"); 58 | } 59 | 60 | @Override 61 | public void contextDestroyed(ServletContextEvent sce) { 62 | } 63 | 64 | 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/main/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 107 | 108 | -------------------------------------------------------------------------------- /src/main/resources/spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 13 | 14 | 15 | 17 | 18 | 19 | 20 | 21 | 22 | 5000 23 | 24 | 1 25 | 26 | 2000 27 | 28 | simple 29 | admin 30 | admin 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /src/main/webapp/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | 3 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/crash/commands/spring_cache.groovy: -------------------------------------------------------------------------------- 1 | package crash.commands.base 2 | 3 | import org.crsh.command.CRaSHCommand 4 | import org.crsh.cmdline.annotations.Usage 5 | import org.crsh.cmdline.annotations.Command 6 | import org.crsh.cmdline.annotations.Argument 7 | 8 | import org.crsh.cmdline.annotations.Required 9 | import org.crsh.shell.ui.UIBuilder 10 | 11 | import fr.dr.sandbox.controller.Customer; 12 | import fr.dr.sandbox.controller.CustomerComponent; 13 | import java.lang.reflect.Method; 14 | import java.util.List; 15 | 16 | /** 17 | * Spring commands to list Customer in cache for the demo application. 18 | * @author drieu 19 | * 20 | */ 21 | @Usage("Spring cache commands") 22 | @Command 23 | class spring_cache extends CRaSHCommand { 24 | 25 | @Usage("Show values that are loaded in cache") 26 | @Command 27 | public void showCache() { 28 | def bean = context.attributes.beans["customerComponent"]; 29 | if (null != bean) { 30 | out.println("Cache contents :"); 31 | List lst = bean.getCache(); 32 | if (null != lst) { 33 | for(Customer c : lst) { 34 | out.println("Name:" + c.getName()); 35 | out.println("Id:" + c.getId()); 36 | c.show(); 37 | } 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/ehcache.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/jsp/add.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ page isELIgnored="false"%> 4 | <%@ page import="fr.dr.sandbox.controller.Customer"%> 5 | <%@ page import="java.util.List"%> 6 | <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | Add a customer 15 | 16 | 17 |
18 |
19 |
20 |

Add a customer

21 |
22 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 |
Id:
Name:
Address:
43 |
44 |
45 |
46 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/jsp/failed.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | FAILED 8 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/jsp/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ page isELIgnored="false"%> 4 | <%@ page import="fr.dr.sandbox.controller.Customer"%> 5 | <%@ page import="java.util.List"%> 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Crash Demo Application 14 | 15 | 16 |
17 |
18 |

Introduction

19 |
20 |
21 |

Hello ${name} This is the main page of this demo application. 22 | You can show datas available in cache ,add Customer and 23 | remove all datas. The goal of this demo application is to 24 | test Crash.

25 |
26 |
27 |
28 |
29 |

Manage Customer in cache

30 |
31 |
32 | 33 | 34 | 35 | 37 | 38 |
Add a customerClear 36 | customer cache
39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | <% 50 | List lst = (List) request 51 | .getAttribute("customers"); 52 | if (null != lst) { 53 | for (Customer c : lst) { 54 | out.println(""); 55 | 56 | out.println(""); 59 | out.println(""); 62 | out.println(""); 65 | 66 | out.println(""); 67 | } 68 | } 69 | %> 70 |
Customers in cache
IdNameAddress
"); 57 | out.println(c.getId()); 58 | out.println(""); 60 | out.println(c.getName()); 61 | out.println(""); 63 | out.println(c.getAddress()); 64 | out.println("
71 |
72 |
73 |
74 |
75 |

How to test Crash with this application ?

76 |
77 |
78 |

Here is the step you have to do for connecting Crash to this demo application.

79 |
    80 |
  1. Connect crash on this JVM
  2. 81 |

    82 | %telnet 5000 localhost
    83 | %ssh -p 2000 -l admin localhost (mdp admin)
    84 | 85 | $JDK_HOME/bin/jps and get the id.
    86 | %cd $CRASH_HOME/bin
    87 | %./crash.sh ID

    88 |
  3. Execute the following command :

    % spring_cache showcache

  4. 89 |
  5. Add a Customer with this web interface : Add a customer
  6. 90 |
  7. Execute the spring_cache command again :

    spring_cache showcache

  8. 91 |
92 |
93 |
94 | 95 | 96 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/jsp/success.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | SUCESS 8 | 9 | Customer save successfully ! 10 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/sandbox.xml: -------------------------------------------------------------------------------- 1 | 2 | 15 | 16 | 17 | 19 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | Demo application 6 | 7 | contextConfigLocation 8 | 9 | classpath:/spring.xml 10 | 11 | 12 | 13 | 14 | 15 | org.springframework.web.context.ContextLoaderListener 16 | 17 | 18 | 19 | 20 | 21 | fr.dr.sandbox.listener.CacheListener 22 | 23 | 24 | 25 | 26 | 27 | sandbox 28 | sandbox 29 | org.springframework.web.servlet.DispatcherServlet 30 | 31 | contextConfigLocation 32 | /WEB-INF/sandbox.xml 33 | 34 | 1 35 | 36 | 37 | 38 | sandbox 39 | / 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /src/main/webapp/resources/css/design.css: -------------------------------------------------------------------------------- 1 | /* Makeshift CSS Reset */ 2 | { 3 | margin: 0; 4 | padding: 0; 5 | } 6 | /* Tell the browser to render HTML 5 elements as block */ 7 | header, footer, aside, nav, article { 8 | display: block; 9 | } 10 | body { 11 | margin: 0 auto; 12 | width: 940px; 13 | font: 13px/22px Helvetica, Arial, sans-serif; 14 | background: #f0f0f0; 15 | } 16 | h2 { 17 | font-size: 28px; 18 | line-height: 44px; 19 | padding: 10px 0; 20 | } 21 | h3 { 22 | font-size: 18px; 23 | line-height: 22px; 24 | padding: 11px 0; 25 | } 26 | p { 27 | padding-bottom: 10px; 28 | } 29 | 30 | #txt { 31 | padding: 0; 32 | color: graytext; 33 | font-style:italic; 34 | } 35 | 36 | table 37 | { 38 | border-collapse:collapse; 39 | width:100%; 40 | max-width:700px; 41 | min-width:400px; 42 | text-align:center; 43 | } 44 | 45 | caption 46 | { 47 | caption-side:bottom; 48 | font-weight:bold; 49 | font-style:italic; 50 | margin:4px; 51 | } 52 | 53 | table,th, td 54 | { 55 | border: 1px solid gray; 56 | } 57 | 58 | th, td 59 | { 60 | height: 24px; 61 | padding:4px; 62 | vertical-align:middle; 63 | } 64 | 65 | th 66 | { 67 | background-image:url(table-shaded.png); 68 | } 69 | 70 | .rowtitle 71 | { 72 | background: #9CF; 73 | font-weight:bold; 74 | } 75 | 76 | ol {counter-reset: repas;} /* on initialise et nomme un compteur */ 77 | li { 78 | list-style-type: none; 79 | counter-increment: repas; /* on incrémente le compteur à chaque nouveau li */ 80 | margin-bottom: 5px; 81 | } 82 | li:before { 83 | content: counter(repas); /* on affiche le compteur */ 84 | padding: 0 20px 6px; 85 | margin-right: 8px; 86 | vertical-align: top; 87 | background: #678; 88 | -moz-border-radius: 60px; 89 | border-radius: 60px; 90 | font-weight: bold; 91 | font-size: 0.8em; 92 | color: white; 93 | } -------------------------------------------------------------------------------- /src/main/webapp/resources/js/script.js: -------------------------------------------------------------------------------- 1 | function colourize() { 2 | var dnl = document.getElementsByTagName("tr"); 3 | for (i = 0; i < dnl.length; i++) { 4 | if ((Math.round(i / 2) * 2) == ((i / 2) * 2)) 5 | dnl.item(i).style.background = "#E8E8FF"; 6 | } 7 | } 8 | window.onload = colourize; --------------------------------------------------------------------------------