Advertisement
Guest User

Untitled

a guest
May 14th, 2017
646
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.53 KB | None | 0 0
  1. package com.zirius.db;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5.  
  6. /**
  7. *
  8. * @author Petter Holone <petter@zirius.no>
  9. */
  10. public class ResourceLockerUtil {
  11.  
  12. public static final String TABLE = "system_uppgrade";
  13. private static final String prefix = "RES_LOCKER_";
  14. private static final String LOCK_WAIT_TIME_KEY = "ResourceLockerUtilWaitTime";
  15. private static final int DEFAULT_WAIT_TIME = 2000;
  16. private static int waitTime = 0;
  17.  
  18. private static boolean isResourceFreeForMe(String resourceID, String lockID) {
  19. {//SQL Query
  20. final com.zirius.db.jData sql = new com.zirius.db.jData();
  21. try {
  22. sql.query("SELECT TID,VERDI FROM " + TABLE + " WHERE ID = '" + resourceID + "' and TID > NOW()");
  23. if (sql.gotoFirst()) {
  24. String machine = sql.getPropertyString("VERDI");
  25. return machine.equals(lockID);
  26. }
  27. return true;
  28. } finally {
  29. sql.close();
  30. }
  31. }
  32. }
  33.  
  34. public static boolean claimResource(String resourceID, String lockID, long minutes) {
  35. if (minutes < 1l) {
  36. throw new IllegalArgumentException("minutes < 1");
  37. }
  38. if (resourceID == null || resourceID.length() < 0) {
  39. throw new IllegalArgumentException("resourceID can not be empty");
  40. }
  41. if (resourceID.length() > 80) {
  42. throw new IllegalArgumentException("resourceID can not be longer than 80 characters");
  43. }
  44. if (lockID == null || lockID.length() < 0) {
  45. throw new IllegalArgumentException("lockID can not be empty");
  46. }
  47. if (lockID.length() > 100) {
  48. throw new IllegalArgumentException("lockID can not be longer than 100 characters");
  49. }
  50. resourceID = prefix + resourceID;
  51. if (isResourceFreeForMe(resourceID, lockID)) {
  52. {//SQL Query
  53. final com.zirius.db.jData sql = new com.zirius.db.jData();
  54. try {
  55. sql.queryUpdateInt("INSERT INTO " + TABLE + " VALUES('" + resourceID + "',1,NOW() + INTERVAL " + minutes + " MINUTE,'" + lockID + "')"
  56. + " ON DUPLICATE KEY UPDATE TID=NOW()+INTERVAL " + minutes + " MINUTE, VERDI='" + lockID + "'");
  57. } finally {
  58. sql.close();
  59. }
  60. }
  61. try {
  62. Thread.sleep(getWaitTime());
  63. } catch (InterruptedException ex) {
  64. ex.printStackTrace();
  65. }
  66. if (isResourceFreeForMe(resourceID, lockID)) {
  67. return true;
  68. } else {
  69. return false;
  70. }
  71. } else {
  72. return false;
  73. }
  74.  
  75. }
  76.  
  77. public static void forceFreeResource(String resourceID) {
  78.  
  79. {//SQL Query
  80. final com.zirius.db.jData sql = new com.zirius.db.jData();
  81. try {
  82. sql.queryUpdateInt("UPDATE system_uppgrade SET TID = NOW() WHERE ID='" + resourceID + "'");
  83. } finally {
  84. sql.close();
  85. }
  86. }
  87.  
  88. }
  89.  
  90. public static void freeResource(String resourceID, String lockID) {
  91. if (isResourceFreeForMe(resourceID, lockID)) {
  92. {//SQL Query
  93. final com.zirius.db.jData sql = new com.zirius.db.jData();
  94. try {
  95. sql.queryUpdateInt("UPDATE system_uppgrade SET TID = NOW() WHERE ID='" + resourceID + "' AND VERDI='" + lockID + "'");
  96. } finally {
  97. sql.close();
  98. }
  99. }
  100. }
  101. }
  102.  
  103. public static int getWaitTime() {
  104. if (waitTime == 0) {
  105. {//SQL Query
  106. final com.zirius.db.jData sql = new com.zirius.db.jData();
  107. try {
  108. final StringBuffer query = new StringBuffer(200);
  109. sql.query("select UTFORT from " + TABLE + " where ID = '" + LOCK_WAIT_TIME_KEY + "'");
  110. if (sql.gotoFirst()) {
  111. do {
  112. int wait = sql.getPropertyint("UTFORT");
  113. if (wait == 0) {
  114. waitTime = DEFAULT_WAIT_TIME;
  115. } else {
  116. waitTime = wait;
  117. }
  118. } while (sql.gotoNext());
  119. }
  120. } catch (Throwable t) {
  121. waitTime = DEFAULT_WAIT_TIME;
  122. } finally {
  123. sql.close();
  124. }
  125. }
  126. }
  127. return waitTime;
  128. }
  129.  
  130. public static void main(String[] args) throws InterruptedException {
  131. {//init server db
  132.  
  133. final String serveraddress = "localhost";
  134. final String sqlUser = "root";
  135. final String sqlPass = "root";
  136. final String client = "duoshopas";
  137. final String clientUser = "test";
  138.  
  139. com.zirius.core.Workbench.server = true;
  140.  
  141. com.zirius.olfi.domain.Constants.currentserver = new com.zirius.db.Server(client, serveraddress, sqlUser, sqlPass, client);
  142. com.zirius.db.pool.init();
  143.  
  144. com.zirius.db.DBService dbservice = new com.zirius.db.DBService(com.zirius.dataObjects.firmaObject.class);
  145. dbservice.hente(com.zirius.db.DBService.DATA_FIRST, null);
  146. com.zirius.core.Workbench.setFirma((com.zirius.dataObjects.firmaObject) dbservice.getModel());
  147.  
  148. com.zirius.core.Workbench.setUser(com.zirius.olfi.modules.user.utils.UserUtil.getUserByUsername(clientUser));
  149. }
  150. ArrayList<TestThread> testThread = new ArrayList();
  151. for (int j = 0; j < 50; j++) {
  152. testThread.add(new TestThread());
  153. }
  154. for (int i = 0; i < 500; i++) {
  155. {//SQL Query
  156. final com.zirius.db.jData sql = new com.zirius.db.jData();
  157. try {
  158. forceFreeResource("ResourceLockerUtil_TEST");
  159. if (sql.gotoFirst()) {
  160. do {
  161. } while (sql.gotoNext());
  162. }
  163. } finally {
  164. sql.close();
  165. }
  166. }
  167. Thread.sleep(1000l);
  168. System.out.println("\n\nSpør etter ressurs. En bør få den\n\n");
  169. for (TestThread t : testThread) {
  170. new Thread(t).start();
  171. }
  172.  
  173. Thread.sleep(6000l);
  174.  
  175. }
  176.  
  177. }
  178. static HashMap<Integer, Boolean> map = new HashMap();
  179.  
  180. static synchronized void updateResult(boolean result, Integer id) {
  181. if (result) {
  182. if (map.get(id) != null) {
  183. throw new Error("Resource already taken for run!");
  184. } else {
  185. map.put(id, result);
  186. }
  187. }
  188. }
  189.  
  190. static class TestThread implements Runnable {
  191.  
  192. int run = 0;
  193. final String myID = java.util.UUID.randomUUID().toString();
  194.  
  195. @Override
  196. public void run() {
  197. run++;
  198. boolean gotResource = claimResource("ResourceLockerUtil_TEST", myID, 1);
  199. updateResult(gotResource, run);
  200. if (gotResource) {
  201. System.out.println(myID + "\t\tI GOT THE RESOURCE on run: " + run);
  202. } else {
  203. //System.out.println(myID + "\t\tResource is bussy!on run: " + run);
  204. }
  205. }
  206. }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement