package org.opencloudb.jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.locks.ReentrantLock;
import org.opencloudb.heartbeat.DBHeartbeat;

public class JDBCHeartbeat extends DBHeartbeat{
	private final ReentrantLock lock;
	private final JDBCDatasource source;
    private final boolean heartbeatnull;
	public JDBCHeartbeat(JDBCDatasource source)
	{
		this.source = source;
		lock = new ReentrantLock(false);
		this.status = INIT_STATUS;
		this.heartbeatSQL = source.getHostConfig().getHearbeatSQL().trim();
		this.heartbeatnull= heartbeatSQL.length()==0;
	}

	@Override
	public void start()
	{
		if (this.heartbeatnull){
			stop();
			return;
		}
		lock.lock();
		try
		{
			isStop.compareAndSet(true, false);
			this.status = DBHeartbeat.OK_STATUS;
		} finally
		{
			lock.unlock();
		}
	}

	@Override
	public void stop()
	{
		lock.lock();
		try
		{
			if (isStop.compareAndSet(false, true))
			{
				isChecking.set(false);
			}
		} finally
		{
			lock.unlock();
		}
	}

	@Override
	public String getLastActiveTime()
	{
		return null;
	}

	@Override
	public long getTimeout()
	{
		return 0;
	}

	@Override
	public void heartbeat()
	{
		if (isStop.get())
			return;

		lock.lock();
		try
		{
			isChecking.set(true);

			try (Connection c = source.getConnection())
			{
				try (Statement s = c.createStatement())
				{
					s.execute(heartbeatSQL);
				}
			}
			status = OK_STATUS;

		} catch (Exception ex)
		{
			status = ERROR_STATUS;
		} finally
		{
			lock.unlock();
			this.isChecking.set(false);
		}
	}
}

@@ -108,16 +108,74 @@
 tement;%0A
+import java.text.SimpleDateFormat;%0Aimport java.util.Date;%0A
 import j
@@ -214,16 +214,49 @@
 antLock;
+%0A%0Aimport org.apache.log4j.Logger;
 %0Aimport 
@@ -292,16 +292,67 @@
 artbeat;
+%0Aimport org.opencloudb.statistic.HeartbeatRecorder;
 %0A%0Apublic
@@ -507,16 +507,216 @@
 atnull;%0A
+    private Long lastSendTime = System.currentTimeMillis();%0A    private Long lastReciveTime = System.currentTimeMillis();%0A    %0A    %0A    private Logger logger = Logger.getLogger(this.getClass());%0A    %0A
 %09public 
@@ -1432,120 +1432,434 @@
 %09%7B%0A%09
-%09return null;%0A%09%7D%0A%0A%09@Override%0A%09public long getTimeout()%0A%09%7B%0A%09%09return 0;%0A%09%7D%0A%0A%09@Override%0A%09public void heartbeat()%0A%09%7B
+    long t = lastReciveTime;%0A%09    SimpleDateFormat sdf = new SimpleDateFormat(%22yyyy-MM-dd HH:mm:ss%22);%0A        return sdf.format(new Date(t));%0A%09%7D%0A%0A%09@Override%0A%09public long getTimeout()%0A%09%7B%0A%09%09return 0;%0A%09%7D%0A%09@Override%0A%09public HeartbeatRecorder getRecorder() %7B%0A        recorder.set(lastReciveTime - lastSendTime);%0A        return recorder;%0A    %7D%0A%09%0A%09@Override%0A%09public void heartbeat()%0A%09%7B%0A%09    lastSendTime = System.currentTimeMillis();
 %0A%09%09i
@@ -2104,16 +2104,167 @@
 STATUS;%0A
+%09%09%09if(logger.isDebugEnabled())%7B%0A%09%09%09    logger.debug(%22JDBCHeartBeat con query sql: %22+heartbeatSQL);%0A%09%09%09%7D%0A%09%09%09lastReciveTime = System.currentTimeMillis();
 %0A%09%09%7D cat

修改jdbc的心跳数据，show @@heartbeat