Skip to content

Instantly share code, notes, and snippets.

@notz
Forked from gurbuzali/ClientEntryListenerTest
Created July 31, 2013 08:51
Show Gist options
  • Save notz/6120468 to your computer and use it in GitHub Desktop.
Save notz/6120468 to your computer and use it in GitHub Desktop.
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.config.Config;
import com.hazelcast.config.GroupConfig;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.NearCacheConfig;
import com.hazelcast.core.*;
import java.util.concurrent.atomic.AtomicInteger;
public class ClientEntryListenerTest3 {
private static AtomicInteger adds = new AtomicInteger();
public static void main(String[] args) throws InterruptedException {
Config config = new Config();
config.setGroupConfig(new GroupConfig("test", "test"));
config.getNetworkConfig().setPort(6701);
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config);
IMap<Integer, String> map = hazelcastInstance.getMap("test");
HazelcastInstance hazelcastInstance2 = Hazelcast.newHazelcastInstance(config);
ClientConfig clientConfig = new ClientConfig();
clientConfig.addAddress("localhost:6701");
clientConfig.addAddress("localhost:6702");
clientConfig.setGroupConfig(new GroupConfig("test", "test"));
clientConfig.setSmart(false);
clientConfig.setRedoOperation(true);
clientConfig.setConnectionAttemptLimit(100);
clientConfig.setConnectionTimeout(5000);
clientConfig.addNearCacheConfig("*", new NearCacheConfig().setInMemoryFormat(MapConfig.InMemoryFormat.OBJECT));
HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
IMap<Integer, String> mapClient = client.getMap("test");
mapClient.addEntryListener(new EntryListener<Integer, String>() {
public void entryAdded(EntryEvent<Integer, String> event) {
adds.incrementAndGet();
}
public void entryRemoved(EntryEvent<Integer, String> event) {}
public void entryUpdated(EntryEvent<Integer, String> event) {}
public void entryEvicted(EntryEvent<Integer, String> event) {}
}, true);
int i = 0;
while (true) {
if (i++ % 2 == 0) {
hazelcastInstance.getLifecycleService().terminate();
Thread.sleep(8000);
mapClient.get(1);
hazelcastInstance = Hazelcast.newHazelcastInstance(config);
map = hazelcastInstance.getMap("test");
} else {
hazelcastInstance2.getLifecycleService().shutdown();
Thread.sleep(8000);
mapClient.get(1);
hazelcastInstance2 = Hazelcast.newHazelcastInstance(config);
}
map.clear();
Thread.sleep(10000);
adds.set(0);
mapClient.put(1, "1");
map.put(2, "2");
map.put(3, "3");
map.put(4, "4");
map.put(5, "5");
map.put(6, "6");
map.put(7, "7");
Thread.sleep(2000);
if (adds.get() != 7) {
System.out.println("ERROR: only got " + adds + " adds out of 7");
System.exit(0);
} else {
System.out.println("OK: got " + adds + " adds out of 7");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment