Java Code Examples for com.hazelcast.core.HazelcastInstance#getMap()

The following examples show how to use com.hazelcast.core.HazelcastInstance#getMap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestCustomerSerializers.java    From subzero with Apache License 2.0 6 votes vote down vote up
@Test
public void testGlobalCustomSerializationConfiguredProgrammaticallyForClientConfig() {
    Config memberConfig = new Config();
    SubZero.useAsGlobalSerializer(memberConfig);
    hazelcastFactory.newHazelcastInstance(memberConfig);

    String mapName = randomMapName();
    ClientConfig config = new ClientConfig();

    SubZero.useAsGlobalSerializer(config, MyGlobalUserSerlizationConfig.class);

    HazelcastInstance member = hazelcastFactory.newHazelcastClient(config);
    IMap<Integer, AnotherNonSerializableObject> myMap = member.getMap(mapName);
    myMap.put(0, new AnotherNonSerializableObject());
    AnotherNonSerializableObject fromCache = myMap.get(0);

    assertEquals("deserialized", fromCache.name);
}
 
Example 2
Source File: OperationTest.java    From eventapis with Apache License 2.0 6 votes vote down vote up
@Test
public void testReadWriteExternal() throws Exception {
    HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
    IMap<Object, Object> test = hazelcastInstance.getMap("test");
    test.executeOnKey("123", new EntryProcessor() {
        @Override
        public Object process(Map.Entry entry) {
            entry.setValue("Blabla");
            return entry;
        }

        @Override
        public EntryBackupProcessor getBackupProcessor() {
            return null;
        }
    });

    System.out.println(test.get("123"));


}
 
Example 3
Source File: TestCustomerSerializers.java    From subzero with Apache License 2.0 6 votes vote down vote up
@Test
public void testGlobalCustomDelegateSerializationConfiguredProgrammaticallyForClientConfig() {
    Config memberConfig = new Config();
    SubZero.useAsGlobalSerializer(memberConfig);
    hazelcastFactory.newHazelcastInstance(memberConfig);

    String mapName = randomMapName();
    ClientConfig config = new ClientConfig();

    SubZero.useAsGlobalSerializer(config, MyGlobalDelegateSerlizationConfig.class);

    HazelcastInstance member = hazelcastFactory.newHazelcastClient(config);
    IMap<Integer, AnotherNonSerializableObject> myMap = member.getMap(mapName);
    myMap.put(0, new AnotherNonSerializableObject());
    AnotherNonSerializableObject fromCache = myMap.get(0);

    assertEquals("deserialized", fromCache.name);
}
 
Example 4
Source File: HazelcastScheduler.java    From greycat with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    Config cfg = new Config();
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);
    Map<Integer, String> mapCustomers = instance.getMap("customers");
    mapCustomers.put(1, "Joe");
    mapCustomers.put(2, "Ali");
    mapCustomers.put(3, "Avi");

    System.out.println("Customer with key 1: "+ mapCustomers.get(1));
    System.out.println("Map Size:" + mapCustomers.size());

    Queue<String> queueCustomers = instance.getQueue("customers");
    queueCustomers.offer("Tom");
    queueCustomers.offer("Mary");
    queueCustomers.offer("Jane");
    System.out.println("First customer: " + queueCustomers.poll());
    System.out.println("Second customer: "+ queueCustomers.peek());
    System.out.println("Queue size: " + queueCustomers.size());
}
 
Example 5
Source File: TimeToLiveExample.java    From chuidiang-ejemplos with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void main( String[] args ) throws FileNotFoundException, InterruptedException
{
   Config config = new Config();
   HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config);
   
   IMap<String,String> map = hazelcastInstance.getMap("TimeToLiveMap");
   
   map.put("hola", "tu", 10, TimeUnit.SECONDS);
   
   while (true){
      Thread.sleep(1000);
      String dato =map.get("hola");
      if (null!=dato){
         System.out.println(dato);
      } else {
         break;
      }
   }
   System.out.println("Data expired");
}
 
Example 6
Source File: ClassLoadingTest.java    From subzero with Apache License 2.0 5 votes vote down vote up
@Test
public void givenMemberHasClassLoaderConfigured_whenObjectIsStored_thenClassLoaderWillBeUsed() throws Exception {
    String mapName = randomMapName();
    Config config = new Config();
    SubZero.useAsGlobalSerializer(config);
    ClassLoader spyingClassLoader = createSpyingClassLoader();
    config.setClassLoader(spyingClassLoader);
    config.addMapConfig(new MapConfig(mapName).setInMemoryFormat(OBJECT));
    HazelcastInstance member = hazelcastFactory.newHazelcastInstance(config);
    IMap<Integer, Object> myMap = member.getMap(mapName);

    myMap.put(0, new MyClass());

    verify(spyingClassLoader).loadClass("info.jerrinot.subzero.ClassLoadingTest$MyClass");
}
 
