1 14 package org.jmanage.core.alert; 15 16 import org.jmanage.core.config.AlertSourceConfig; 17 import org.jmanage.core.management.ServerConnection; 18 import org.jmanage.core.management.ServerConnector; 19 import org.jmanage.core.management.ConnectionFailedException; 20 import org.jmanage.core.util.Loggers; 21 22 import java.util.logging.Logger ; 23 import java.util.logging.Level ; 24 import java.io.IOException ; 25 26 31 public abstract class AlertSource { 32 33 private static final Logger logger = Loggers.getLogger(AlertSource.class); 34 35 protected final AlertSourceConfig sourceConfig; 36 protected AlertHandler handler; 37 protected ServerConnection connection; 38 protected String alertId; 39 protected String alertName; 40 private boolean stopThreads = false; 41 42 43 public AlertSource(AlertSourceConfig sourceConfig){ 44 assert sourceConfig != null; 45 this.sourceConfig = sourceConfig; 46 } 47 48 49 private void connectionClosed(){ 50 try { 51 this.unregisterInternal(); 52 } catch (Exception e) { 53 logger.severe("Error unregistering for monitoring. error=" + 54 e.getMessage()); 55 } 56 57 closeConnection(); 58 59 60 new EstablishConnection().start(); 61 } 62 63 private void closeConnection(){ 64 try { 65 connection.close(); 66 } catch (IOException e) { 67 logger.log(Level.WARNING, "Error while closing connection. error: " + 68 e.getMessage()); 69 } 70 } 71 72 private void connectionOpen(){ 73 try { 74 75 registerInternal(); 76 logger.info("Registered for alert: " + alertName + 77 ", application: " + 78 sourceConfig.getApplicationConfig().getName()); 79 } catch (Exception e) { 80 82 logger.log(Level.SEVERE, "Error registering alert: " + alertName, e); 83 84 closeConnection(); 85 return; 86 } 87 88 new ConnectionMonitor().start(); 89 } 90 91 public void register(AlertHandler handler, 92 String alertId, 93 String alertName){ 94 assert this.handler == null; 95 assert connection == null; 96 97 this.handler = handler; 98 this.alertId = alertId; 99 this.alertName = alertName; 100 101 102 new EstablishConnection().start(); 103 } 104 105 public void unregister(){ 106 stopThreads = true; 107 unregisterInternal(); 108 109 110 try { 111 connection.close(); 112 } catch (IOException e) { 113 logger.log(Level.WARNING, "Error while closing connection. error: " + 114 e.getMessage()); 115 } 116 117 logger.info("Un-registered for alert: " + alertName + 118 ", application: " + 119 sourceConfig.getApplicationConfig().getName()); 120 121 connection = null; 122 handler = null; 123 } 124 125 protected abstract void registerInternal(); 126 127 protected abstract void unregisterInternal(); 128 129 private class ConnectionMonitor extends Thread { 130 131 public void run(){ 132 while(!stopThreads){ 133 if(!connection.isOpen()){ 134 135 logger.warning("Connection lost to application: " + 136 sourceConfig.getApplicationConfig().getName()); 137 AlertSource.this.connectionClosed(); 138 return; 139 } 140 try { 141 sleep(500); 142 } catch (InterruptedException e) {} 143 } 144 } 145 } 146 147 private class EstablishConnection extends Thread { 148 149 public void run(){ 150 while(!stopThreads){ 151 152 try { 153 154 connection = ServerConnector.getServerConnection( 155 sourceConfig.getApplicationConfig()); 156 if(connection.isOpen()){ 157 158 logger.info("Established connection to application: " 159 + sourceConfig.getApplicationConfig().getName()); 160 AlertSource.this.connectionOpen(); 161 return; 162 } 163 } catch (Exception e) { 164 logger.info("Failed to establish connection to application: " 165 + sourceConfig.getApplicationConfig().getName() 166 + ". message=" + e.getMessage()); 167 } 168 169 try { 170 sleep(30 * 1000); 171 } catch (InterruptedException e) {} 172 } 173 } 174 } 175 } 176 | Popular Tags |