123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- 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();
- }
- }
- }
- }
- }
|