Spring Boot Starter
Spring Boot makes it easy to create and use third-party libraries, such as Hazelcast, with minimum configurations possible. While Spring Boot provides starters for some libraries, Hazelcast hosts its own starter.
Let’s create a simple Spring Boot application which starts a Hazelcast member and auto-wires it.
1. Create a New Java Project
We assume you’re using an IDE. Create a blank Java project named
tutorial-jet-starter
and copy the Gradle or Maven file into it:
plugins {
id 'org.springframework.boot' version '2.2.6.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'org.example'
version '1.0-SNAPSHOT'
repositories.mavenCentral()
dependencies {
implementation 'com.hazelcast.jet.contrib:hazelcast-jet-spring-boot-starter:2.0.0'
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>org.example</groupId>
<artifactId>tutorial-jet-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.hazelcast.jet.contrib</groupId>
<artifactId>hazelcast-jet-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</project>
2. Create the Application Main Class
The following code creates a Spring Boot application which starts a Jet member with default configuration.
package org.example;
import com.hazelcast.jet.JetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TutorialApplication {
@Autowired
JetService jet;
public static void main(String[] args) {
SpringApplication.run(TutorialApplication.class, args);
}
}
When you run it on your IDE, you should see in the logs that a Hazelcast member is started and the default configuration file is used:
...
c.h.i.config.AbstractConfigLocator : Loading 'hazelcast-jet-default.xml' from the classpath.
...
c.h.i.config.AbstractConfigLocator : Loading 'hazelcast-jet-member-default.xml' from the classpath.
...
3. Custom Configuration
Let’s add some custom configuration to our Hazelcast member by defining a
configuration file named hazelcast.yaml
at the root directory.
hazelcast:
jet:
instance:
cooperative-thread-count: 4
edge-defaults:
queue-size: 2048
To configure the underlying HazelcastInstance
we’ll define a
configuration file named hazelcast.yaml
at the root directory.
hazelcast:
cluster-name: tutorial-jet-starter
When you stop and re-run the main class you should now see that the configuration files we’ve just created is used to start the member:
...
c.h.i.config.AbstractConfigLocator : Loading 'hazelcast.yaml' from the working directory.
...
c.h.i.config.AbstractConfigLocator : Loading 'hazelcast.yaml' from the working directory.
...
Using Properties File
If your configuration files are not at the root directory or you want to
use a different name then you can create an application.properties
file and set the hazelcast.jet.server.config
and hazelcast.jet.imdg.config
like below:
hazelcast.jet.server.config=file:config/hazelcast-jet-tutorial.yaml
hazelcast.jet.imdg.config=file:config/hazelcast-tutorial.yaml
Since Spring Boot converts these config properties to resource URLs,
you need to use file:
prefix for files at the working directory and
classpath:
for files on the classpath.
Using System Properties
You can also set configuration files using system property:
System.setProperty("hazelcast.jet.config", "config/hazelcast-jet-tutorial.yaml");
System.setProperty("hazelcast.config", "config/hazelcast-tutorial.yaml");
This will work if your configuration files are at the working
directory. If they are on the classpath you should use classpath:
prefix.
4. Hazelcast Client
If you have a Hazelcast cluster already running and want to connect to it
with a client all you need to do is to put a client configuration file
(hazelcast-client.yaml
) to the root directory instead of the Hazelcast
configuration:
hazelcast-client:
cluster-name: tutorial-jet-starter
network:
cluster-members:
- 127.0.0.1
Using Properties File
If your configuration file is not at the root directory or you want to
use a different name then you can create an application.properties
file and set the hazelcast.jet.client.config
like below:
hazelcast.jet.client.config=file:config/hazelcast-client-tutorial.yaml
You need to use file:
prefix for files at the working directory and
classpath:
for files on the classpath.