Let us assume that there is a distributed setup of microservices, which can dockerized or not, just for us to apply the following steps in building Hazelcast caching:
- Open the pom.xml file of the ch12-hiber project and add the following Spring Cache starter POM dependency, as follows:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
- Then, add the Spring Boot 2.0 starter POM for Hazelcast support. Also, include the updated and stable version of the Hazelcast-Spring external libraries needed to configure the Hazelcast cache manager for the Spring 5 application, as follows:
<dependency> <groupId>com.hazelcast</groupId> <artifactId>hazelcast</artifactId> </dependency> <dependency> <groupId>com.hazelcast</groupId> <artifactId>hazelcast-spring</artifactId> <version>3.8.3</version> </dependency>
- For the serialization of ArrayList<T> and data models, add the following supplementary cache libraries that can help avoid Hazelcast serialization-related exceptions:
<dependency> <groupId>javax.cache</groupId> <artifactId>cache-api</artifactId> </dependency>
- Check whether all data models in the org.packt.hiber.core.model.data package implement java.io.Serializable. All entity models must be serializable, and that is a requirement for Hazelcast caching.
- Now, it is time to build the caching configuration class. Inside the org.packt.hiber.core.config, package and add the CachingConfig class that builds the cache manager with the Hazelcast instance and some properties. Do not forget to apply the @EnableCaching class-level annotation:
@Configuration @EnableCaching public class CachingConfig { @Bean public Config hazelCastConfig() { Config config = new Config(); config.setInstanceName("hazelcast-packt-cache"); config.setProperty("hazelcast.jmx", "true"); MapConfig deptCache = new MapConfig(); deptCache.setTimeToLiveSeconds(20); deptCache.setEvictionPolicy(EvictionPolicy.LFU); config.getMapConfigs().put("hazeldept",deptCache); return config; } @Bean public HazelcastInstance hazelcastInstance( Config hazelCastConfig) { return Hazelcast.newHazelcastInstance(hazelCastConfig); } @Bean public CacheManager cacheManager( HazelcastInstance hazelcastInstance) { return new HazelcastCacheManager(hazelcastInstance); } }
- To apply Hazelcast caching, open DepartmentDaoImpl and attach @Cacheable to the data retrieval operations:
@Repository public class DepartmentDaoImpl implements DepartmentDao{ @Autowired private SessionFactory sessionFactory; @Cacheable("hazeldept") @Override public List<Department> getAllDepts() { return sessionFactory.openSession() .createQuery("select d from Department d", Department.class).getResultList(); } @Cacheable("hazeldept") @Override public List<Department> getDeptsByName(String name) { return sessionFactory.openSession() .createQuery("select d from Department d where d.name LIKE '%:name%'", Department.class) .setParameter("name", name).getResultList(); } // refer to sources }
- Save all files. Deploy the project and check the log file:
15:45:22.591 [main] INFO com.hazelcast.system - [192.168.56.1]:5701 [dev] [3.8.2] Hazelcast 3.8.2 (20170518 - a60f944) starting at [192.168.56.1]:5701 15:45:22.591 [main] INFO com.hazelcast.system - [192.168.56.1]:5701 [dev] [3.8.2] Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved. 15:45:22.591 [main] INFO com.hazelcast.system - [192.168.56.1]:5701 [dev] [3.8.2] Configured Hazelcast Serialization version : 1 15:45:22.820 [main] INFO c.h.s.i.o.impl.BackpressureRegulator - [192.168.56.1]:5701 [dev] [3.8.2] Backpressure is disabled 15:45:23.702 [main] INFO com.hazelcast.instance.Node - [192.168.56.1]:5701 [dev] [3.8.2] Creating MulticastJoiner 15:45:23.871 [main] INFO c.h.s.i.o.impl.OperationExecutorImpl - [192.168.56.1]:5701 [dev] [3.8.2] Starting 8 partition threads 15:45:23.871 [main] INFO c.h.s.i.o.impl.OperationExecutorImpl - [192.168.56.1]:5701 [dev] [3.8.2] Starting 5 generic threads (1 dedicated for priority tasks) 15:45:23.871 [main] INFO com.hazelcast.core.LifecycleService - [192.168.56.1]:5701 [dev] [3.8.2] [192.168.56.1]:5701 is STARTING 15:45:26.435 [main] INFO com.hazelcast.system - [192.168.56.1]:5701 [dev] [3.8.2] Cluster version set to 3.8 15:45:26.437 [main] INFO c.h.i.cluster.impl.MulticastJoiner - [192.168.56.1]:5701 [dev] [3.8.2] Members [1] { Member [192.168.56.1]:5701 - 3b7936c6-9eda-479a-ba21-02552e3b24b1
this } 15:45:26.477 [main] INFO c.h.internal.jmx.ManagementService - [192.168.56.1]:5701 [dev] [3.8.2] Hazelcast JMX agent enabled. 15:45:26.501 [main] INFO com.hazelcast.core.LifecycleService - [192.168.56.1]:5701 [dev] [3.8.2] [192.168.56.1]:5701 is STARTED
- Again, run the same application but with a different port. Observe the following log file:
15:54:54.784 [main] INFO c.h.i.cluster.impl.MulticastJoiner - [192.168.56.1]:5702 [dev] [3.8.2] Trying to join to discovered node: [192.168.56.1]:5701 15:54:54.786 [hz.hazelcast-packt-cache.cached.thread-2] INFO c.h.nio.tcp.InitConnectionTask - [192.168.56.1]:5702 [dev] [3.8.2] Connecting to /192.168.56.1:5701, timeout: 0, bind-any: true 15:54:54.796 [hz.hazelcast-packt-cache.IO.thread-Acceptor] INFO c.h.nio.tcp.SocketAcceptorThread - [192.168.56.1]:5701 [dev] [3.8.2] Accepting socket connection from /192.168.56.1:31400 15:54:54.806 [hz.hazelcast-packt-cache.cached.thread-2] INFO c.h.nio.tcp.TcpIpConnectionManager - [192.168.56.1]:5702 [dev] [3.8.2] Established socket connection between /192.168.56.1:31400 and /192.168.56.1:5701
15:54:54.806 [hz.hazelcast-packt-cache.cached.thread-3] INFO c.h.nio.tcp.TcpIpConnectionManager - [192.168.56.1]:5701 [dev] [3.8.2] Established socket connection between /192.168.56.1:5701 and /192.168.56.1:31400