Weitere ähnliche Inhalte Ähnlich wie Clustering Made Easier: Using Terracotta with Hibernate and/or EHCache (20) Mehr von Cris Holdorph (9) Kürzlich hochgeladen (20) Clustering Made Easier: Using Terracotta with Hibernate and/or EHCache1. Clustering Made Easier Using Terracotta with Hibernate and/or Ehcache Cris J. Holdorph Software Architect Unicon, Inc. Jasig Conference San Diego, CA March 9, 2010 © Copyright Unicon, Inc., 2008. Some rights reserved. This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/us/ 43. ehcache.xml <ehcache> <diskStore path="java.io.tmpdir" /> <defaultCache maxElementsInMemory="10" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="300" overflowToDisk="true" /> <cache name="PEOPLE_CACHE" maxElementsInMemory="2000" eternal="false" overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="3600" /> </ehcache> 44. CensusCollectServlet.java import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; … private Cache peopleCache = null; … public void init(ServletConfig config) throws ServletException { super.init(config); CacheManager manager = new CacheManager(); peopleCache = manager.getCache("PEOPLE_CACHE"); } ... 45. CensusCollectServlet.java ... Person person = null; Element element = peopleCache.get(ssn); if (person == null) { person = new Person(); } else { person = (Person)element.getValue(); } request.setAttribute(PERSON, person); ... peopleCache.put(new Element(ssn, person)); ... 47. ehcache.xml <cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution. RMICacheManagerPeerProviderFactory" properties="hostName=fully_qualified_hostname_or_ip, peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446, timeToLive=32"/> <cache name=" PEOPLE_CACHE " ...> <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory” properties="replicateAsynchronously=true, replicatePuts=true, replicatePutsViaCopy=false, replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true asynchronousReplicationIntervalMillis=1000"/> </cache> 49. pom.xml <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> <version>1.5.8</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-terracotta</artifactId> <version>1.8.0</version> </dependency> 50. ehcache.xml <ehcache> <diskStore path="java.io.tmpdir" /> <defaultCache maxElementsInMemory="10" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="300" overflowToDisk="true" /> <terracottaConfig url="localhost:9510"/> <cache name="PEOPLE_CACHE" maxElementsInMemory="2000" eternal="false" overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="3600" <terracotta /> </cache> </ehcache> 51. Terracotta Server $ terracotta-3.2.0/bin/start-tc-server.sh Start the Terracotta Server First The Terracotta server must be started before any applications that use Terracotta. 53. hibernate.cfg.xml <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> <property name="connection.url">jdbc:hsqldb:hsql://localhost:8887</property> <property name="connection.username">sa</property> <property name="connection.password"></property> <property name="connection.pool_size">2</property> <property name="dialect">org.hibernate.dialect.HSQLDialect</property> . . . 54. hibernate.cfg.xml (2) . . . <property name="current_session_context_class">org.hibernate.context.ManagedSessionContext</property> <property name="cache.provider_class"> org.hibernate.cache.NoCacheProvider </property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">validate</property> <mapping resource="org/jasig/census/Person.hbm.xml"/> </session-factory> </hibernate-configuration> 55. Person.hbm.xml <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="org.jasig.census"> <class name="Person" table="PERSON"> <cache usage="read-write" /> <id name="id" column="PERSON_ID"> <generator class="native"/> </id> <property name="name"/> <property name="city"/> <property name="state"/> <property name="ssn"/> </class> </hibernate-mapping> 56. pom.xml <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.3.2.GA</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.8.0.GA</version> </dependency> <dependency> <groupId>hsqldb</groupId> <artifactId>hsqldb</artifactId> <version>1.8.0.7</version> </dependency> 57. getAllPeople() (.java code) public static List<?> getAllPeople() { SessionFactory sf = getSessionFactory(); Session session = sf.openSession(); session.beginTransaction(); List<?> result = session.createQuery("from Person").list(); session.getTransaction().commit(); session.close(); return result; } 59. pom.xml <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.3.2.GA</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>3.3.2.GA</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.8.0.GA</version> </dependency> . . . 60. hibernate.cfg.xml . . . <property name="current_session_context_class">org.hibernate.context.ManagedSessionContext</property> <property name="use_second_level_cache">true</property> <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">validate</property> <mapping resource="org/jasig/census/Person.hbm.xml"/> </session-factory> </hibernate-configuration> 61. ehcache.xml <ehcache> <diskStore path="java.io.tmpdir" /> <defaultCache maxElementsInMemory="10" overflowToDisk="true" timeToIdleSeconds="120" timeToLiveSeconds="300" eternal="false" /> <cache name="org.jasig.census.Person" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="3000" timeToLiveSeconds="6000" overflowToDisk="false" /> <cache name="query.findAllPeople" maxElementsInMemory="50" eternal="false" timeToLiveSeconds="86400" overflowToDisk="false" /> </ehcache> 62. getAllPeople() (.java code) public static List<?> getAllPeople() { SessionFactory sf = getSessionFactory(); Session session = sf.openSession(); session.beginTransaction(); List<?> result = session.createQuery("from Person"). setCacheable(true).setCacheRegion("query.findAllPeople"). list(); session.getTransaction().commit(); session.close(); return result; } 64. pom.xml <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.3.2.GA</version> </dependency> <dependency> <groupId>org.terracotta.hibernate</groupId> <artifactId>terracotta-hibernate-cache</artifactId> <version>1.1.0</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.8.0.GA</version> </dependency> . . . 65. pom.xml (2) . . . <repositories> <repository> <id>terracotta-repository</id> <url>http://www.terracotta.org/download/reflector/maven2</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> </project> 66. hibernate.cfg.xml . . . <property name="current_session_context_class">org.hibernate.context.ManagedSessionContext</property> <property name="use_second_level_cache">true</property> <property name="cache.provider_class"> org.terracotta.hibernate.TerracottaHibernateCacheProvider </property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">validate</property> <mapping resource="org/jasig/census/Person.hbm.xml"/> </session-factory> </hibernate-configuration> 67. Terracotta Server $ /opt/terracotta-3.2.0/bin/start-tc-server.sh Start the Terracotta Server First The Terracotta server must be started before any applications that use Terracotta. 68. Hibernate Application $ export JAVA_OPTS="-javaagent:/opt/terracotta-3.2.0/hibernate/terracotta-hibernate-agent-1.1.0.jar" $ /opt/apache-tomcat-6.0.24/bin/startup.sh Start Application with Options The application must be started with special JAVA_OPTS set for Terracotta. 96. Questions & Answers Cris J. Holdorph Software Architect Unicon, Inc. [email_address] www.unicon.net