mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
Fix for Issue 4 - Allow programmatic shutdown of EbeanServer
This commit is contained in:
1 parent
384931d92e
commit
bcce1a7148
31 files changed
+1397
-2073
No files matched your search
+4
-4
@@ -8,20 +8,20 @@ package com.avaje.ebeaninternal.server.lib.sql;
|
||||
* when these events occur on the DataSource.
|
||||
* </p>
|
||||
*/
|
||||
public interface DataSourceNotify {
|
||||
public interface DataSourceAlert {
|
||||
|
||||
/**
|
||||
* Send an alert to say the dataSource is back up.
|
||||
*/
|
||||
public void notifyDataSourceUp(String dataSourceName);
|
||||
public void dataSourceUp(String dataSourceName);
|
||||
|
||||
/**
|
||||
* Send an alert to say the dataSource is down.
|
||||
*/
|
||||
public void notifyDataSourceDown(String dataSourceName);
|
||||
public void dataSourceDown(String dataSourceName);
|
||||
|
||||
/**
|
||||
* Send an alert to say the dataSource is getting close to its max size.
|
||||
*/
|
||||
public void notifyWarning(String subject, String msg);
|
||||
public void dataSourceWarning(String subject, String msg);
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.lib.sql;
|
||||
|
||||
|
||||
/**
|
||||
* Listens for alerting events such as DataSource down.
|
||||
*/
|
||||
public interface DataSourceAlertListener {
|
||||
|
||||
/**
|
||||
* Send an Alert saying the dataSource is down.
|
||||
*/
|
||||
public void dataSourceDown(String dataSourceName);
|
||||
|
||||
/**
|
||||
* Send an Alert saying the dataSource is back up.
|
||||
*/
|
||||
public void dataSourceUp(String dataSourceName);
|
||||
|
||||
/**
|
||||
* Send an Alert saying the dataSource has reached a high
|
||||
* number of connections.
|
||||
*/
|
||||
public void warning(String subject, String msg);
|
||||
|
||||
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.lib.sql;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.avaje.ebean.config.DataSourceConfig;
|
||||
|
||||
/**
|
||||
* Manages access to named DataSources using singleton scope.
|
||||
*/
|
||||
public final class DataSourceGlobalManager {
|
||||
|
||||
private static final DataSourceManager manager = new DataSourceManager();
|
||||
|
||||
private DataSourceGlobalManager() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true when the dataSource is shutting down.
|
||||
*/
|
||||
public static boolean isShuttingDown() {
|
||||
return manager.isShuttingDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown the dataSources.
|
||||
*/
|
||||
public static void shutdown() {
|
||||
manager.shutdown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the list of DataSourcePool's.
|
||||
*/
|
||||
public static List<DataSourcePool> getPools() {
|
||||
return manager.getPools();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a DataSource pool by its name.
|
||||
*/
|
||||
public static DataSourcePool getDataSource(String name) {
|
||||
return manager.getDataSource(name);
|
||||
}
|
||||
|
||||
public static DataSourcePool getDataSource(String name, DataSourceConfig dsConfig) {
|
||||
return manager.getDataSource(name, dsConfig);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,233 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.lib.sql;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import com.avaje.ebean.config.DataSourceConfig;
|
||||
import com.avaje.ebean.config.GlobalProperties;
|
||||
import com.avaje.ebeaninternal.api.ClassUtil;
|
||||
import com.avaje.ebeaninternal.server.lib.BackgroundRunnable;
|
||||
import com.avaje.ebeaninternal.server.lib.BackgroundThread;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
/**
|
||||
* Manages access to named DataSources.
|
||||
*/
|
||||
public class DataSourceManager implements DataSourceNotify {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(DataSourceManager.class);
|
||||
|
||||
/**
|
||||
* An alerter that notifies when the database has problems.
|
||||
*/
|
||||
private final DataSourceAlertListener alertlistener;
|
||||
|
||||
/**
|
||||
* Cache of the named DataSources.
|
||||
*/
|
||||
private final Hashtable<String,DataSourcePool> dsMap = new Hashtable<String, DataSourcePool>();
|
||||
|
||||
/**
|
||||
* Monitor for creating dataSources.
|
||||
*/
|
||||
private final Object monitor = new Object();
|
||||
|
||||
/**
|
||||
* The database checker registered with BackgroundThread.
|
||||
*/
|
||||
private final BackgroundRunnable dbChecker;
|
||||
|
||||
/**
|
||||
* The frequency to test db while it is up.
|
||||
*/
|
||||
private final int dbUpFreqInSecs;
|
||||
|
||||
/**
|
||||
* The frequency to test db while it is down.
|
||||
*/
|
||||
private final int dbDownFreqInSecs;
|
||||
|
||||
/**
|
||||
* Set to true when shutting down.
|
||||
*/
|
||||
private boolean shuttingDown;
|
||||
|
||||
private boolean deregisterDriver;
|
||||
|
||||
/**
|
||||
* Construct with explicit ConfigProperties.
|
||||
*/
|
||||
public DataSourceManager() {
|
||||
|
||||
this.alertlistener = createAlertListener();
|
||||
|
||||
// perform heart beat every 30 seconds by default
|
||||
this.dbUpFreqInSecs = GlobalProperties.getInt("datasource.heartbeatfreq",30);
|
||||
this.dbDownFreqInSecs = GlobalProperties.getInt("datasource.deadbeatfreq",10);
|
||||
this.dbChecker = new BackgroundRunnable(new Checker(), dbUpFreqInSecs);
|
||||
this.deregisterDriver = GlobalProperties.getBoolean("datasource.deregisterDriver", true);
|
||||
|
||||
try {
|
||||
BackgroundThread.add(dbChecker);
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error(null, e);
|
||||
}
|
||||
}
|
||||
|
||||
private DataSourceAlertListener createAlertListener() throws DataSourceException {
|
||||
|
||||
String alertCN = GlobalProperties.get("datasource.alert.class", null);
|
||||
if (alertCN == null){
|
||||
return new SimpleAlerter();
|
||||
|
||||
} else {
|
||||
try {
|
||||
return (DataSourceAlertListener)ClassUtil.newInstance(alertCN, this.getClass());
|
||||
|
||||
} catch (Exception ex){
|
||||
throw new DataSourceException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an alert to say the dataSource is back up.
|
||||
*/
|
||||
public void notifyDataSourceUp(String dataSourceName){
|
||||
|
||||
dbChecker.setFreqInSecs(dbUpFreqInSecs);
|
||||
|
||||
if (alertlistener != null){
|
||||
alertlistener.dataSourceUp(dataSourceName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an alert to say the dataSource is down.
|
||||
*/
|
||||
public void notifyDataSourceDown(String dataSourceName){
|
||||
|
||||
dbChecker.setFreqInSecs(dbDownFreqInSecs);
|
||||
|
||||
if (alertlistener != null){
|
||||
alertlistener.dataSourceDown(dataSourceName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an alert to say the dataSource is getting close to its max size.
|
||||
*/
|
||||
public void notifyWarning(String subject, String msg){
|
||||
if (alertlistener != null){
|
||||
alertlistener.warning(subject, msg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true when the dataSource is shutting down.
|
||||
*/
|
||||
public boolean isShuttingDown() {
|
||||
synchronized(monitor) {
|
||||
return shuttingDown;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown the dataSources.
|
||||
*/
|
||||
public void shutdown() {
|
||||
|
||||
synchronized(monitor) {
|
||||
|
||||
this.shuttingDown = true;
|
||||
|
||||
Collection<DataSourcePool> values = dsMap.values();
|
||||
for (DataSourcePool ds : values) {
|
||||
try {
|
||||
ds.shutdown();
|
||||
} catch (DataSourceException e) {
|
||||
// should never be thrown as the DataSources are all created...
|
||||
logger.error(null, e);
|
||||
}
|
||||
}
|
||||
if (deregisterDriver){
|
||||
for (DataSourcePool ds : values) {
|
||||
ds.deregisterDriver();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the DataSourcePool's.
|
||||
*/
|
||||
public List<DataSourcePool> getPools() {
|
||||
synchronized(monitor) {
|
||||
// create a copy of the DataSourcePool's
|
||||
ArrayList<DataSourcePool> list = new ArrayList<DataSourcePool>();
|
||||
list.addAll(dsMap.values());
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the dataSource using the default ConfigProperties.
|
||||
*/
|
||||
public DataSourcePool getDataSource(String name) {
|
||||
return getDataSource(name, null);
|
||||
}
|
||||
|
||||
|
||||
public DataSourcePool getDataSource(String name, DataSourceConfig dsConfig){
|
||||
|
||||
if (name == null){
|
||||
throw new IllegalArgumentException("name not defined");
|
||||
}
|
||||
|
||||
synchronized(monitor){
|
||||
DataSourcePool pool = dsMap.get(name);
|
||||
if (pool == null){
|
||||
if (dsConfig == null){
|
||||
dsConfig = new DataSourceConfig();
|
||||
dsConfig.loadSettings(name);
|
||||
}
|
||||
pool = new DataSourcePool(this, name, dsConfig);
|
||||
dsMap.put(name, pool);
|
||||
}
|
||||
return pool;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the database is up by performing a simple query. This should
|
||||
* be done periodically. By default every 30 seconds.
|
||||
*/
|
||||
private void checkDataSource() {
|
||||
|
||||
synchronized (monitor) {
|
||||
if (!isShuttingDown()) {
|
||||
Iterator<DataSourcePool> it = dsMap.values().iterator();
|
||||
while (it.hasNext()) {
|
||||
DataSourcePool ds = it.next();
|
||||
ds.checkDataSource();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs every dbUpFreqInSecs to make sure dataSource is up.
|
||||
*/
|
||||
private final class Checker implements Runnable {
|
||||
|
||||
public void run() {
|
||||
checkDataSource();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,7 +46,7 @@ public class DataSourcePool implements DataSource {
|
||||
/**
|
||||
* Used to notify of changes to the DataSource status.
|
||||
*/
|
||||
private final DataSourceNotify notify;
|
||||
private final DataSourceAlert notify;
|
||||
|
||||
/**
|
||||
* Optional listener that can be notified when connections are got from and
|
||||
@@ -73,6 +73,8 @@ public class DataSourcePool implements DataSource {
|
||||
* The sql used to test a connection.
|
||||
*/
|
||||
private final String heartbeatsql;
|
||||
|
||||
private final int heartbeatFreqSecs;
|
||||
|
||||
/**
|
||||
* The transaction isolation level as per java.sql.Connection.
|
||||
@@ -154,7 +156,9 @@ public class DataSourcePool implements DataSource {
|
||||
*/
|
||||
private long leakTimeMinutes;
|
||||
|
||||
public DataSourcePool(DataSourceNotify notify, String name, DataSourceConfig params) {
|
||||
private final Runnable heartbeatRunnable = new HeartBeatRunnable();
|
||||
|
||||
public DataSourcePool(DataSourceAlert notify, String name, DataSourceConfig params) {
|
||||
|
||||
this.notify = notify;
|
||||
this.name = name;
|
||||
@@ -175,7 +179,8 @@ public class DataSourcePool implements DataSource {
|
||||
this.maxConnections = params.getMaxConnections();
|
||||
this.waitTimeoutMillis = params.getWaitTimeoutMillis();
|
||||
this.heartbeatsql = params.getHeartbeatSql();
|
||||
|
||||
this.heartbeatFreqSecs = params.getHeartbeatFreqSecs();
|
||||
|
||||
queue = new PooledConnectionQueue(this);
|
||||
|
||||
String un = params.getUsername();
|
||||
@@ -204,7 +209,15 @@ public class DataSourcePool implements DataSource {
|
||||
throw new DataSourceException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
class HeartBeatRunnable implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
checkDataSource();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
|
||||
throw new SQLFeatureNotSupportedException("We do not support java.util.logging");
|
||||
@@ -296,7 +309,7 @@ public class DataSourcePool implements DataSource {
|
||||
logger.warn(msg);
|
||||
if (notify != null) {
|
||||
String subject = "DataSourcePool [" + name + "] warning";
|
||||
notify.notifyWarning(subject, msg);
|
||||
notify.dataSourceWarning(subject, msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -304,10 +317,9 @@ public class DataSourcePool implements DataSource {
|
||||
private void notifyDataSourceIsDown(SQLException ex) {
|
||||
|
||||
if (!dataSourceDownAlertSent) {
|
||||
String msg = "FATAL: DataSourcePool [" + name + "] is down!!!";
|
||||
logger.error(msg, ex);
|
||||
logger.error("FATAL: DataSourcePool [" + name + "] is down!!!", ex);
|
||||
if (notify != null) {
|
||||
notify.notifyDataSourceDown(name);
|
||||
notify.dataSourceDown(name);
|
||||
}
|
||||
dataSourceDownAlertSent = true;
|
||||
|
||||
@@ -320,15 +332,14 @@ public class DataSourcePool implements DataSource {
|
||||
|
||||
private void notifyDataSourceIsUp() {
|
||||
if (dataSourceDownAlertSent) {
|
||||
String msg = "RESOLVED FATAL: DataSourcePool [" + name + "] is back up!";
|
||||
logger.error(msg);
|
||||
logger.error("RESOLVED FATAL: DataSourcePool [" + name + "] is back up!");
|
||||
if (notify != null) {
|
||||
notify.notifyDataSourceUp(name);
|
||||
notify.dataSourceUp(name);
|
||||
}
|
||||
dataSourceDownAlertSent = false;
|
||||
|
||||
} else if (!dataSourceUp) {
|
||||
logger.warn("DataSourcePool [" + name + "] is back up!");
|
||||
logger.info("DataSourcePool [" + name + "] is back up!");
|
||||
}
|
||||
|
||||
if (!dataSourceUp) {
|
||||
@@ -336,14 +347,36 @@ public class DataSourcePool implements DataSource {
|
||||
reset();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the heartbeat frequency in seconds.
|
||||
* <p>
|
||||
* This is the frequency that the heartbeat runnable should be run.
|
||||
* </p>
|
||||
*/
|
||||
public int getHeartbeatFreqSecs() {
|
||||
return heartbeatFreqSecs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Runnable used to check the dataSource using a heartbeat query.
|
||||
*/
|
||||
public Runnable getHeartbeatRunnable() {
|
||||
return heartbeatRunnable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the dataSource is up. Trim connections.
|
||||
* <p>
|
||||
* This is called by the HeartbeatRunnable which should be scheduled to
|
||||
* run periodically (every heartbeatFreqSecs seconds actually).
|
||||
* </p>
|
||||
*/
|
||||
protected void checkDataSource() {
|
||||
public void checkDataSource() {
|
||||
Connection conn = null;
|
||||
try {
|
||||
// test to see if we can create a new connection...
|
||||
// Get a connection from the pool and test it
|
||||
conn = getConnection();
|
||||
testConnection(conn);
|
||||
|
||||
@@ -356,6 +389,7 @@ public class DataSourcePool implements DataSource {
|
||||
|
||||
} catch (SQLException ex) {
|
||||
notifyDataSourceIsDown(ex);
|
||||
|
||||
} finally {
|
||||
try {
|
||||
if (conn != null) {
|
||||
@@ -506,7 +540,7 @@ public class DataSourcePool implements DataSource {
|
||||
protected boolean validateConnection(PooledConnection conn) {
|
||||
try {
|
||||
if (heartbeatsql == null) {
|
||||
logger.info("Can not test connection as heartbeatsql is not set");
|
||||
logger.debug("Can not test connection as heartbeatsql is not set");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -514,8 +548,7 @@ public class DataSourcePool implements DataSource {
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
String desc = "heartbeatsql test failed on connection[" + conn.getName() + "]";
|
||||
logger.warn(desc);
|
||||
logger.warn("heartbeatsql test failed on connection[" + conn.getName() + "]");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -582,7 +615,7 @@ public class DataSourcePool implements DataSource {
|
||||
* Grow the pool by creating a new connection. The connection can either be
|
||||
* added to the available list, or returned.
|
||||
* <p>
|
||||
* This method is protected by synchronisation in calling methods.
|
||||
* This method is protected by synchronization in calling methods.
|
||||
* </p>
|
||||
*/
|
||||
protected PooledConnection createConnectionForQueue(int connId) throws SQLException {
|
||||
@@ -661,7 +694,7 @@ public class DataSourcePool implements DataSource {
|
||||
String msg = "Just testing if alert message is sent successfully.";
|
||||
|
||||
if (notify != null) {
|
||||
notify.notifyWarning(subject, msg);
|
||||
notify.dataSourceWarning(subject, msg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -674,8 +707,11 @@ public class DataSourcePool implements DataSource {
|
||||
* Connections are not waited on, as that would hang the server.
|
||||
* </p>
|
||||
*/
|
||||
public void shutdown() {
|
||||
public void shutdown(boolean deregisterDriver) {
|
||||
queue.shutdown();
|
||||
if (deregisterDriver){
|
||||
deregisterDriver();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -804,12 +840,10 @@ public class DataSourcePool implements DataSource {
|
||||
*/
|
||||
public void deregisterDriver() {
|
||||
try {
|
||||
logger.debug("Deregistered the JDBC driver "+this.databaseDriver);
|
||||
DriverManager.deregisterDriver(DriverManager.getDriver(this.databaseUrl));
|
||||
String msg = "Deregistered the JDBC driver "+this.databaseDriver;
|
||||
logger.debug(msg);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error trying to deregister the JDBC driver "+this.databaseDriver;
|
||||
logger.warn(msg, e);
|
||||
logger.warn("Error trying to deregister the JDBC driver "+this.databaseDriver, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -256,11 +256,13 @@ public class PooledConnection extends ConnectionDelegator
|
||||
String msg = "Closing Connection[" + getName() + "]" + " psReuse[" + pstmtHitCounter
|
||||
+ "] psCreate[" + pstmtMissCounter + "] psSize[" + pstmtCache.size() + "]";
|
||||
|
||||
logger.info(msg);
|
||||
logger.debug(msg);
|
||||
|
||||
try {
|
||||
if (connection.isClosed()) {
|
||||
logger.warn("Closing Connection[" + getName() + "] that is already closed?");
|
||||
// Typically the JDBC Driver has its own JVM shutdown hook and already
|
||||
// closed the connections in our DataSource pool so making this DEBUG level
|
||||
logger.debug("Closing Connection[" + getName() + "] that is already closed?");
|
||||
return;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
|
||||
@@ -273,7 +273,7 @@ public class PooledConnectionQueue {
|
||||
int busySize = registerBusyConnection(c);
|
||||
|
||||
String msg = "DataSourcePool [" + name + "] grow; id["+c.getName()+"] busy["+busySize+"] max["+maxSize+"]";
|
||||
logger.info(msg);
|
||||
logger.debug(msg);
|
||||
|
||||
checkForWarningSize();
|
||||
return c;
|
||||
@@ -333,7 +333,7 @@ public class PooledConnectionQueue {
|
||||
try {
|
||||
doingShutdown = true;
|
||||
Status status = createStatus();
|
||||
logger.info("DataSourcePool [" + name + "] shutdown: "+status);
|
||||
logger.debug("DataSourcePool [" + name + "] shutdown: "+status);
|
||||
|
||||
closeFreeConnections(true);
|
||||
|
||||
@@ -422,7 +422,7 @@ public class PooledConnectionQueue {
|
||||
freeList.setShallowCopy(freeListCopy);
|
||||
|
||||
String msg = "DataSourcePool [" + name + "] trimmed [" + trimedCount + "] inactive connections. New size[" + totalConnections() + "]";
|
||||
logger.info(msg);
|
||||
logger.debug(msg);
|
||||
}
|
||||
return trimedCount;
|
||||
}
|
||||
@@ -436,7 +436,7 @@ public class PooledConnectionQueue {
|
||||
try {
|
||||
while (!freeList.isEmpty()) {
|
||||
PooledConnection c = freeList.remove();
|
||||
logger.info("PSTMT Statistics: "+c.getStatistics());
|
||||
logger.debug("PSTMT Statistics: "+c.getStatistics());
|
||||
c.closeConnectionFully(logErrors);
|
||||
}
|
||||
} finally {
|
||||
|
||||
@@ -1,107 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.lib.sql;
|
||||
|
||||
import com.avaje.ebean.config.GlobalProperties;
|
||||
import com.avaje.ebeaninternal.server.lib.util.MailEvent;
|
||||
import com.avaje.ebeaninternal.server.lib.util.MailListener;
|
||||
import com.avaje.ebeaninternal.server.lib.util.MailMessage;
|
||||
import com.avaje.ebeaninternal.server.lib.util.MailSender;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* A simple smtp email alert that sends a email message
|
||||
* on dataSourceDown and dataSourceUp etc.
|
||||
* <ul>
|
||||
* <li>alert.fromuser = the from user name
|
||||
* <li>alert.fromemail = the from email account
|
||||
* <li>alert.toemail = comma delimited list of email accounts to email
|
||||
* <li>alert.mailserver = the smpt server name
|
||||
* </ul>
|
||||
*/
|
||||
public class SimpleAlerter implements DataSourceAlertListener, MailListener {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SimpleAlerter.class);
|
||||
|
||||
//boolean sendInBackGround = true;
|
||||
|
||||
/**
|
||||
* Create a SimpleAlerter.
|
||||
*/
|
||||
public SimpleAlerter() {
|
||||
}
|
||||
|
||||
/**
|
||||
* If the email failed then log the error.
|
||||
*/
|
||||
public void handleEvent(MailEvent event) {
|
||||
Throwable e = event.getError();
|
||||
if (e != null){
|
||||
logger.error(null, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the dataSource down alert.
|
||||
*/
|
||||
public void dataSourceDown(String dataSourceName) {
|
||||
String msg = getSubject(true, dataSourceName);
|
||||
sendMessage(msg, msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the dataSource up alert.
|
||||
*/
|
||||
public void dataSourceUp(String dataSourceName) {
|
||||
String msg = getSubject(false, dataSourceName);
|
||||
sendMessage(msg, msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the warning message.
|
||||
*/
|
||||
public void warning(String subject, String msg) {
|
||||
sendMessage(subject, msg);
|
||||
}
|
||||
|
||||
private String getSubject(boolean isDown, String dsName) {
|
||||
String msg = "The DataSource "+dsName;
|
||||
if (isDown){
|
||||
msg += " is DOWN!!";
|
||||
} else {
|
||||
msg += " is UP.";
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
private void sendMessage(String subject, String msg){
|
||||
|
||||
String fromUser = GlobalProperties.get("alert.fromuser", null);
|
||||
String fromEmail = GlobalProperties.get("alert.fromemail", null);
|
||||
String mailServerName = GlobalProperties.get("alert.mailserver", null);
|
||||
String toEmail = GlobalProperties.get("alert.toemail", null);
|
||||
|
||||
if (mailServerName == null){
|
||||
//throw new RuntimeException("alert.mailserver not set...");
|
||||
return;
|
||||
}
|
||||
|
||||
MailMessage data = new MailMessage();
|
||||
data.setSender(fromUser, fromEmail);
|
||||
data.addBodyLine(msg);
|
||||
data.setSubject(subject);
|
||||
|
||||
String[] toList = toEmail.split(",");
|
||||
if (toList.length==0) {
|
||||
throw new RuntimeException("alert.toemail has not been set?");
|
||||
}
|
||||
for (int i = 0; i < toList.length; i++) {
|
||||
data.addRecipient(null, toList[i].trim());
|
||||
}
|
||||
|
||||
MailSender sender = new MailSender(mailServerName);
|
||||
sender.setMailListener(this);
|
||||
sender.sendInBackground(data);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.avaje.ebeaninternal.server.lib.sql;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.avaje.ebean.config.GlobalProperties;
|
||||
import com.avaje.ebeaninternal.server.lib.util.MailEvent;
|
||||
import com.avaje.ebeaninternal.server.lib.util.MailListener;
|
||||
import com.avaje.ebeaninternal.server.lib.util.MailMessage;
|
||||
import com.avaje.ebeaninternal.server.lib.util.MailSender;
|
||||
|
||||
/**
|
||||
* A simple smtp email alert that sends a email message on dataSourceDown and
|
||||
* dataSourceUp etc.
|
||||
* <ul>
|
||||
* <li>alert.fromuser = the from user name
|
||||
* <li>alert.fromemail = the from email account
|
||||
* <li>alert.toemail = comma delimited list of email accounts to email
|
||||
* <li>alert.mailserver = the smpt server name
|
||||
* </ul>
|
||||
*/
|
||||
public class SimpleDataSourceAlert implements DataSourceAlert, MailListener {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SimpleDataSourceAlert.class);
|
||||
|
||||
// boolean sendInBackGround = true;
|
||||
|
||||
/**
|
||||
* Create a SimpleAlerter.
|
||||
*/
|
||||
public SimpleDataSourceAlert() {
|
||||
}
|
||||
|
||||
/**
|
||||
* If the email failed then log the error.
|
||||
*/
|
||||
public void handleEvent(MailEvent event) {
|
||||
Throwable e = event.getError();
|
||||
if (e != null) {
|
||||
logger.error(null, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the dataSource down alert.
|
||||
*/
|
||||
@Override
|
||||
public void dataSourceDown(String dataSourceName) {
|
||||
String msg = getSubject(true, dataSourceName);
|
||||
sendMessage(msg, msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the dataSource up alert.
|
||||
*/
|
||||
@Override
|
||||
public void dataSourceUp(String dataSourceName) {
|
||||
String msg = getSubject(false, dataSourceName);
|
||||
sendMessage(msg, msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the warning message.
|
||||
*/
|
||||
@Override
|
||||
public void dataSourceWarning(String subject, String msg) {
|
||||
sendMessage(subject, msg);
|
||||
}
|
||||
|
||||
private String getSubject(boolean isDown, String dsName) {
|
||||
String msg = "The DataSource " + dsName;
|
||||
if (isDown) {
|
||||
msg += " is DOWN!!";
|
||||
} else {
|
||||
msg += " is UP.";
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
private void sendMessage(String subject, String msg) {
|
||||
|
||||
String mailServerName = GlobalProperties.get("datasource.alert.mailserver", null);
|
||||
if (mailServerName == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String fromUser = GlobalProperties.get("datasource.alert.fromuser", null);
|
||||
String fromEmail = GlobalProperties.get("datasource.alert.fromemail", null);
|
||||
String toEmail = GlobalProperties.get("datasource.alert.toemail", null);
|
||||
|
||||
MailMessage data = new MailMessage();
|
||||
data.setSender(fromUser, fromEmail);
|
||||
data.addBodyLine(msg);
|
||||
data.setSubject(subject);
|
||||
|
||||
String[] toList = toEmail.split(",");
|
||||
if (toList.length == 0) {
|
||||
throw new RuntimeException("alert.toemail has not been set?");
|
||||
}
|
||||
for (int i = 0; i < toList.length; i++) {
|
||||
data.addRecipient(null, toList[i].trim());
|
||||
}
|
||||
|
||||
MailSender sender = new MailSender(mailServerName);
|
||||
sender.setMailListener(this);
|
||||
sender.sendInBackground(data);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user