Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create a Hazelcast instance embedded in-process/in-memory, without networking?

Tags:

java

hazelcast

In my unit tests, I want to create an embedded (in-process/in-memory) Hazelcast instance that does not attempt to start or perform any networking operations at all.

How do I do this?

For example:

Config config = new Config();

// what goes here?

HazelcastInstance inProcessOnly = Hazelcast.newHazelcastInstance(config);
like image 881
Les Hazlewood Avatar asked Jan 12 '13 03:01

Les Hazlewood


People also ask

What is embedded Hazelcast?

Embedded Mode Hazelcast is written in Java, which means that Java developers can integrate, or embed, it as a library in their applications. This topology is called embedded mode. All you need to do is add the Hazelcast JAR file to the application's classpath and start a member with the Java API.


2 Answers

FWIW I have created a test in Hazelcast 3.6.1 and programmatically disabled the network cluster using the following code in the constructor. This creates a standalone server for testing.

It's not as quick as using a mock but it is faster than accepting the default config.

Config config = new Config();
config.setProperty("hazelcast.shutdownhook.enabled", "false");
NetworkConfig network = config.getNetworkConfig();
network.getJoin().getTcpIpConfig().setEnabled(false);
network.getJoin().getMulticastConfig().setEnabled(false);
HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
like image 52
Ayub Malik Avatar answered Oct 29 '22 07:10

Ayub Malik


You can also use TestHazelcastInstanceFactory which is also used by Hazelcast internally for unit tests. You will need to add this Maven dependency for it:

<dependency>
  <groupId>com.hazelcast</groupId>
  <artifactId>hazelcast</artifactId>
  <version>${hazelcast.version}</version>
  <classifier>tests</classifier>
  <scope>test</scope>
</dependency>

See BasicCacheTest for an example of how it used.

like image 38
Wim Deblauwe Avatar answered Oct 29 '22 07:10

Wim Deblauwe