package com.rdlze.radializebase.notification; import java.rmi.NotBoundException; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import com.rdlze.radializebase.interfaces.InterfaceNotifiable; import com.rdlze.radializeutils.notification.Notification; public final class Notifier { private static InterfaceNotifiable notifiable; private static InterfaceNotifiable[] notifiables; private static boolean[] needReconnect; private static boolean severalBrokers = false; private static String componentsName; private static String[] ips; private static NeedForReconnectChecker reconnecter = null; int config; public Notifier() { } public static final boolean init(String notificationSubjectIPAddress, String notificationSubjectServiceName, int port) { try { // Se Registra no BrokenServer Registry registry = LocateRegistry .getRegistry(notificationSubjectIPAddress); System.out.println("buggable " + notificationSubjectServiceName + " " + notificationSubjectIPAddress); notifiable = (InterfaceNotifiable) registry .lookup(notificationSubjectServiceName); } catch (RemoteException e) { e.printStackTrace(); } catch (NotBoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return true; } public static final synchronized void notify(Notification n) throws RemoteException { if(severalBrokers) for(int i = 0; i < notifiables.length; i++){ InterfaceNotifiable iN = notifiables[i]; try{ iN.notify(n); }catch(Exception e){ needReconnect[i] = true; e.printStackTrace(); } } else notifiable.notify(n); } public static final synchronized void register(String IP, String name, int port) throws RemoteException { notifiable.registerNotifiable(IP, name, port); } /** * Method to connect to several broker to notify. * @param brokerIp Ips separated by comma * @param notificationSubjectServiceName Name of the component which you are connecting to. * @param shallSplitIps True if is connecting to several brokers. */ public static void init(String brokerIp, String notificationSubjectServiceName, int port, boolean shallSplitIps) { severalBrokers=true; System.out.println("init broker"); ips = brokerIp.split(","); System.out.println("creating notifiables"); notifiables = new InterfaceNotifiable[ips.length]; System.out.println("enter for; split length: "+ ips.length); needReconnect = new boolean[ips.length]; componentsName = notificationSubjectServiceName; for(int i = 0 ; i < ips.length; i++){ try { Thread.sleep(1000); } catch (InterruptedException e1) { e1.printStackTrace(); } connectTo(notificationSubjectServiceName, i); } if(severalBrokers&&reconnecter==null){ reconnecter = new NeedForReconnectChecker(); reconnecter.start(); } } private static void connectTo(String notificationSubjectServiceName, int i) { String ip = ips[i].trim(); try { System.out.println("getting broker registry 2 connect" + notificationSubjectServiceName + " " + ip); // Se Registra no BrokenServer Registry registry = LocateRegistry .getRegistry(ip); notifiables[i] = (InterfaceNotifiable) registry .lookup(notificationSubjectServiceName); needReconnect[i] = false; System.out.println("buggable " + notificationSubjectServiceName + " " + ip); } catch (RemoteException e) { e.printStackTrace(); } catch (NotBoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } private static class NeedForReconnectChecker extends Thread{ public NeedForReconnectChecker(){} public void run(){ while(true){ try { if(needReconnect!=null) System.out.println("Checking need for reconnecting..."); for(int i = 0; i < needReconnect.length; i++){ if(needReconnect[i]){ connectTo(componentsName, i); } } sleep(60000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }