Featured image of post Use Hazelcast + Spring Boot

Use Hazelcast + Spring Boot

Install Hazelcast on MacOS

brew tap hazelcast/hz
brew install [email protected]
hz -V

Start Hazelcast

hz start

Use Hazelcast in Spring Boot

Method 1

package com.example.demo.controller;

import com.hazelcast.core.HazelcastInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.ConcurrentMap;

@RestController
public class HazelcastController {
    @Autowired
    HazelcastInstance hazelcastInstance;

    private ConcurrentMap<String, String> mapping() {
        return hazelcastInstance.getMap("map");
    }

    @GetMapping("/Hazelcast")
    public String Hazelcast(@RequestParam(value = "key") String key) throws InterruptedException {
        String res = mapping().get(key);
        return res;
    }

    @PostMapping("/Hazelcast")
    public String putHazelcast(@RequestParam(value = "key") String key, @RequestParam(value = "value") String value) {
        mapping().put(key, value);
        return "OK!";
    }
}

Test this project

cd ~/<Project Folder>

# bash 1
mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dserver.port=8080"

# bash 2
mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dserver.port=8081"
~ % curl "http://127.0.0.1:8080/Hazelcast?key=A"
~ % curl "http://127.0.0.1:8081/Hazelcast?key=A"
~ % curl -X POST "http://127.0.0.1:8080/Hazelcast?key=A&value=1"
OK!%                                                                     
~ % curl "http://127.0.0.1:8080/Hazelcast?key=A"
1%
~ % curl "http://127.0.0.1:8081/Hazelcast?key=A"
1%                                                                       
~ % curl -X POST "http://127.0.0.1:8081/Hazelcast?key=B&value=2"
OK!%                                                                     
~ % curl "http://127.0.0.1:8080/Hazelcast?key=B"
2%                                                                       
~ % curl "http://127.0.0.1:8081/Hazelcast?key=B"
2%

Method 2

application.properties

spring.hazelcast.config=classpath:config/my-hazelcast.xml
spring.cache.type=hazelcast

my-hazelcast.xml

save this file to resources/config/my-hazelcast.xml

<?xml version="1.0" encoding="UTF-8"?>
<hazelcast xmlns="http://www.hazelcast.com/schema/config"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="https://hazelcast.com/schema/config
           https://hazelcast.com/schema/config/hazelcast-config-5.3.xsd">

    <network>
        <join>
            <multicast enabled="false"/>
            <tcp-ip enabled="true">
                <member>127.0.0.1</member>
                <!--<member>127.0.0.1-21</member>-->
            </tcp-ip>
        </join>
    </network>

</hazelcast>
package com.example.demo.controller;

import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

@RestController
public class HazelcastController {

    @GetMapping("/Hazelcast")
    @Cacheable(value = "testKeyAndValue", key = "#key")
    public String Hazelcast(@RequestParam(value = "key") String key) throws InterruptedException {
        throw new ResponseStatusException(HttpStatus.NOT_FOUND);
    }

    @PostMapping("/Hazelcast")
    @CachePut(value = "testKeyAndValue", key = "#key")
    public String putHazelcast(@RequestParam(value = "key") String key, @RequestParam(value = "value") String value) {
        System.out.println(key + ", " + value);
        return key + ": " + value;
    }
}

Test this project

cd ~/<Project Folder>

# bash 1
~ % mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dserver.port=8081"

# bash 2
~ % mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dserver.port=8082"
~ % curl "http://127.0.0.1:8081/Hazelcast?key=A"
{"timestamp":"2030-01-01T00:00:00.000+00:00","status":404,"error":"Not Found","path":"/Hazelcast"}%
~ % curl "http://127.0.0.1:8082/Hazelcast?key=A"
{"timestamp":"2030-01-01T00:00:01.000+00:00","status":404,"error":"Not Found","path":"/Hazelcast"}%                                             
~ % curl -X POST "http://127.0.0.1:8081/Hazelcast?key=A&value=1"
A: 1%                                                                     
~ % curl "http://127.0.0.1:8081/Hazelcast?key=A"
A: 1%                                                                     
~ % curl "http://127.0.0.1:8082/Hazelcast?key=A"
A: 1%                                                                     
~ % curl -X POST "http://127.0.0.1:8082/Hazelcast?key=A&value=111"
A: 111%                                                                   
~ % curl "http://127.0.0.1:8081/Hazelcast?key=A"
A: 111%                                                                   
~ % curl "http://127.0.0.1:8082/Hazelcast?key=A"
A: 111%

 

Licensed under CC BY-NC-SA 4.0