Class CassandraBuilder
- java.lang.Object
-
- com.github.nosan.embedded.cassandra.CassandraBuilder
-
public class CassandraBuilder extends Object
A builder that can be used to configure and createCassandra.This class is not thread safe and should not be shared across different threads!
- Since:
- 4.0.0
- Author:
- Dmytro Nosan
- See Also:
build(),CassandraBuilderConfigurator
-
-
Field Summary
Fields Modifier and Type Field Description static VersionDEFAULT_VERSIONDefault Cassandra version.
-
Constructor Summary
Constructors Constructor Description CassandraBuilder()Creates a newCassandraBuilder.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CassandraBuilderaddConfigProperties(Map<String,?> configProperties)Adds multiple Cassandra configuration properties to the current configuration, merging them with the properties defined in the `cassandra.yaml` file.CassandraBuilderaddConfigProperty(String name, Object value)Sets a single Cassandra configuration property, which will be merged with properties defined in the `cassandra.yaml` file.CassandraBuilderaddEnvironmentVariable(String name, Object value)Adds a Cassandra environment variable to the current configuration.CassandraBuilderaddEnvironmentVariables(Map<String,?> environmentVariables)Adds multiple Cassandra environment variables to the current configuration.CassandraBuilderaddJvmOptions(String... jvmOptions)Adds Cassandra native Java Virtual Machine (JVM) options to the current configuration.CassandraBuilderaddJvmOptions(Collection<String> jvmOptions)Adds the specified Cassandra native Java Virtual Machine (JVM) options to the existing configuration.CassandraBuilderaddSystemProperties(Map<String,?> systemProperties)Adds multiple Cassandra native Java Virtual Machine (JVM) system properties to the current configuration.CassandraBuilderaddSystemProperty(String name, Object value)Adds a single Cassandra native Java Virtual Machine (JVM) system property to the current configuration.CassandraBuilderaddWorkingDirectoryCustomizers(WorkingDirectoryCustomizer... workingDirectoryCustomizers)Adds one or moreWorkingDirectoryCustomizerinstances to customize the working directory.CassandraBuilderaddWorkingDirectoryCustomizers(Collection<? extends WorkingDirectoryCustomizer> workingDirectoryCustomizers)Adds one or moreWorkingDirectoryCustomizerinstances to the current configuration.CassandraBuilderaddWorkingDirectoryResource(Resource resource, String path)Copies a resource to a specified target path within the working directory.Cassandrabuild()Build a newCassandrainstance.CassandraBuilderconfigFile(Resource configFile)Sets the Cassandra configuration file path.CassandraBuilderconfigProperties(Map<String,?> configProperties)Sets the Cassandra configuration properties to be merged with the properties defined in the `cassandra.yaml` file.CassandraBuilderconfigure(CassandraBuilderConfigurator configurator)Applies the specified configurator to customize this builder.CassandraBuilderenvironmentVariables(Map<String,?> environmentVariables)Sets the Cassandra environment variables.StringgetName()Gets the currently configured Cassandra instance name.VersiongetVersion()Gets the currently configured Cassandra version.CassandraBuilderjvmOptions(String... jvmOptions)Sets Cassandra native Java Virtual Machine (JVM) Options.CassandraBuilderjvmOptions(Collection<String> jvmOptions)Sets the Cassandra native Java Virtual Machine (JVM) options.CassandraBuilderlogger(org.slf4j.Logger logger)Sets the Cassandra logger.CassandraBuildername(String name)Sets the Cassandra instance name.CassandraBuilderregisterShutdownHook(boolean registerShutdownHook)Specifies whether the createdCassandrainstance should have a shutdown hook registered.CassandraBuilderstartupTimeout(Duration startupTimeout)Sets the startup timeout.CassandraBuildersystemProperties(Map<String,?> systemProperties)Sets the Cassandra native Java Virtual Machine (JVM) system properties.CassandraBuilderversion(Version version)Sets the Cassandra version.CassandraBuilderversion(String version)Sets the Cassandra version.CassandraBuilderworkingDirectory(IOSupplier<? extends Path> workingDirectorySupplier)Sets theSupplierfor the working directory, which will be invoked each timebuild()is called.CassandraBuilderworkingDirectoryCustomizers(WorkingDirectoryCustomizer... workingDirectoryCustomizers)Sets theWorkingDirectoryCustomizerto customize the working directory.CassandraBuilderworkingDirectoryCustomizers(Collection<? extends WorkingDirectoryCustomizer> workingDirectoryCustomizers)Sets theWorkingDirectoryCustomizerinstances to customize the working directory.CassandraBuilderworkingDirectoryDestroyer(WorkingDirectoryDestroyer workingDirectoryDestroyer)Sets theWorkingDirectoryDestroyerto handle the cleanup of the working directory.CassandraBuilderworkingDirectoryInitializer(WorkingDirectoryInitializer workingDirectoryInitializer)Sets theWorkingDirectoryInitializerto be used for initializing the working directory.
-
-
-
Field Detail
-
DEFAULT_VERSION
public static final Version DEFAULT_VERSION
Default Cassandra version.
-
-
Constructor Detail
-
CassandraBuilder
public CassandraBuilder()
Creates a newCassandraBuilder.
-
-
Method Detail
-
name
public CassandraBuilder name(String name)
Sets the Cassandra instance name.Defaults to "cassandra-0", "cassandra-1", and so on.
- Parameters:
name- the Cassandra instance name- Returns:
- this builder
-
getName
public String getName()
Gets the currently configured Cassandra instance name.- Returns:
- the Cassandra instance name, or
nullif not configured
-
version
public CassandraBuilder version(String version)
Sets the Cassandra version.Defaults to
DEFAULT_VERSION.- Parameters:
version- the Cassandra version to set- Returns:
- this builder
-
version
public CassandraBuilder version(Version version)
Sets the Cassandra version.Defaults to
DEFAULT_VERSION.- Parameters:
version- the Cassandra version to be set- Returns:
- this builder instance
-
getVersion
public Version getVersion()
Gets the currently configured Cassandra version.Defaults to
DEFAULT_VERSIONif no version is explicitly set.- Returns:
- the configured Cassandra version, never
null
-
logger
public CassandraBuilder logger(org.slf4j.Logger logger)
Sets the Cassandra logger. This logger will capture and consume Cassandra's stdout and stderr outputs.Defaults to
LoggerFactory.getLogger(Cassandra.class).- Parameters:
logger- the logger to be used for Cassandra- Returns:
- this builder instance
-
registerShutdownHook
public CassandraBuilder registerShutdownHook(boolean registerShutdownHook)
Specifies whether the createdCassandrainstance should have a shutdown hook registered.Defaults to
true.- Parameters:
registerShutdownHook-trueto register a shutdown hook,falseotherwise- Returns:
- this builder instance
-
startupTimeout
public CassandraBuilder startupTimeout(Duration startupTimeout)
Sets the startup timeout.Defaults to 2 minutes.
- Parameters:
startupTimeout- the startup timeout- Returns:
- this builder
-
configFile
public CassandraBuilder configFile(Resource configFile)
Sets the Cassandra configuration file path.This is equivalent to:
addSystemProperty("cassandra.config", configFile);- Parameters:
configFile- the path to the Cassandra configuration file, must not benull- Returns:
- this builder instance
-
workingDirectory
public CassandraBuilder workingDirectory(IOSupplier<? extends Path> workingDirectorySupplier)
Sets theSupplierfor the working directory, which will be invoked each timebuild()is called.The supplied directory will be initialized by
WorkingDirectoryInitializerand used as the Cassandra home directory.At the end of the process, the provided working directory may be fully or partially deleted by
WorkingDirectoryDestroyer.Defaults to
Files.createTempDirectory("").- Parameters:
workingDirectorySupplier- the supplier providing the working directory, must not benull- Returns:
- this builder instance
- See Also:
IOSupplier.wrap(Supplier)
-
workingDirectoryInitializer
public CassandraBuilder workingDirectoryInitializer(WorkingDirectoryInitializer workingDirectoryInitializer)
Sets theWorkingDirectoryInitializerto be used for initializing the working directory.Defaults to
DefaultWorkingDirectoryInitializer, which uses the underlyingWebCassandraDirectoryProvider.- Parameters:
workingDirectoryInitializer- the initializer responsible for setting up the working directory, must not benull- Returns:
- this builder instance
- See Also:
DefaultWorkingDirectoryInitializer
-
workingDirectoryDestroyer
public CassandraBuilder workingDirectoryDestroyer(WorkingDirectoryDestroyer workingDirectoryDestroyer)
Sets theWorkingDirectoryDestroyerto handle the cleanup of the working directory.Defaults to
WorkingDirectoryDestroyer.deleteAll().- Parameters:
workingDirectoryDestroyer- the destroyer responsible for managing the cleanup of the working directory, must not benull- Returns:
- this builder instance
- See Also:
WorkingDirectoryDestroyer.doNothing(),WorkingDirectoryDestroyer.deleteOnly(String...),WorkingDirectoryDestroyer.deleteAll()
-
workingDirectoryCustomizers
public CassandraBuilder workingDirectoryCustomizers(WorkingDirectoryCustomizer... workingDirectoryCustomizers)
Sets theWorkingDirectoryCustomizerto customize the working directory. Setting this value will replace any previously configured customizers.- Parameters:
workingDirectoryCustomizers- the customizers to use for configuring the working directory, must not benull- Returns:
- this builder instance
- See Also:
WorkingDirectoryCustomizer.addResource(Resource, String),workingDirectoryCustomizers(Collection),addWorkingDirectoryCustomizers(WorkingDirectoryCustomizer...),addWorkingDirectoryCustomizers(Collection)
-
workingDirectoryCustomizers
public CassandraBuilder workingDirectoryCustomizers(Collection<? extends WorkingDirectoryCustomizer> workingDirectoryCustomizers)
Sets theWorkingDirectoryCustomizerinstances to customize the working directory. This operation will replace any previously configured customizers.- Parameters:
workingDirectoryCustomizers- the customizers to configure the working directory, must not benull- Returns:
- this builder instance
- See Also:
WorkingDirectoryCustomizer.addResource(Resource, String),workingDirectoryCustomizers(WorkingDirectoryCustomizer...),addWorkingDirectoryCustomizers(WorkingDirectoryCustomizer...),addWorkingDirectoryCustomizers(Collection)
-
addWorkingDirectoryCustomizers
public CassandraBuilder addWorkingDirectoryCustomizers(WorkingDirectoryCustomizer... workingDirectoryCustomizers)
Adds one or moreWorkingDirectoryCustomizerinstances to customize the working directory.- Parameters:
workingDirectoryCustomizers- the customizers to add, must not benull- Returns:
- this builder instance
- See Also:
WorkingDirectoryCustomizer.addResource(Resource, String),addWorkingDirectoryCustomizers(Collection),workingDirectoryCustomizers(WorkingDirectoryCustomizer...),workingDirectoryCustomizers(Collection)
-
addWorkingDirectoryCustomizers
public CassandraBuilder addWorkingDirectoryCustomizers(Collection<? extends WorkingDirectoryCustomizer> workingDirectoryCustomizers)
Adds one or moreWorkingDirectoryCustomizerinstances to the current configuration.- Parameters:
workingDirectoryCustomizers- the customizers to add, must not benullor empty- Returns:
- this builder instance for chaining
- See Also:
WorkingDirectoryCustomizer.addResource(Resource, String),addWorkingDirectoryCustomizers(WorkingDirectoryCustomizer...),workingDirectoryCustomizers(WorkingDirectoryCustomizer...),workingDirectoryCustomizers(Collection)
-
environmentVariables
public CassandraBuilder environmentVariables(Map<String,?> environmentVariables)
Sets the Cassandra environment variables. This operation will replace any previously configured environment variables.Example usage:
Map<String, Object> environmentVariables = new LinkedHashMap<>(); environmentVariables.put("TZ", "Europe/London"); builder.environmentVariables(environmentVariables);- Parameters:
environmentVariables- a map containing the Cassandra environment variables, must not benull- Returns:
- this builder instance for method chaining
- See Also:
addEnvironmentVariable(String, Object),addEnvironmentVariables(Map)
-
addEnvironmentVariable
public CassandraBuilder addEnvironmentVariable(String name, Object value)
Adds a Cassandra environment variable to the current configuration.Example usage:
builder.addEnvironmentVariable("TZ", "Europe/London");- Parameters:
name- the name of the Cassandra environment variable, must not benullor emptyvalue- the value of the Cassandra environment variable, must not benull- Returns:
- this builder instance for method chaining
- See Also:
addEnvironmentVariables(Map),environmentVariables(Map)
-
addEnvironmentVariables
public CassandraBuilder addEnvironmentVariables(Map<String,?> environmentVariables)
Adds multiple Cassandra environment variables to the current configuration.Example usage:
Map<String, Object> environmentVariables = new LinkedHashMap<>(); environmentVariables.put("TZ", "Europe/London"); builder.addEnvironmentVariables(environmentVariables);- Parameters:
environmentVariables- a map containing Cassandra environment variables, must not benullor empty- Returns:
- this builder instance for method chaining
- See Also:
addEnvironmentVariable(String, Object),environmentVariables(Map)
-
systemProperties
public CassandraBuilder systemProperties(Map<String,?> systemProperties)
Sets the Cassandra native Java Virtual Machine (JVM) system properties. This action replaces any previously configured system properties.Example usage:
Map<String, Object> systemProperties = new LinkedHashMap<>(); systemProperties.put("cassandra.config", new ClassPathResource("cassandra.yaml")); systemProperties.put("cassandra.native_transport_port", 9042); systemProperties.put("cassandra.jmx.local.port", 7199); builder.systemProperties(systemProperties);- Parameters:
systemProperties- a map of Cassandra system properties, must not benullor empty- Returns:
- this builder instance for method chaining
- See Also:
addSystemProperty(String, Object)
-
addSystemProperty
public CassandraBuilder addSystemProperty(String name, Object value)
Adds a single Cassandra native Java Virtual Machine (JVM) system property to the current configuration.Example usage:
builder.addSystemProperty("cassandra.config", new ClassPathResource("cassandra.yaml")); builder.addSystemProperty("user.timezone", "Europe/London");- Parameters:
name- the name of the Cassandra system property, must not benullor emptyvalue- the value of the Cassandra system property, must not benull- Returns:
- this builder instance for method chaining
- See Also:
addSystemProperties(Map)
-
addSystemProperties
public CassandraBuilder addSystemProperties(Map<String,?> systemProperties)
Adds multiple Cassandra native Java Virtual Machine (JVM) system properties to the current configuration. This method can be used to set or override existing system properties.Example usage:
Map<String, Object> systemProperties = new LinkedHashMap<>(); systemProperties.put("cassandra.config", new ClassPathResource("cassandra.yaml")); systemProperties.put("cassandra.native_transport_port", 9042); systemProperties.put("cassandra.jmx.local.port", 7199); builder.addSystemProperties(systemProperties);- Parameters:
systemProperties- a map of Cassandra system properties, must not benullor empty- Returns:
- this builder instance for method chaining
- See Also:
addSystemProperty(String, Object)
-
jvmOptions
public CassandraBuilder jvmOptions(String... jvmOptions)
Sets Cassandra native Java Virtual Machine (JVM) Options. Setting this value will replace any previously configured options.- Parameters:
jvmOptions- the JVM options to set- Returns:
- this builder
- See Also:
jvmOptions(Collection),addJvmOptions(Collection),addJvmOptions(String...)
-
jvmOptions
public CassandraBuilder jvmOptions(Collection<String> jvmOptions)
Sets the Cassandra native Java Virtual Machine (JVM) options. This method replaces any previously configured JVM options with the specified values.Example usage:
builder.setJvmOptions(Arrays.asList("-Xmx1024m", "-Xms512m"));- Parameters:
jvmOptions- a collection of JVM options to set, must not benullor containnullelements- Returns:
- this builder instance for method chaining
- See Also:
jvmOptions(Collection),addJvmOptions(Collection),addJvmOptions(String...)
-
addJvmOptions
public CassandraBuilder addJvmOptions(String... jvmOptions)
Adds Cassandra native Java Virtual Machine (JVM) options to the current configuration. This method appends the specified options to the existing list without replacing them.Example usage:
builder.addJvmOptions("-Xmx1024m", "-Xms512m");- Parameters:
jvmOptions- the JVM options to add, must not benullor containnullelements- Returns:
- this builder instance for method chaining
- See Also:
addJvmOptions(Collection),jvmOptions(Collection),jvmOptions(String...)
-
addJvmOptions
public CassandraBuilder addJvmOptions(Collection<String> jvmOptions)
Adds the specified Cassandra native Java Virtual Machine (JVM) options to the existing configuration. This method appends the given options rather than replacing any previously configured ones.Example usage:
builder.addJvmOptions(Arrays.asList("-Xmx2048m", "-Xms1024m"));- Parameters:
jvmOptions- a collection of JVM options to add; must not benullor containnullelements- Returns:
- this builder instance for method chaining
- See Also:
addJvmOptions(String...),jvmOptions(Collection),jvmOptions(String...)
-
configProperties
public CassandraBuilder configProperties(Map<String,?> configProperties)
Sets the Cassandra configuration properties to be merged with the properties defined in the `cassandra.yaml` file. Setting this value replaces any previously configured properties.For example:
This will produce the following YAML output:Map<String, Object> properties = new LinkedHashMap<>(); properties.put("client_encryption_options.enabled", true); properties.put("cluster_name", "MyCluster"); builder.configProperties(properties);... cluster_name: "MyCluster" client_encryption_options: enabled: true ... ...Note that this method fully replaces any previously set configuration properties. To add or modify specific properties incrementally, use
addConfigProperty(String, Object)oraddConfigProperties(Map).- Parameters:
configProperties- the Cassandra configuration properties to set; must not benull- Returns:
- this builder instance for method chaining
- See Also:
addConfigProperty(String, Object),addConfigProperties(Map)
-
addConfigProperty
public CassandraBuilder addConfigProperty(String name, Object value)
Sets a single Cassandra configuration property, which will be merged with properties defined in the `cassandra.yaml` file.For example:
builder.configProperty("client_encryption_options.enabled", true) .configProperty("cluster_name", "MyCluster");This will produce the following YAML output:
... cluster_name: "MyCluster" client_encryption_options: enabled: true ... ...This method allows for fine-grained updates of individual configuration properties. If multiple updates are needed, consider using
addConfigProperties(Map)for efficiency.- Parameters:
name- the name of the configuration property (e.g.,native_transport_port,client_encryption_options.enabled); must not benullor emptyvalue- the value of the configuration property- Returns:
- this builder instance for method chaining
- See Also:
addConfigProperties(Map),configProperties(Map)
-
addConfigProperties
public CassandraBuilder addConfigProperties(Map<String,?> configProperties)
Adds multiple Cassandra configuration properties to the current configuration, merging them with the properties defined in the `cassandra.yaml` file. This method allows for bulk updates of configuration settings.For example:
Map<String, Object> properties = new LinkedHashMap<>(); properties.put("client_encryption_options.enabled", true); properties.put("cluster_name", "MyCluster"); builder.configProperties(properties);This will result in the following YAML structure:
... cluster_name: "MyCluster" client_encryption_options: enabled: true ... ...Note that this method replaces any previously configured properties with the provided ones. If granular updates are needed, consider using
addConfigProperty(String, Object).- Parameters:
configProperties- a map of Cassandra configuration properties to add- Returns:
- this builder instance for method chaining
- See Also:
addConfigProperty(String, Object),configProperties(Map)
-
addWorkingDirectoryResource
public CassandraBuilder addWorkingDirectoryResource(Resource resource, String path)
Copies a resource to a specified target path within the working directory.This method is equivalent to the following:
addWorkingDirectoryCustomizers(WorkingDirectoryCustomizer.addResource(resource, path));Use this method to place files like configuration files, such as
conf/cassandra.yaml, into the appropriate location within the working directory.- Parameters:
path- the target path (file only) within the working directory (e.g.,conf/cassandra.yaml); must not benullor emptyresource- the resource to copy; must not benull- Returns:
- this builder instance for method chaining
- See Also:
WorkingDirectoryCustomizer.addResource(Resource, String)
-
configure
public CassandraBuilder configure(CassandraBuilderConfigurator configurator)
Applies the specified configurator to customize this builder.This method provides a way to modify the builder using the logic defined in the given configurator. Configurators can encapsulate reusable configuration logic, making it easier to apply pre-defined settings to multiple builders.
- Parameters:
configurator- the configurator to apply to this builder; must not benull- Returns:
- this builder instance for method chaining
- See Also:
for an example configurator
-
-