View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.concurrent.jdbc;
33  
34  import java.io.Serializable;
35  import java.util.Objects;
36  import javax.annotation.ParametersAreNonnullByDefault;
37  import org.spf4j.jdbc.DbType;
38  import org.spf4j.jdbc.JdbcTemplate;
39  
40  /*
41   * CREATE TABLE HEARTBEATS (
42   *  OWNER VARCHAR(255) NOT NULL,
43   *  INTERVAL_MILLIS bigint(20) NOT NULL,
44   *  LAST_HEARTBEAT_INSTANT_MILLIS bigint(20) NOT NULL,
45   *  PRIMARY KEY (OWNER),
46   *  UNIQUE KEY HEARTBEATS_PK (OWNER)
47   * );
48   *
49   * Table description for storing heartbeats for processes. (OWNER)
50   * The main purpose of this table is to detect dead OWNERS (when their heart stops beating)
51   * OWNER = String column uniquely identifying a process.
52   * INTERVAL_MILLIS - the delay between heartbeats.
53   * LAST_HEARTBEAT_INSTANT_MILLIS - the millis since epoch when the last heartbeat happened.
54   */
55  @ParametersAreNonnullByDefault
56  public final class HeartBeatTableDesc implements Serializable {
57  
58    private static final long serialVersionUID = 1L;
59  
60    public static final HeartBeatTableDesc DEFAULT = new HeartBeatTableDesc(
61            System.getProperty("spf4j.jdbc.heartBeats.sql.tableName", "HEARTBEATS"),
62            System.getProperty("spf4j.jdbc.heartBeats.sql.ownerColumn", "OWNER"),
63            System.getProperty("spf4j.jdbc.heartBeats.sql.intervalMillisColumn", "INTERVAL_MILLIS"),
64            System.getProperty("spf4j.jdbc.heartBeats.sql.lastHeartBeatMillisColumn", "LAST_HEARTBEAT_INSTANT_MILLIS"),
65            DbType.valueOf(System.getProperty("spf4j.jdbc.heartBeats.sql.dbType", "H2")));
66  
67  
68    private final String tableName;
69    private final String ownerColumn;
70    private final String intervalColumn;
71    private final String lastHeartbeatColumn;
72  
73    /**
74     * MSSQL = DATEDIFF(ms, '1970-01-01 00:00:00', GETUTCDATE())
75     * ORACLE = (SYSDATE - TO_DATE('01-01-1970 00:00:00', 'DD-MM-YYYY HH24:MI:SS')) * 24 * 3600000
76     * H2 = TIMESTAMPDIFF('MILLISECOND', timestamp '1970-01-01 00:00:00', CURRENT_TIMESTAMP())
77     * POSTGRES = extract(epoch FROM now()) * 1000
78     */
79    private final DbType dbType;
80  
81    public HeartBeatTableDesc(final String tableName, final String ownerColun,
82          final String intervalColumn, final String lastHeartbeatColumn, final DbType dbType) {
83      JdbcTemplate.checkJdbcObjectName(tableName);
84      JdbcTemplate.checkJdbcObjectName(ownerColun);
85      JdbcTemplate.checkJdbcObjectName(intervalColumn);
86      JdbcTemplate.checkJdbcObjectName(lastHeartbeatColumn);
87      this.tableName = tableName;
88      this.ownerColumn = ownerColun;
89      this.intervalColumn = intervalColumn;
90      this.lastHeartbeatColumn = lastHeartbeatColumn;
91      this.dbType = dbType;
92    }
93  
94    public String getTableName() {
95      return tableName;
96    }
97  
98    public String getOwnerColumn() {
99      return ownerColumn;
100   }
101 
102   public String getIntervalColumn() {
103     return intervalColumn;
104   }
105 
106   public String getLastHeartbeatColumn() {
107     return lastHeartbeatColumn;
108   }
109 
110   public DbType getDbType() {
111     return dbType;
112   }
113 
114   public HeartBeatTableDesc withDbType(final DbType pdbType) {
115     return new HeartBeatTableDesc(tableName, ownerColumn, intervalColumn, lastHeartbeatColumn, pdbType);
116   }
117 
118   @Override
119   public int hashCode() {
120     return tableName.hashCode();
121   }
122 
123   @Override
124   public boolean equals(final Object obj) {
125     if (this == obj) {
126       return true;
127     }
128     if (obj == null) {
129       return false;
130     }
131     if (getClass() != obj.getClass()) {
132       return false;
133     }
134     final HeartBeatTableDesc other = (HeartBeatTableDesc) obj;
135     if (!Objects.equals(this.tableName, other.tableName)) {
136       return false;
137     }
138     if (!Objects.equals(this.ownerColumn, other.ownerColumn)) {
139       return false;
140     }
141     if (!Objects.equals(this.intervalColumn, other.intervalColumn)) {
142       return false;
143     }
144     if (!Objects.equals(this.lastHeartbeatColumn, other.lastHeartbeatColumn)) {
145       return false;
146     }
147     return this.dbType == other.dbType;
148   }
149 
150 
151 
152 
153   @Override
154   public String toString() {
155     return "HeartbeatTableDesc{" + "tableName=" + tableName + ", ownerColun=" + ownerColumn + ", intervalColumn="
156             + intervalColumn + ", lastHeartbeatColumn=" + lastHeartbeatColumn + '}';
157   }
158 
159 }