Guest User

Untitled

a guest
Oct 14th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1. package com.arconsis.locationtolife.server.verticle;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.Driver;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10.  
  11. import org.apache.log4j.Logger;
  12. import org.vertx.java.core.AsyncResult;
  13. import org.vertx.java.core.AsyncResultHandler;
  14. import org.vertx.java.core.Handler;
  15. import org.vertx.java.core.eventbus.EventBus;
  16. import org.vertx.java.core.eventbus.Message;
  17. import org.vertx.java.core.json.JsonArray;
  18. import org.vertx.java.core.json.JsonObject;
  19. import org.vertx.java.deploy.Verticle;
  20.  
  21. import snaq.db.ConnectionPool;
  22.  
  23. public class DataAccessManager extends Verticle {
  24.  
  25. private static Logger LOG = Logger.getLogger(DataAccessManager.class);
  26. private static JsonObject CONFIG = null;
  27. private static ConnectionPool POOL = null;
  28.  
  29. private static final String QUERY_ALL_ITEMS = "SELECT * FROM item_detail;";
  30. private static final String QUERY_ITEM_DETAIL = "SELECT * FROM item_detail WHERE id = ?";
  31.  
  32. private EventBus eventBus = null;
  33.  
  34. @Override
  35. public void start() throws Exception {
  36. if (LOG.isDebugEnabled()) {
  37. LOG.debug(">> start");
  38. }
  39. eventBus = vertx.eventBus();
  40. CONFIG = container.getConfig();
  41.  
  42. initializeDatabase();
  43. setupMessageHandler();
  44. }
  45.  
  46.  
  47.  
  48. private void initializeDatabase() {
  49. try {
  50. Class<?> clazz = Class.forName(CONFIG.getString("driverClass"));
  51. driver = (Driver)clazz.newInstance();
  52. DriverManager.registerDriver(driver);
  53.  
  54. // Configure pool of not already done
  55. if (POOL == null) {
  56. int minPool = CONFIG.getNumber("minPool").intValue();
  57. int maxPool = CONFIG.getNumber("maxPool").intValue();
  58. int maxSize = CONFIG.getNumber("maxSize").intValue();
  59. int idleTimeout = CONFIG.getNumber("idleTimeout").intValue();
  60. String url = CONFIG.getString("jdbcUrl");
  61. String username = CONFIG.getString("username");
  62. String password = CONFIG.getString("password");
  63.  
  64. POOL = new ConnectionPool("LOCALPOOL", minPool, maxPool,
  65. maxSize, idleTimeout, url, username, password);
  66. LOG.debug("created new connection pool instance!");
  67. }
  68.  
  69. } catch (Exception ex) {
  70. LOG.error("Could not load database driver!", ex);
  71. }
  72. }
  73.  
  74.  
  75. private void setupMessageHandler() {
  76.  
  77. registerItems();
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. private void registerItems() {
  86. eventBus.registerHandler("query.items", new Handler<Message<JsonObject>>() {
  87. public void handle(Message<JsonObject> message) {
  88. if (LOG.isDebugEnabled()) {
  89. LOG.debug(">> query.items");
  90. }
  91.  
  92. //LOG.debug(message.body.toString());
  93.  
  94. final Connection conn = getConnection(1000);
  95. Statement stmt = null;
  96. ResultSet rs = null;
  97. try {
  98.  
  99. if (conn != null) {
  100.  
  101. stmt = conn.createStatement();
  102. rs = stmt.executeQuery(QUERY_ALL_ITEMS);
  103.  
  104. JsonArray jsonArray = new JsonArray();
  105. while (rs.next()) {
  106. JsonObject jsonObject = new JsonObject();
  107. jsonObject.putNumber("id", rs.getLong("id"));
  108. jsonObject.putString("name", rs.getString("name"));
  109. jsonObject.putString("data", rs.getString("data"));
  110. jsonObject.putString("created_date", rs.getTimestamp("created_date").toString());
  111. jsonObject.putString("changed_date", rs.getTimestamp("changed_date").toString());
  112.  
  113. String tags = rs.getString("tags");
  114. jsonObject.putString("tags", tags == null ? "" : tags);
  115.  
  116. String category = rs.getString("category");
  117. jsonObject.putString("category", category == null ? "" : category);
  118.  
  119. long group_id = rs.getLong("group_id");
  120. jsonObject.putNumber("group_id", group_id);
  121.  
  122. jsonArray.add(jsonObject);
  123. }
  124.  
  125. JsonObject replyObject = new JsonObject();
  126. replyObject.putArray("items", jsonArray);
  127.  
  128. message.reply(replyObject.putString("status", "ok"));
  129. } else {
  130. // timeout occurred!
  131. LOG.debug("TIMEOUT OCCURED");
  132. message.reply(message.body.putString("status", "error"));
  133. }
  134.  
  135.  
  136. } catch (Exception e) {
  137. LOG.error("Error ", e);
  138. message.reply(message.body.putString("status", "error"));
  139. } finally {
  140. try {
  141. if (rs != null) {rs.close();};
  142. if (stmt != null) {stmt.close();};
  143. if (conn!= null) {conn.close();};
  144. } catch(SQLException e) { /* .. */};
  145. }
  146. }
  147. }, new AsyncResultHandler<Void>() {
  148. public void handle(AsyncResult<Void> arg0) {
  149. if (LOG.isDebugEnabled()) {
  150. LOG.debug(">> query.items handler registered");
  151. }
  152. }
  153. });
  154.  
  155. eventBus.registerHandler("query.items.detail", new Handler<Message<JsonObject>>() {
  156. public void handle(Message<JsonObject> message) {
  157. if (LOG.isDebugEnabled()) {
  158. LOG.debug(">> query.items.detail");
  159. }
  160.  
  161. //LOG.debug(message.body.toString());
  162. final Integer item_detail_id = message.body.getNumber("item_detail_id").intValue();
  163.  
  164. final Connection conn = getConnection(1000);
  165. PreparedStatement stmt = null;
  166. ResultSet rs = null;
  167. try {
  168.  
  169. if (conn != null) {
  170.  
  171. stmt = conn.prepareStatement(QUERY_ITEM_DETAIL);
  172. stmt.setInt(1, item_detail_id);
  173. rs = stmt.executeQuery();
  174.  
  175.  
  176. JsonObject jsonObject = null;
  177. while (rs.next()) {
  178. jsonObject = new JsonObject();
  179. jsonObject.putNumber("id", rs.getLong("id"));
  180. jsonObject.putString("name", rs.getString("name"));
  181. jsonObject.putString("data", rs.getString("data"));
  182. jsonObject.putString("created_date", rs.getTimestamp("created_date").toString());
  183. jsonObject.putString("changed_date", rs.getTimestamp("changed_date").toString());
  184.  
  185. String tags = rs.getString("tags");
  186. jsonObject.putString("tags", tags == null ? "" : tags);
  187.  
  188. String category = rs.getString("category");
  189. jsonObject.putString("category", category == null ? "" : category);
  190.  
  191. long group_id = rs.getLong("group_id");
  192. jsonObject.putNumber("group_id", group_id);
  193. }
  194.  
  195.  
  196. JsonObject replyObject = new JsonObject();
  197.  
  198. String responseStatus = "ok";
  199. if (jsonObject != null) {
  200. replyObject.putObject("item", jsonObject);
  201. } else {
  202. responseStatus = "notFound";
  203. }
  204.  
  205. message.reply(replyObject.putString("status", responseStatus));
  206.  
  207.  
  208. } else {
  209. // timeout occurred!
  210. LOG.debug("TIMEOUT OCCURED");
  211. message.reply(message.body.putString("status", "error"));
  212. }
  213.  
  214.  
  215. } catch (Exception e) {
  216. LOG.error("Error ", e);
  217. message.reply(message.body.putString("status", "error"));
  218. } finally {
  219. try {
  220. if (rs != null) {rs.close();};
  221. if (stmt != null) {stmt.close();};
  222. if (conn!= null) {conn.close();};
  223. } catch(SQLException e) { /* .. */};
  224. }
  225. }
  226. }, new AsyncResultHandler<Void>() {
  227. public void handle(AsyncResult<Void> arg0) {
  228. if (LOG.isDebugEnabled()) {
  229. LOG.debug(">> query.items.detail handler registered");
  230. }
  231. }
  232. });
  233. }
  234.  
  235.  
  236.  
  237.  
  238. private final Connection getConnection(long timeout) {
  239.  
  240. Connection conn = null;
  241. try {
  242.  
  243. conn = POOL.getConnection(timeout);
  244. } catch (Exception ex) {
  245. LOG.error("DB Exception: " + ex.getMessage());
  246. }
  247.  
  248. return conn;
  249. }
  250.  
  251.  
  252. }
Add Comment
Please, Sign In to add comment