Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.zirius.db;
- import java.util.ArrayList;
- import java.util.HashMap;
- /**
- *
- * @author Petter Holone <petter@zirius.no>
- */
- public class ResourceLockerUtil {
- public static final String TABLE = "system_uppgrade";
- private static final String prefix = "RES_LOCKER_";
- private static final String LOCK_WAIT_TIME_KEY = "ResourceLockerUtilWaitTime";
- private static final int DEFAULT_WAIT_TIME = 2000;
- private static int waitTime = 0;
- private static boolean isResourceFreeForMe(String resourceID, String lockID) {
- {//SQL Query
- final com.zirius.db.jData sql = new com.zirius.db.jData();
- try {
- sql.query("SELECT TID,VERDI FROM " + TABLE + " WHERE ID = '" + resourceID + "' and TID > NOW()");
- if (sql.gotoFirst()) {
- String machine = sql.getPropertyString("VERDI");
- return machine.equals(lockID);
- }
- return true;
- } finally {
- sql.close();
- }
- }
- }
- public static boolean claimResource(String resourceID, String lockID, long minutes) {
- if (minutes < 1l) {
- throw new IllegalArgumentException("minutes < 1");
- }
- if (resourceID == null || resourceID.length() < 0) {
- throw new IllegalArgumentException("resourceID can not be empty");
- }
- if (resourceID.length() > 80) {
- throw new IllegalArgumentException("resourceID can not be longer than 80 characters");
- }
- if (lockID == null || lockID.length() < 0) {
- throw new IllegalArgumentException("lockID can not be empty");
- }
- if (lockID.length() > 100) {
- throw new IllegalArgumentException("lockID can not be longer than 100 characters");
- }
- resourceID = prefix + resourceID;
- if (isResourceFreeForMe(resourceID, lockID)) {
- {//SQL Query
- final com.zirius.db.jData sql = new com.zirius.db.jData();
- try {
- sql.queryUpdateInt("INSERT INTO " + TABLE + " VALUES('" + resourceID + "',1,NOW() + INTERVAL " + minutes + " MINUTE,'" + lockID + "')"
- + " ON DUPLICATE KEY UPDATE TID=NOW()+INTERVAL " + minutes + " MINUTE, VERDI='" + lockID + "'");
- } finally {
- sql.close();
- }
- }
- try {
- Thread.sleep(getWaitTime());
- } catch (InterruptedException ex) {
- ex.printStackTrace();
- }
- if (isResourceFreeForMe(resourceID, lockID)) {
- return true;
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- public static void forceFreeResource(String resourceID) {
- {//SQL Query
- final com.zirius.db.jData sql = new com.zirius.db.jData();
- try {
- sql.queryUpdateInt("UPDATE system_uppgrade SET TID = NOW() WHERE ID='" + resourceID + "'");
- } finally {
- sql.close();
- }
- }
- }
- public static void freeResource(String resourceID, String lockID) {
- if (isResourceFreeForMe(resourceID, lockID)) {
- {//SQL Query
- final com.zirius.db.jData sql = new com.zirius.db.jData();
- try {
- sql.queryUpdateInt("UPDATE system_uppgrade SET TID = NOW() WHERE ID='" + resourceID + "' AND VERDI='" + lockID + "'");
- } finally {
- sql.close();
- }
- }
- }
- }
- public static int getWaitTime() {
- if (waitTime == 0) {
- {//SQL Query
- final com.zirius.db.jData sql = new com.zirius.db.jData();
- try {
- final StringBuffer query = new StringBuffer(200);
- sql.query("select UTFORT from " + TABLE + " where ID = '" + LOCK_WAIT_TIME_KEY + "'");
- if (sql.gotoFirst()) {
- do {
- int wait = sql.getPropertyint("UTFORT");
- if (wait == 0) {
- waitTime = DEFAULT_WAIT_TIME;
- } else {
- waitTime = wait;
- }
- } while (sql.gotoNext());
- }
- } catch (Throwable t) {
- waitTime = DEFAULT_WAIT_TIME;
- } finally {
- sql.close();
- }
- }
- }
- return waitTime;
- }
- public static void main(String[] args) throws InterruptedException {
- {//init server db
- final String serveraddress = "localhost";
- final String sqlUser = "root";
- final String sqlPass = "root";
- final String client = "duoshopas";
- final String clientUser = "test";
- com.zirius.core.Workbench.server = true;
- com.zirius.olfi.domain.Constants.currentserver = new com.zirius.db.Server(client, serveraddress, sqlUser, sqlPass, client);
- com.zirius.db.pool.init();
- com.zirius.db.DBService dbservice = new com.zirius.db.DBService(com.zirius.dataObjects.firmaObject.class);
- dbservice.hente(com.zirius.db.DBService.DATA_FIRST, null);
- com.zirius.core.Workbench.setFirma((com.zirius.dataObjects.firmaObject) dbservice.getModel());
- com.zirius.core.Workbench.setUser(com.zirius.olfi.modules.user.utils.UserUtil.getUserByUsername(clientUser));
- }
- ArrayList<TestThread> testThread = new ArrayList();
- for (int j = 0; j < 50; j++) {
- testThread.add(new TestThread());
- }
- for (int i = 0; i < 500; i++) {
- {//SQL Query
- final com.zirius.db.jData sql = new com.zirius.db.jData();
- try {
- forceFreeResource("ResourceLockerUtil_TEST");
- if (sql.gotoFirst()) {
- do {
- } while (sql.gotoNext());
- }
- } finally {
- sql.close();
- }
- }
- Thread.sleep(1000l);
- System.out.println("\n\nSpør etter ressurs. En bør få den\n\n");
- for (TestThread t : testThread) {
- new Thread(t).start();
- }
- Thread.sleep(6000l);
- }
- }
- static HashMap<Integer, Boolean> map = new HashMap();
- static synchronized void updateResult(boolean result, Integer id) {
- if (result) {
- if (map.get(id) != null) {
- throw new Error("Resource already taken for run!");
- } else {
- map.put(id, result);
- }
- }
- }
- static class TestThread implements Runnable {
- int run = 0;
- final String myID = java.util.UUID.randomUUID().toString();
- @Override
- public void run() {
- run++;
- boolean gotResource = claimResource("ResourceLockerUtil_TEST", myID, 1);
- updateResult(gotResource, run);
- if (gotResource) {
- System.out.println(myID + "\t\tI GOT THE RESOURCE on run: " + run);
- } else {
- //System.out.println(myID + "\t\tResource is bussy!on run: " + run);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement