package coherencetest;
import com.tangosol.net.CacheFactory; import com.tangosol.net.MemberEvent; import com.tangosol.net.MemberListener; import com.tangosol.net.NamedCache; import com.tangosol.util.MapEvent; import com.tangosol.util.MapListener;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.InetAddress;
import java.util.ArrayList; import java.util.List;
class ProxyListenerNotification implements MemberListener { static String IP1 = "192.168.0.150"; String ip; public void memberJoined (MemberEvent e) { System.out.println("====== Member Join "); } public void memberLeaving(MemberEvent e) {} public void memberLeft(MemberEvent e) { System.out.println("************ Member Left"); System.out.println("===================="); // System.out.println(e.toString()); System.out.println(e.getMember()); String processId = e.getMember().getProcessName(); String totalstring = e.getMember().getAddress().getHostName(); String localhostname =""; try { InetAddress addr = InetAddress.getLocalHost(); localhostname=addr.getHostName().toString(); System.out.println("=============*======="+ip); } catch (Exception e1) { System.out.println(e1.getMessage()); } if (totalstring.indexOf(localhostname) != 0) { if (localhostname == "ocp") { ip=IP1; } String shStr; String port = getPort(Integer.parseInt(processId),ip); if ( port =="9099") { shStr = "/home/oracle/Middleware/coherence/bin/proxy-cache.sh"; } else { shStr = "/home/oracle/Middleware/coherence/bin/proxy-cache1.sh"; } Process process; String killStr = "kill -9 "+processId; try { process = Runtime.getRuntime().exec(killStr); System.out.println("kill successful"); process = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",shStr},null,null); } catch(Exception e1) { System.out.println(e1.getMessage()); }
} } public String getPort(int processid,String ip) { Process process; String line=""; try { String shStr = "netstat -nap |grep "+processid +" | grep tcp | grep "+ ip+ ":9"; System.out.println(shStr); process = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",shStr},null,null); InputStreamReader ir = new InputStreamReader(process.getInputStream()); LineNumberReader input = new LineNumberReader(ir); process.waitFor(); line = input.readLine(); /*while ((line = input.readLine()) != null){ strList.add(line); }*/ }catch (Exception e) { System.out.println(e.getMessage()); } int index=line.indexOf(":9"); String port = line.substring(index+1, index+5); System.out.println(" port ="+port); return port; } }
public class ProxyListener {
public static void main(String[] args) { try { NamedCache cache = CacheFactory.getCache("POFSample"); /* cache.addMapListener(new MapListener() { public void entryUpdated(MapEvent arg0) { System.out.println(arg0); } public void entryInserted(MapEvent arg0) { System.out.println(arg0); } public void entryDeleted(MapEvent arg0) { System.out.println(arg0); }}); */ String processId = "6458"; String localhostname =""; try { InetAddress addr = InetAddress.getLocalHost(); localhostname=addr.getHostName().toString(); System.out.println("=============*======="+localhostname); } catch (Exception e1) { System.out.println(e1.getMessage()); } String ip = "192.168.0.150"; String shStr; ProxyListenerNotification pl = new ProxyListenerNotification(); String port = pl.getPort(Integer.parseInt(processId),ip); //cache.getCacheService().addMemberListener(new ProxyListenerNotification()); if ( port.equals("9099")) { System.out.println("1"); shStr = "/home/oracle/Middleware/coherence/bin/proxy-cache.sh"; } else { System.out.println("2"); shStr = "/home/oracle/Middleware/coherence/bin/proxy-cache1.sh"; } Process process; String killStr = "kill -9 "+processId; try { process = Runtime.getRuntime().exec(killStr); System.out.println("kill successful"); process = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",shStr},null,null); } catch(Exception e1) { System.out.println(e1.getMessage()); } while(true){ // Thread.sleep(100000); } } catch (Exception e) { System.out.println(e); } } }
|