All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.guicedee.guicedhazelcast.implementations.HazelcastClientProvider Maven / Gradle / Ivy

Go to download

An integration library for Hazelcast and Guice utilizing a GuiceContext Injector.

There is a newer version: 62
Show newest version
package com.guicedee.guicedhazelcast.implementations;

import com.google.inject.Provider;
import com.google.inject.ProvisionException;
import com.guicedee.guicedinjection.interfaces.IGuicePreDestroy;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.config.InvalidConfigurationException;
import com.hazelcast.core.HazelcastInstance;
import org.apache.commons.lang3.RandomUtils;

import java.util.Random;

import static com.guicedee.guicedhazelcast.services.HazelcastClientPreStartup.*;

public class HazelcastClientProvider
		implements Provider, IGuicePreDestroy
{

	@Override
	public HazelcastInstance get()
	{
		//Only start a client instance if i didn't start a local one
		if (ModuleLayer.boot()
		               .findModule("za.co.bayport.jpms.caching")
		               .isEmpty())
		{
			try
			{
				clientInstance = HazelcastClient.newHazelcastClient(config);
			}catch (ProvisionException | InvalidConfigurationException pe)
			{
				config.setInstanceName(config.getInstanceName() + "_" + RandomUtils.nextInt(1, 100));
				clientInstance = HazelcastClient.newHazelcastClient(config);
			}
		}
		return clientInstance;
	}

	@Override
	public void onDestroy()
	{
		if (clientInstance != null)
		{
			clientInstance.shutdown();
			clientInstance = null;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy