Created
April 9, 2015 13:22
-
-
Save gurbuzali/9745003e52aff64df19d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package compile; | |
| import com.hazelcast.config.Config; | |
| import com.hazelcast.core.EntryAdapter; | |
| import com.hazelcast.core.EntryEvent; | |
| import com.hazelcast.core.EntryListener; | |
| import com.hazelcast.core.Hazelcast; | |
| import com.hazelcast.core.HazelcastInstance; | |
| import com.hazelcast.core.IMap; | |
| import com.hazelcast.core.MapEvent; | |
| import java.io.File; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.util.Random; | |
| public class Main { | |
| public static void main(String[] args) throws Exception { | |
| Config config = new Config(); | |
| config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); | |
| config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true); | |
| config.getNetworkConfig().getJoin().getTcpIpConfig().addMember("192.168.1.6:5701"); | |
| config.getNetworkConfig().getJoin().getTcpIpConfig().addMember("192.168.1.6:5702"); | |
| HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(config); | |
| HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(config); | |
| IMap<Object, Object> map1 = instance1.getMap("map"); | |
| IMap<Object, Object> map2 = instance2.getMap("map"); | |
| map1.addEntryListener(new EntryAdapter<Object, Object>(), true); | |
| map2.addEntryListener(new EntryAdapter<Object, Object>(), true); | |
| MyThread thread1 = new MyThread(map1, "ins1"); | |
| thread1.start(); | |
| MyThread thread2 = new MyThread(map2, "ins2"); | |
| thread2.start(); | |
| sleepSec(3); | |
| blockPort("en0", "5701"); | |
| blockPort("en0", "5702"); | |
| blockPort("lo0", "5701"); | |
| blockPort("lo0", "5702"); | |
| sleepSec(6); | |
| thread2.running = false; | |
| thread2.join(); | |
| sleepSec(1); | |
| System.out.println("fatal restart"); | |
| instance2.getLifecycleService().terminate(); | |
| instance2 = Hazelcast.newHazelcastInstance(config); | |
| map2 = instance2.getMap("map"); | |
| thread2 = new MyThread(map2, "ins2-new"); | |
| thread2.start(); | |
| sleepSec(1); | |
| clearBlocked(); | |
| sleepSec(120); | |
| } | |
| public static void maid(String[] args) throws Exception { | |
| listBlocked(); | |
| blockPort("en0", "5701"); | |
| listBlocked(); | |
| blockPort("en0", "5702"); | |
| listBlocked(); | |
| unblockPort("en0", "5701"); | |
| listBlocked(); | |
| clearBlocked(); | |
| listBlocked(); | |
| } | |
| static void listBlocked() throws IOException { | |
| String[] command = {"sh", "list-blocked.sh"}; | |
| ProcessBuilder proBuilder = new ProcessBuilder(command).redirectErrorStream(true); | |
| proBuilder.directory(new File("/Users/ali/bin")); | |
| Process process = proBuilder.start(); | |
| printStream(process); | |
| } | |
| static void clearBlocked() throws IOException { | |
| System.out.println("fatal clearing all blocked ports"); | |
| String[] command = {"sh", "clear-blocked.sh"}; | |
| ProcessBuilder proBuilder = new ProcessBuilder(command).redirectErrorStream(true); | |
| proBuilder.directory(new File("/Users/ali/bin")); | |
| Process process = proBuilder.start(); | |
| printStream(process); | |
| } | |
| static void blockPort(String networkInterface, String port) throws IOException { | |
| System.out.println("fatal blocking port : " + port + " of " + networkInterface + " interface"); | |
| String[] command = {"sh", "block-port.sh", networkInterface, port}; | |
| ProcessBuilder proBuilder = new ProcessBuilder(command).redirectErrorStream(true); | |
| proBuilder.directory(new File("/Users/ali/bin")); | |
| Process process = proBuilder.start(); | |
| printStream(process); | |
| } | |
| static void unblockPort(String networkInterface, String port) throws IOException { | |
| String[] command = {"sh", "unblock-port.sh", networkInterface, port}; | |
| ProcessBuilder proBuilder = new ProcessBuilder(command).redirectErrorStream(true); | |
| proBuilder.directory(new File("/Users/ali/bin")); | |
| Process process = proBuilder.start(); | |
| printStream(process); | |
| } | |
| static void printStream(Process process) throws IOException { | |
| InputStream in = process.getInputStream(); | |
| byte[] data = new byte[1024]; | |
| int len; | |
| while ((len = in.read(data)) != -1) { | |
| String s = new String(data, 0, len); | |
| System.out.print(s); | |
| } | |
| System.out.println(""); | |
| } | |
| static void sleepSec(int sec){ | |
| try { | |
| Thread.sleep(sec * 1000); | |
| } catch (InterruptedException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| static class MyThread extends Thread { | |
| String instanceName; | |
| IMap map; | |
| volatile boolean running = true; | |
| public MyThread(IMap map, String instanceName) { | |
| this.map = map; | |
| this.instanceName = instanceName; | |
| } | |
| @Override | |
| public void run() { | |
| int count = new Random().nextInt(); | |
| while (running) { | |
| sleepSec(1); | |
| map.putAsync(count, count); | |
| System.out.println(instanceName + " -------> " + count++); | |
| } | |
| System.out.println(instanceName + " -------> done !!!!"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment