Search in sources :

Example 6 with XmlClientConfigBuilder

use of com.hazelcast.client.config.XmlClientConfigBuilder in project hazelcast by hazelcast.

the class ClientToMemberDiscoveryTest method setup.

@Before
public void setup() {
    String serverXmlFileName = "hazelcast-multicast-plugin.xml";
    String clientXmlFileName = "hazelcast-client-multicast-plugin.xml";
    InputStream xmlResource = MulticastDiscoveryStrategy.class.getClassLoader().getResourceAsStream(serverXmlFileName);
    serverConfig = new XmlConfigBuilder(xmlResource).build();
    InputStream xmlClientResource = MulticastDiscoveryStrategy.class.getClassLoader().getResourceAsStream(clientXmlFileName);
    clientConfig = new XmlClientConfigBuilder(xmlClientResource).build();
}
Also used : XmlConfigBuilder(com.hazelcast.config.XmlConfigBuilder) XmlClientConfigBuilder(com.hazelcast.client.config.XmlClientConfigBuilder) InputStream(java.io.InputStream) MulticastDiscoveryStrategy(com.hazelcast.spi.discovery.multicast.MulticastDiscoveryStrategy) Before(org.junit.Before)

Example 7 with XmlClientConfigBuilder

use of com.hazelcast.client.config.XmlClientConfigBuilder in project hazelcast by hazelcast.

the class TestHazelcastFactory method newHazelcastClient.

public HazelcastInstance newHazelcastClient(ClientConfig config) {
    if (!mockNetwork) {
        return HazelcastClient.newHazelcastClient(config);
    }
    if (config == null) {
        config = new XmlClientConfigBuilder().build();
    }
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    HazelcastClientProxy proxy;
    try {
        if (tccl == ClassLoader.getSystemClassLoader()) {
            Thread.currentThread().setContextClassLoader(HazelcastClient.class.getClassLoader());
        }
        ClientConnectionManagerFactory clientConnectionManagerFactory = clientRegistry.createClientServiceFactory("127.0.0.1", CLIENT_PORTS);
        AddressProvider testAddressProvider = createAddressProvider(config);
        HazelcastClientInstanceImpl client = new HazelcastClientInstanceImpl(config, clientConnectionManagerFactory, testAddressProvider);
        client.start();
        clients.add(client);
        OutOfMemoryErrorDispatcher.registerClient(client);
        proxy = new HazelcastClientProxy(client);
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
    }
    return proxy;
}
Also used : AddressProvider(com.hazelcast.client.connection.AddressProvider) XmlClientConfigBuilder(com.hazelcast.client.config.XmlClientConfigBuilder) ClientConnectionManagerFactory(com.hazelcast.client.impl.ClientConnectionManagerFactory) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) HazelcastClient(com.hazelcast.client.HazelcastClient) HazelcastClientProxy(com.hazelcast.client.impl.HazelcastClientProxy)

Example 8 with XmlClientConfigBuilder

use of com.hazelcast.client.config.XmlClientConfigBuilder in project hazelcast by hazelcast.

the class HazelcastClientCachingProvider method getConfig.

private ClientConfig getConfig(URL configURL, ClassLoader theClassLoader, String instanceName) throws IOException {
    ClientConfig config = new XmlClientConfigBuilder(configURL).build();
    config.setClassLoader(theClassLoader);
    if (instanceName != null) {
        // If instance name is specified via properties use it
        // even though instance name is specified in the config.
        config.setInstanceName(instanceName);
    } else if (config.getInstanceName() == null) {
        // Use config url as instance name if instance name is not specified.
        config.setInstanceName(configURL.toString());
    }
    return config;
}
Also used : XmlClientConfigBuilder(com.hazelcast.client.config.XmlClientConfigBuilder) ClientConfig(com.hazelcast.client.config.ClientConfig)

Example 9 with XmlClientConfigBuilder

use of com.hazelcast.client.config.XmlClientConfigBuilder in project gora by apache.

the class JCacheStore method initialize.

@Override
public void initialize(Class<K> keyClass, Class<T> persistentClass, Properties properties) {
    super.initialize(keyClass, persistentClass, properties);
    CachingProvider cachingProvider = Caching.getCachingProvider(properties.getProperty(GORA_DEFAULT_JCACHE_PROVIDER_KEY));
    if (properties.getProperty(JCACHE_CACHE_NAMESPACE_PROPERTY_KEY) != null) {
        goraCacheNamespace = properties.getProperty(JCACHE_CACHE_NAMESPACE_PROPERTY_KEY);
    }
    try {
        this.persistentDataStore = DataStoreFactory.getDataStore(keyClass, persistentClass, new Configuration());
    } catch (GoraException ex) {
        LOG.error("Couldn't initialize persistent DataStore.", ex);
    }
    if (properties.getProperty(GORA_DEFAULT_JCACHE_PROVIDER_KEY).contains(HAZELCAST_SERVER_CACHE_PROVIDER_IDENTIFIER)) {
        Config config = new ClasspathXmlConfig(properties.getProperty(GORA_DEFAULT_JCACHE_HAZELCAST_CONFIG_KEY));
        hazelcastInstance = Hazelcast.newHazelcastInstance(config);
    } else {
        try {
            ClientConfig config = new XmlClientConfigBuilder(properties.getProperty(GORA_DEFAULT_JCACHE_HAZELCAST_CONFIG_KEY)).build();
            hazelcastInstance = HazelcastClient.newHazelcastClient(config);
        } catch (IOException ex) {
            LOG.error("Couldn't locate the client side cache provider configuration.", ex);
        }
    }
    Properties providerProperties = new Properties();
    providerProperties.setProperty(HazelcastCachingProvider.HAZELCAST_INSTANCE_NAME, hazelcastInstance.getName());
    try {
        manager = cachingProvider.getCacheManager(new URI(goraCacheNamespace), null, providerProperties);
    } catch (URISyntaxException ex) {
        LOG.error("Couldn't initialize cache manager to bounded hazelcast instance.", ex);
        manager = cachingProvider.getCacheManager();
    }
    if (((properties.getProperty(JCACHE_AUTO_CREATE_CACHE_PROPERTY_KEY) != null) && Boolean.valueOf(properties.getProperty(JCACHE_AUTO_CREATE_CACHE_PROPERTY_KEY))) || ((manager.getCache(super.getPersistentClass().getSimpleName(), keyClass, persistentClass) == null))) {
        cacheEntryList = new ConcurrentSkipListSet<>();
        cacheConfig = new CacheConfig<K, T>();
        cacheConfig.setTypes(keyClass, persistentClass);
        if (properties.getProperty(JCACHE_READ_THROUGH_PROPERTY_KEY) != null) {
            cacheConfig.setReadThrough(Boolean.valueOf(properties.getProperty(JCACHE_READ_THROUGH_PROPERTY_KEY)));
        } else {
            cacheConfig.setReadThrough(true);
        }
        if (properties.getProperty(JCACHE_WRITE_THROUGH_PROPERTY_KEY) != null) {
            cacheConfig.setWriteThrough(Boolean.valueOf(properties.getProperty(JCACHE_WRITE_THROUGH_PROPERTY_KEY)));
        } else {
            cacheConfig.setWriteThrough(true);
        }
        if (properties.getProperty(JCACHE_STORE_BY_VALUE_PROPERTY_KEY) != null) {
            cacheConfig.setStoreByValue(Boolean.valueOf(properties.getProperty(JCACHE_STORE_BY_VALUE_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_STATISTICS_PROPERTY_KEY) != null) {
            cacheConfig.setStatisticsEnabled(Boolean.valueOf(properties.getProperty(JCACHE_STATISTICS_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_MANAGEMENT_PROPERTY_KEY) != null) {
            cacheConfig.setStatisticsEnabled(Boolean.valueOf(properties.getProperty(JCACHE_MANAGEMENT_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_EVICTION_POLICY_PROPERTY_KEY) != null) {
            cacheConfig.getEvictionConfig().setEvictionPolicy(EvictionPolicy.valueOf(properties.getProperty(JCACHE_EVICTION_POLICY_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_EVICTION_MAX_SIZE_POLICY_PROPERTY_KEY) != null) {
            cacheConfig.getEvictionConfig().setMaximumSizePolicy(EvictionConfig.MaxSizePolicy.valueOf(properties.getProperty(JCACHE_EVICTION_MAX_SIZE_POLICY_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_EVICTION_SIZE_PROPERTY_KEY) != null) {
            cacheConfig.getEvictionConfig().setSize(Integer.valueOf(properties.getProperty(JCACHE_EVICTION_SIZE_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_EXPIRE_POLICY_PROPERTY_KEY) != null) {
            String expiryPolicyIdentifier = properties.getProperty(JCACHE_EXPIRE_POLICY_PROPERTY_KEY);
            if (expiryPolicyIdentifier.equals(JCACHE_ACCESSED_EXPIRY_IDENTIFIER)) {
                cacheConfig.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new AccessedExpiryPolicy(new Duration(TimeUnit.SECONDS, Integer.valueOf(properties.getProperty(JCACHE_EXPIRE_POLICY_DURATION_PROPERTY_KEY))))));
            } else if (expiryPolicyIdentifier.equals(JCACHE_CREATED_EXPIRY_IDENTIFIER)) {
                cacheConfig.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, Integer.valueOf(properties.getProperty(JCACHE_EXPIRE_POLICY_DURATION_PROPERTY_KEY))))));
            } else if (expiryPolicyIdentifier.equals(JCACHE_MODIFIED_EXPIRY_IDENTIFIER)) {
                cacheConfig.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new ModifiedExpiryPolicy(new Duration(TimeUnit.SECONDS, Integer.valueOf(properties.getProperty(JCACHE_EXPIRE_POLICY_DURATION_PROPERTY_KEY))))));
            } else if (expiryPolicyIdentifier.equals(JCACHE_TOUCHED_EXPIRY_IDENTIFIER)) {
                cacheConfig.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new TouchedExpiryPolicy(new Duration(TimeUnit.SECONDS, Integer.valueOf(properties.getProperty(JCACHE_EXPIRE_POLICY_DURATION_PROPERTY_KEY))))));
            }
        }
        if (properties.getProperty(HAZELCAST_CACHE_IN_MEMORY_FORMAT_PROPERTY_KEY) != null) {
            String inMemoryFormat = properties.getProperty(HAZELCAST_CACHE_IN_MEMORY_FORMAT_PROPERTY_KEY);
            if (inMemoryFormat.equals(HAZELCAST_CACHE_BINARY_IN_MEMORY_FORMAT_IDENTIFIER) || inMemoryFormat.equals(HAZELCAST_CACHE_OBJECT_IN_MEMORY_FORMAT_IDENTIFIER) || inMemoryFormat.equals(HAZELCAST_CACHE_NATIVE_IN_MEMORY_FORMAT_IDENTIFIER)) {
                cacheConfig.setInMemoryFormat(InMemoryFormat.valueOf(inMemoryFormat));
            }
        }
        cacheConfig.setCacheLoaderFactory(JCacheCacheFactoryBuilder.factoryOfCacheLoader(this.persistentDataStore, keyClass, persistentClass));
        cacheConfig.setCacheWriterFactory(JCacheCacheFactoryBuilder.factoryOfCacheWriter(this.persistentDataStore, keyClass, persistentClass));
        cache = manager.createCache(persistentClass.getSimpleName(), cacheConfig).unwrap(ICache.class);
    } else {
        cache = manager.getCache(super.getPersistentClass().getSimpleName(), keyClass, persistentClass).unwrap(ICache.class);
        this.populateLocalCacheEntrySet(cache);
    }
    cache.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(JCacheCacheFactoryBuilder.factoryOfEntryListener(new JCacheCacheEntryListener<K, T>(cacheEntryList)), null, true, true));
    LOG.info("JCache Gora datastore initialized successfully.");
}
Also used : CacheEntryListenerConfiguration(javax.cache.configuration.CacheEntryListenerConfiguration) Configuration(org.apache.hadoop.conf.Configuration) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) CacheConfig(com.hazelcast.config.CacheConfig) EvictionConfig(com.hazelcast.config.EvictionConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) ClasspathXmlConfig(com.hazelcast.config.ClasspathXmlConfig) ClasspathXmlConfig(com.hazelcast.config.ClasspathXmlConfig) Duration(javax.cache.expiry.Duration) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) AccessedExpiryPolicy(javax.cache.expiry.AccessedExpiryPolicy) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) Properties(java.util.Properties) URI(java.net.URI) GoraException(org.apache.gora.util.GoraException) XmlClientConfigBuilder(com.hazelcast.client.config.XmlClientConfigBuilder) ICache(com.hazelcast.cache.ICache) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ModifiedExpiryPolicy(javax.cache.expiry.ModifiedExpiryPolicy) ClientConfig(com.hazelcast.client.config.ClientConfig) HazelcastCachingProvider(com.hazelcast.cache.HazelcastCachingProvider) CachingProvider(javax.cache.spi.CachingProvider)