Example 7
Source File: TopicManager.java    From mercury with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Map<String, String> getTopic(String topic) throws IOException {
    if (topicExists(topic)) {
        String nodes = HazelcastSetup.NAMESPACE+NODES;
        HazelcastInstance client = HazelcastSetup.getHazelcastClient();
        IMap<String, byte[]> map = client.getMap(nodes);
        return (Map<String, String>) msgPack.unpack(map.get(topic));
    } else {
        return null;
    }
}
 
Example 8
Source File: InterceptorDemo.java    From hazelcast-demo with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	HazelcastInstance ins = Hazelcast.newHazelcastInstance();
	IMap<Integer, String> imap = ins.getMap("");
	imap.addInterceptor(new InterceptorExample());// 添加拦截器
	imap.put(1, "Mei");
	imap.put(1, "Tracer");
	imap.put(1, "D.va");
	imap.put(1, "Mercy");
	
	imap.get(1);
	imap.remove(1);
	System.out.println(imap.get(1));
}
 
Example 9
Source File: BackupExpirationMapTest.java    From hazelcast-simulator with Apache License 2.0 5 votes vote down vote up
private static long totalEntryCountOnNode(String name, HazelcastInstance instance) {
    IMap map = instance.getMap(name);
    LocalMapStats localMapStats = map.getLocalMapStats();
    long ownedEntryCount = localMapStats.getOwnedEntryCount();
    long backupEntryCount = localMapStats.getBackupEntryCount();
    return ownedEntryCount + backupEntryCount;
}
 
Example 10
Source File: HazelcastGetStartServerSlave.java    From hazelcast-demo with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	//创建一个 hazelcastInstance实例
	HazelcastInstance instance = Hazelcast.newHazelcastInstance();
	Map<Integer, String> clusterMap = instance.getMap("MyMap");
	Queue<String> clusterQueue = instance.getQueue("MyQueue");
	
	System.out.println("Map Value:" + clusterMap.get(1));
	System.out.println("Queue Size :" + clusterQueue.size());
	System.out.println("Queue Value 1:" + clusterQueue.poll());
	System.out.println("Queue Value 2:" + clusterQueue.poll());
	System.out.println("Queue Size :" + clusterQueue.size());
}
 
Example 11
Source File: TopicManager.java    From mercury with Apache License 2.0 5 votes vote down vote up
private void deleteTopic(String topic) {
    if (topicExists(topic)) {
        String nodes = HazelcastSetup.NAMESPACE+NODES;
        String realTopic = HazelcastSetup.NAMESPACE+topic;
        HazelcastInstance client = HazelcastSetup.getHazelcastClient();
        // remove topic from node list
        IMap<String, byte[]> map = client.getMap(nodes);
        map.remove(topic);
        // destroy topic from cluster
        ITopic<byte[]> iTopic = client.getReliableTopic(realTopic);
        iTopic.destroy();
        log.info("Topic {} deleted", topic);
    }
}
 
Example 12
Source File: MapStoreExampleMain.java    From hazelcast-demo with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws FileNotFoundException {
	//加载配置
	Config config = new ClasspathXmlConfig("org/palm/hazelcast/map/store/mapStoreConfig.xml");
	//创建Hazelcast实力
	HazelcastInstance ins = Hazelcast.newHazelcastInstance(config);
	//获取Map
	Map<Integer, String> map = ins.getMap("demo");
	println(map.get(1));//输出第一条数据
	
	map.put(11, "Moonbrook");//添加一条数据
	println(map.get(11));//输出第一条数据
	
	map.remove(11);//移除添加的数据
	println(map.get(11));//输出被移除的数据
}
 
Example 13
Source File: SubscriptionMain.java    From chuidiang-ejemplos with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void main(String[] args){
    Config config = new Config();
    GlobalSerializerConfig globalConfig = new GlobalSerializerConfig();
    globalConfig.setOverrideJavaSerialization(true).setImplementation(new DataSerializer());

    config.getSerializationConfig().setGlobalSerializerConfig(globalConfig);

   HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);

   IMap<String, String> aMap = instance.getMap("aMap");

   aMap.addEntryListener(new MyListener(), true);

   
   aMap.put("key", "value");
   aMap.put("key", "Another value");
   aMap.remove("key");
   aMap.put("other key", "other value");
   aMap.clear();


   IMap<String, Data> otherMap = instance.getMap("otherMap");

   otherMap.addEntryListener(new MyListener(), true);
   otherMap.put("key", new Data());
   Data data = otherMap.get("key");
   data.date=new Date();
   data.value=1000;
   otherMap.put("key",data);

   instance.shutdown();
}
 
Example 14
Source File: TestCustomerSerializers.java    From subzero with Apache License 2.0 5 votes vote down vote up
@Test
public void testGlobalCustomSerializer_SpecialRegistrationRegisteredInDefaultConfigFile() {
    String mapName = randomMapName();
    Config config = new Config();

    SubZero.useAsGlobalSerializer(config);
    HazelcastInstance member = hazelcastFactory.newHazelcastInstance(config);
    IMap<Integer, ClassWithUnmodifieableList> myMap = member.getMap(mapName);
    myMap.put(0, new ClassWithUnmodifieableList("foo"));

    //does not throw an exception
    myMap.get(0);
}
 
Example 15
Source File: TestCustomerSerializers.java    From subzero with Apache License 2.0 5 votes vote down vote up
@Test
public void testTypedCustomSerializerRegisteredInDefaultConfigFile() throws Exception {
    String mapName = randomMapName();
    Config config = new Config();
    SubZero.useForClasses(config, NonSerializableObjectRegisteredInDefaultConfigFile.class);
    HazelcastInstance member = hazelcastFactory.newHazelcastInstance(config);
    IMap<Integer, NonSerializableObjectRegisteredInDefaultConfigFile> myMap = member.getMap(mapName);

    myMap.put(0, new NonSerializableObjectRegisteredInDefaultConfigFile());
    NonSerializableObjectRegisteredInDefaultConfigFile fromCache = myMap.get(0);

    assertEquals("deserialized", fromCache.name);
}
 
Example 16
Source File: HazelcastConfigSimple.java    From hazelcast-demo with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	// 从classpath加载配置文件
	Config config = new ClasspathXmlConfig("xmlconfig/simple-config.xml");
	// 获取网络配置
	NetworkConfig netConfig = config.getNetworkConfig();
	// 获取用户定义的map配置
	MapConfig mapConfigXml = config.getMapConfig("demo.config");
	// 获取系统默认的map配置
	MapConfig mapConfigDefault = config.getMapConfig("default");
	// 输出集群监听的起始端口号
	System.out.println("Current port:" + netConfig.getPort());
	// 输出监听端口的累加号
	System.out.println("Current port count:" + netConfig.getPortCount());
	// 输出自定义map的备份副本个数
	System.out.println("Config map backup count:" + mapConfigXml.getBackupCount());
	// 输出默认map的备份副本个数
	System.out.println("Default map backup count:" + mapConfigDefault.getBackupCount());

	// 测试创建Hazelcast实例并读写测试数据
	HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(config);
	HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(config);

	Map<Integer, String> defaultMap1 = instance1.getMap("defaultMap");
	defaultMap1.put(1, "testMap");
	Map<Integer, String> configMap1 = instance1.getMap("configMap");
	configMap1.put(1, "configMap");

	Map<Integer, String> testMap2 = instance2.getMap("defaultMap");
	System.out.println("Default map value:" + testMap2.get(1));
	Map<Integer, String> configMap2 = instance2.getMap("configMap");
	System.out.println("Config map value:" + configMap2.get(1));
}
 
Example 17
Source File: TopicManager.java    From mercury with Apache License 2.0 4 votes vote down vote up
private boolean topicExists(String topic) {
    String nodes = HazelcastSetup.NAMESPACE+NODES;
    HazelcastInstance client = HazelcastSetup.getHazelcastClient();
    IMap<String, byte[]> map = client.getMap(nodes);
    return map.containsKey(topic);
}
 
Example 18
Source File: HazelcastEventStoreTest.java    From spring-boot-admin with Apache License 2.0 4 votes vote down vote up
@Override
protected InstanceEventStore createStore(int maxLogSizePerAggregate) {
	HazelcastInstance hazelcast = new TestHazelcastInstanceFactory(1).newHazelcastInstance();
	return new HazelcastEventStore(maxLogSizePerAggregate, hazelcast.getMap("testList"));
}
 
Example 19
Source File: DistributedLockFactory.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
public static IMap getDistributedMap(String instanceName, String mapName) {
	logger.debug("Getting Hazelcast map with name [" + mapName + "]");
	HazelcastInstance hz = getHazelcastInstance(instanceName);
	return hz.getMap(mapName);
}
 
Example 20
Source File: AdminServerHazelcastAutoConfiguration.java    From Moss with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean(InstanceEventStore.class)
public HazelcastEventStore eventStore(HazelcastInstance hazelcastInstance) {
    IMap<InstanceId, List<InstanceEvent>> map = hazelcastInstance.getMap(mapName);
    return new HazelcastEventStore(map);
}