KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > mysql > StatusTask


1 package net.sf.jasperreports.mysql;
2
3 import java.sql.Connection JavaDoc;
4 import java.sql.PreparedStatement JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.util.Date JavaDoc;
8
9 /**
10  * Extends task to run an update on status information from
11  * an sql databse.
12  */

13 public class StatusTask extends Task
14 {
15     private long pBytes_received;
16     private long pBytes_sent;
17     private long pCom_select;
18     private long pCom_insert;
19     private long pConnections;
20
21     private boolean firstUpdate = true;
22     private static final long TWO_TO_THE_32ND_POWER = 4294967296L;
23
24     /**
25      * sets the databases source and desitanation in Task.
26      *
27      * @param parent - the database to read from.
28      * @param destination - the database to write to.
29      */

30     public StatusTask(Database parent, Database destination)
31     {
32         super(parent, destination);
33     }
34
35     /**
36      * gets information from SHOW STATUS in source and dumps it into destination.
37      * <p/>
38      * columns retrieved:
39      * report_time TIMESTAMP(12), //the time at which this function was called.
40      * bytes_received BIGINT,
41      * bytes_sent BIGINT,
42      * total_selects BIGINT,
43      * total_insert BIGINT,
44      * connections BIGINT,
45      * uptime BIGINT
46      */

47
48     public void run()
49     {
50         try
51         {
52             //System.out.print("updating status...");
53
String JavaDoc currentSource = parent.name;
54
55             //connect
56
Connection JavaDoc userConn = parent.getConnection();
57             Connection JavaDoc repConn = destination.getConnection();
58
59             //prepare query and insert statements
60
PreparedStatement JavaDoc get_status = userConn.prepareStatement(
61                     "SHOW STATUS LIKE ?;");
62             PreparedStatement JavaDoc ins_status = repConn.prepareStatement("INSERT INTO status"
63                                                                     +
64                                                                     " VALUES (?,?,?,?,?,?,?,?);");
65
66             //get the data ot of SHOW STATUS;
67
long Bytes_received = 0;
68             long Bytes_sent = 0;
69             long Com_select = 0;
70             long Com_insert = 0;
71             long Connections = 0;
72             long Uptime = 0;
73
74             //bytes_received
75
get_status.setString(1, "Bytes_received");
76             ResultSet JavaDoc rBytes_received = get_status.executeQuery();
77             if (rBytes_received.next())
78             {
79                 Bytes_received = rBytes_received.getLong("Value");
80                 rBytes_received.close();
81             }
82
83             //bytes_sent
84
get_status.setString(1, "Bytes_sent");
85             ResultSet JavaDoc rBytes_sent = get_status.executeQuery();
86             if (rBytes_sent.next())
87             {
88                 Bytes_sent = rBytes_sent.getLong("Value");
89                 rBytes_sent.close();
90             }
91
92             //com_select
93
get_status.setString(1, "Com_select");
94             ResultSet JavaDoc rCom_select = get_status.executeQuery();
95             if (rCom_select.next())
96             {
97                 Com_select = rCom_select.getLong("Value");
98                 rCom_select.close();
99             }
100
101             //com_insert
102
get_status.setString(1, "Com_insert");
103             ResultSet JavaDoc rCom_insert = get_status.executeQuery();
104             if (rCom_insert.next())
105             {
106                 Com_insert = rCom_insert.getLong("Value");
107                 rCom_insert.close();
108             }
109
110             //connections
111
get_status.setString(1, "Connections");
112             ResultSet JavaDoc rConnections = get_status.executeQuery();
113             if (rConnections.next())
114             {
115                 Connections = rConnections.getLong("Value");
116                 rConnections.close();
117             }
118
119             //uptime
120
get_status.setString(1, "Uptime");
121             ResultSet JavaDoc rUptime = get_status.executeQuery();
122             if (rUptime.next())
123             {
124                 Uptime = rUptime.getLong("Value");
125                 rUptime.close();
126             }
127             //see if the is the first update. if not insert the data
128
if (firstUpdate == false)
129             {
130
131                 //get the current date
132
Date JavaDoc d = new Date JavaDoc();
133                 java.sql.Timestamp JavaDoc SQLdate = new java.sql.Timestamp JavaDoc(
134                         d.getTime());
135
136                 //status columns
137
// source VARCHAR(32),
138
// database VARCHAR(32),
139
// report_time TIMESTAMP,
140
// bytes_received BIGINT,
141
// bytes_sent BIGINT,
142
// total_selects BIGINT,
143
// total_insert BIGINT,
144
// connections BIGINT,
145
// uptime BIGINT"
146
ins_status.setString(1, currentSource);
147                 ins_status.setTimestamp(2, SQLdate);
148                 long deltaBytesRecevied = Bytes_received - pBytes_received;
149                 if (deltaBytesRecevied < 0) {
150                     deltaBytesRecevied += TWO_TO_THE_32ND_POWER;
151                 }
152                 ins_status.setLong(3, deltaBytesRecevied);
153                 long deltaBytesSent = Bytes_sent - pBytes_sent;
154                 if (deltaBytesSent < 0) {
155                     deltaBytesSent += TWO_TO_THE_32ND_POWER;
156                 }
157                 ins_status.setLong(4, deltaBytesSent);
158                 long deltaSelect = Com_select - pCom_select;
159                 if (deltaSelect < 0) {
160                     deltaSelect += TWO_TO_THE_32ND_POWER;
161                 }
162                 ins_status.setLong(5, deltaSelect);
163                 long deltaInsert = Com_insert - pCom_insert;
164                 if (deltaInsert < 0) {
165                     deltaInsert += TWO_TO_THE_32ND_POWER;
166                 }
167                 ins_status.setLong(6, deltaInsert);
168                 long deltaConnections = Connections - pConnections;
169                 if (deltaConnections < 0) {
170                     deltaConnections += TWO_TO_THE_32ND_POWER;
171                 }
172                 ins_status.setLong(7, deltaConnections);
173                 ins_status.setLong(8, Uptime);
174                 //execute
175
ins_status.executeUpdate();
176                 ins_status.close();
177                 get_status.close();
178                 //if so, set globals for reference at the next update.
179
} else
180             {
181                 firstUpdate = false;
182             }
183             pBytes_received = Bytes_received;
184             pBytes_sent = Bytes_sent;
185             pCom_select = Com_select;
186             pCom_insert = Com_insert;
187             pConnections = Connections;
188
189         } catch (SQLException JavaDoc e)
190         {
191             e.printStackTrace();
192             System.out.println("Exception: " + e.getMessage());
193             System.out.println("The Status Task did not complete.");
194             System.exit(1);
195         }
196         //System.out.println("Status Task executed");
197
}
198
199 }
200
Popular Tags