Sin descripción

4b1188e49c0eb8a59795eaf3dac36b9dd23f1254.svn-base 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.rdlze.radializebase.notification;
  2. import java.rmi.NotBoundException;
  3. import java.rmi.RemoteException;
  4. import java.rmi.registry.LocateRegistry;
  5. import java.rmi.registry.Registry;
  6. import com.rdlze.radializebase.interfaces.InterfaceNotifiable;
  7. import com.rdlze.radializeutils.notification.Notification;
  8. public final class Notifier {
  9. private static InterfaceNotifiable notifiable;
  10. private static InterfaceNotifiable[] notifiables;
  11. private static boolean[] needReconnect;
  12. private static boolean severalBrokers = false;
  13. private static String componentsName;
  14. private static String[] ips;
  15. private static NeedForReconnectChecker reconnecter = null;
  16. int config;
  17. public Notifier() {
  18. }
  19. public static final boolean init(String notificationSubjectIPAddress,
  20. String notificationSubjectServiceName, int port) {
  21. try {
  22. // Se Registra no BrokenServer
  23. Registry registry = LocateRegistry
  24. .getRegistry(notificationSubjectIPAddress);
  25. System.out.println("buggable " + notificationSubjectServiceName
  26. + " " + notificationSubjectIPAddress);
  27. notifiable = (InterfaceNotifiable) registry
  28. .lookup(notificationSubjectServiceName);
  29. } catch (RemoteException e) {
  30. e.printStackTrace();
  31. } catch (NotBoundException e) {
  32. e.printStackTrace();
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36. return true;
  37. }
  38. public static final synchronized void notify(Notification n)
  39. throws RemoteException {
  40. if(severalBrokers)
  41. for(int i = 0; i < notifiables.length; i++){
  42. InterfaceNotifiable iN = notifiables[i];
  43. try{
  44. iN.notify(n);
  45. }catch(Exception e){
  46. needReconnect[i] = true;
  47. e.printStackTrace();
  48. }
  49. }
  50. else
  51. notifiable.notify(n);
  52. }
  53. public static final synchronized void register(String IP, String name, int port)
  54. throws RemoteException {
  55. notifiable.registerNotifiable(IP, name, port);
  56. }
  57. /**
  58. * Method to connect to several broker to notify.
  59. * @param brokerIp Ips separated by comma
  60. * @param notificationSubjectServiceName Name of the component which you are connecting to.
  61. * @param shallSplitIps True if is connecting to several brokers.
  62. */
  63. public static void init(String brokerIp, String notificationSubjectServiceName, int port,
  64. boolean shallSplitIps) {
  65. severalBrokers=true;
  66. System.out.println("init broker");
  67. ips = brokerIp.split(",");
  68. System.out.println("creating notifiables");
  69. notifiables = new InterfaceNotifiable[ips.length];
  70. System.out.println("enter for; split length: "+ ips.length);
  71. needReconnect = new boolean[ips.length];
  72. componentsName = notificationSubjectServiceName;
  73. for(int i = 0 ; i < ips.length; i++){
  74. try {
  75. Thread.sleep(1000);
  76. } catch (InterruptedException e1) {
  77. e1.printStackTrace();
  78. }
  79. connectTo(notificationSubjectServiceName, i);
  80. }
  81. if(severalBrokers&&reconnecter==null){
  82. reconnecter = new NeedForReconnectChecker();
  83. reconnecter.start();
  84. }
  85. }
  86. private static void connectTo(String notificationSubjectServiceName, int i) {
  87. String ip = ips[i].trim();
  88. try {
  89. System.out.println("getting broker registry 2 connect" + notificationSubjectServiceName
  90. + " " + ip);
  91. // Se Registra no BrokenServer
  92. Registry registry = LocateRegistry
  93. .getRegistry(ip);
  94. notifiables[i] = (InterfaceNotifiable) registry
  95. .lookup(notificationSubjectServiceName);
  96. needReconnect[i] = false;
  97. System.out.println("buggable " + notificationSubjectServiceName
  98. + " " + ip);
  99. } catch (RemoteException e) {
  100. e.printStackTrace();
  101. } catch (NotBoundException e) {
  102. e.printStackTrace();
  103. } catch (Exception e) {
  104. e.printStackTrace();
  105. }
  106. }
  107. private static class NeedForReconnectChecker extends Thread{
  108. public NeedForReconnectChecker(){}
  109. public void run(){
  110. while(true){
  111. try {
  112. if(needReconnect!=null)
  113. System.out.println("Checking need for reconnecting...");
  114. for(int i = 0; i < needReconnect.length; i++){
  115. if(needReconnect[i]){
  116. connectTo(componentsName, i);
  117. }
  118. }
  119. sleep(60000);
  120. } catch (InterruptedException e) {
  121. // TODO Auto-generated catch block
  122. e.printStackTrace();
  123. }
  124. }
  125. }
  126. }
  127. }