From ac7eb459fdc1f405ddb38a8b227e04de23ae1966 Mon Sep 17 00:00:00 2001 From: Amaresh Kumar Date: Thu, 30 Aug 2018 17:45:54 +0530 Subject: Fixed Sonar Issues in LatencyReport.Java Fixed 3 Sonar issues. Issue-ID: DMAAP-676 Change-Id: I5f1862843a10eae8f9ba7f0d04a635c5f95b807e Signed-off-by: Amaresh Kumar --- .../dmaap/datarouter/reports/LatencyReport.java | 58 +++++++++++----------- 1 file changed, 28 insertions(+), 30 deletions(-) (limited to 'datarouter-prov/src/main/java/org/onap') diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/LatencyReport.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/LatencyReport.java index ba8f15a0..549511b7 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/LatencyReport.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/LatencyReport.java @@ -145,40 +145,38 @@ public class LatencyReport extends ReportBase { DB db = new DB(); @SuppressWarnings("resource") Connection conn = db.getConnection(); - PreparedStatement ps = conn.prepareStatement(SELECT_SQL); + try(PreparedStatement ps = conn.prepareStatement(SELECT_SQL)){ ps.setLong(1, from); ps.setLong(2, to); - ResultSet rs = ps.executeQuery(); - PrintWriter os = new PrintWriter(outfile); - os.println("recordid,feedid,uri,size,min,max,avg,fanout"); - Counters c = null; - while (rs.next()) { - long etime = rs.getLong("EVENT_TIME"); - String type = rs.getString("TYPE"); - String id = rs.getString("PUBLISH_ID"); - String fid = rs.getString("FEED_FILEID"); - int feed = rs.getInt("FEEDID"); - long clen = rs.getLong("CONTENT_LENGTH"); - if (c != null && !id.equals(c.id)) { - String line = id + "," + c.toString(); - os.println(line); - c = null; + try(ResultSet rs = ps.executeQuery()) { + try(PrintWriter os = new PrintWriter(outfile)) { + os.println("recordid,feedid,uri,size,min,max,avg,fanout"); + Counters c = null; + while (rs.next()) { + long etime = rs.getLong("EVENT_TIME"); + String type = rs.getString("TYPE"); + String id = rs.getString("PUBLISH_ID"); + String fid = rs.getString("FEED_FILEID"); + int feed = rs.getInt("FEEDID"); + long clen = rs.getLong("CONTENT_LENGTH"); + if (c != null && !id.equals(c.id)) { + String line = id + "," + c.toString(); + os.println(line); + c = null; + } + if (c == null) { + c = new Counters(id, feed, clen, fid); + } + if (feed != c.feedid) + System.err.println("Feed ID mismatch, " + feed + " <=> " + c.feedid); + if (clen != c.clen) + System.err.println("Cont Len mismatch, " + clen + " <=> " + c.clen); + c.addEvent(type, etime); + } } - if (c == null) { - c = new Counters(id, feed, clen, fid); - } - if (feed != c.feedid) - System.err.println("Feed ID mismatch, " + feed + " <=> " + c.feedid); - if (clen != c.clen) - System.err.println("Cont Len mismatch, " + clen + " <=> " + c.clen); -// if (fid != c.fileid) -// System.err.println("File ID mismatch, "+fid+" <=> "+c.fileid); - c.addEvent(type, etime); + db.release(conn); + } } - rs.close(); - ps.close(); - db.release(conn); - os.close(); } catch (FileNotFoundException e) { System.err.println("File cannot be written: " + outfile); } catch (SQLException e) { -- cgit 1.2.3-korg