Example 10 with XmlClientConfigBuilder

use of com.hazelcast.client.config.XmlClientConfigBuilder in project bazel by bazelbuild.

the class ConcurrentMapFactory method createHazelcast.

public static ConcurrentMap<String, byte[]> createHazelcast(RemoteOptions options) {
    HazelcastInstance instance;
    if (options.hazelcastClientConfig != null) {
        try {
            ClientConfig config = new XmlClientConfigBuilder(options.hazelcastClientConfig).build();
            instance = HazelcastClient.newHazelcastClient(config);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } else if (options.hazelcastNode != null) {
        // If --hazelcast_node is specified then create a client instance.
        ClientConfig config = new ClientConfig();
        ClientNetworkConfig net = config.getNetworkConfig();
        net.addAddress(options.hazelcastNode.split(","));
        instance = HazelcastClient.newHazelcastClient(config);
    } else if (options.hazelcastStandaloneListenPort != 0) {
        Config config = new Config();
        config.getNetworkConfig().setPort(options.hazelcastStandaloneListenPort).getJoin().getMulticastConfig().setEnabled(false);
        instance = Hazelcast.newHazelcastInstance(config);
    } else {
        // Otherwise create a default instance. This is going to look at
        // -Dhazelcast.config=some-hazelcast.xml for configuration.
        instance = Hazelcast.newHazelcastInstance();
    }
    return instance.getMap(HAZELCAST_CACHE_NAME);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) XmlClientConfigBuilder(com.hazelcast.client.config.XmlClientConfigBuilder) Config(com.hazelcast.config.Config) ClientConfig(com.hazelcast.client.config.ClientConfig) ClientNetworkConfig(com.hazelcast.client.config.ClientNetworkConfig) IOException(java.io.IOException) ClientConfig(com.hazelcast.client.config.ClientConfig) ClientNetworkConfig(com.hazelcast.client.config.ClientNetworkConfig)

Aggregations

XmlClientConfigBuilder (com.hazelcast.client.config.XmlClientConfigBuilder)13 ClientConfig (com.hazelcast.client.config.ClientConfig)10 InputStream (java.io.InputStream)6 ClientNetworkConfig (com.hazelcast.client.config.ClientNetworkConfig)5 DiscoveryConfig (com.hazelcast.config.DiscoveryConfig)4 HazelcastInstance (com.hazelcast.core.HazelcastInstance)4 QuickTest (com.hazelcast.test.annotation.QuickTest)4 Test (org.junit.Test)4 Config (com.hazelcast.config.Config)3 DefaultDiscoveryService (com.hazelcast.spi.discovery.impl.DefaultDiscoveryService)3 DefaultDiscoveryServiceProvider (com.hazelcast.spi.discovery.impl.DefaultDiscoveryServiceProvider)3 DiscoveryService (com.hazelcast.spi.discovery.integration.DiscoveryService)3 DiscoveryServiceProvider (com.hazelcast.spi.discovery.integration.DiscoveryServiceProvider)3 IOException (java.io.IOException)3 AddressTranslator (com.hazelcast.client.connection.AddressTranslator)2 ClientConnectionManagerFactory (com.hazelcast.client.impl.ClientConnectionManagerFactory)2 HazelcastClientInstanceImpl (com.hazelcast.client.impl.HazelcastClientInstanceImpl)2 HazelcastClientProxy (com.hazelcast.client.impl.HazelcastClientProxy)2 Address (com.hazelcast.nio.Address)2 Before (org.junit.Before)2