{"id":27211137,"url":"https://github.com/prat-man/unique4j","last_synced_at":"2025-04-10T01:28:31.775Z","repository":{"id":39944336,"uuid":"221760856","full_name":"prat-man/unique4j","owner":"prat-man","description":"Java library to allow only single instance of a java application to run and enable communication between first instance and subsequent instances","archived":false,"fork":false,"pushed_at":"2023-08-12T09:29:19.000Z","size":550,"stargazers_count":13,"open_issues_count":4,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-03-17T19:03:31.670Z","etag":null,"topics":["java","lock","single-instance","single-instance-app","socket","sockets"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/prat-man.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-11-14T18:22:33.000Z","updated_at":"2023-12-09T19:37:39.000Z","dependencies_parsed_at":"2022-09-12T10:51:03.447Z","dependency_job_id":null,"html_url":"https://github.com/prat-man/unique4j","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prat-man%2Funique4j","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prat-man%2Funique4j/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prat-man%2Funique4j/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prat-man%2Funique4j/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prat-man","download_url":"https://codeload.github.com/prat-man/unique4j/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248139917,"owners_count":21054200,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","lock","single-instance","single-instance-app","socket","sockets"],"created_at":"2025-04-10T01:28:31.153Z","updated_at":"2025-04-10T01:28:31.743Z","avatar_url":"https://github.com/prat-man.png","language":"Java","readme":"# Unique4j\n\n![unique4j logo](unique4j.png)\n\n\u003cbr/\u003e\n\n## Introduction\n\nUnique4j is a cross-platform Java library to allow only single instance of a Java application to run and enable communication between first instance and subsequent instances.\n\nIt is compatible with Java 1.6+ and is platform independent.\n\n\u003cbr\u003e\n\n## Dependency Management\n\n### Maven\n\n    \u003cdependency\u003e\n        \u003cgroupId\u003etk.pratanumandal\u003c/groupId\u003e\n        \u003cartifactId\u003eunique4j\u003c/artifactId\u003e\n        \u003cversion\u003e1.4\u003c/version\u003e\n    \u003c/dependency\u003e\n\n### Gradle\n\n    dependencies {\n        implementation 'tk.pratanumandal:unique4j:1.4'\n    }\n\n\u003cbr\u003e\n\n## How To Use\n\nDeclare an application unique ID which is a common constant for all the instances. This ID must be as unique as possible. A good strategy is to use the entire package name (group ID + artifact ID) along with some random characters.\n\n    // unique application ID\n    public static String APP_ID = \"tk.pratanumandal.unique4j-mlsdvo-20191511-#j.6\";\n\n\u003cbr\u003e\n\nCreate an instance of \u003ccode\u003eUnique\u003c/code\u003e class.\n\n    // create unique instance\n    Unique4j unique = new Unique4j(APP_ID) {\n        @Override\n        public void receiveMessage(String message) {\n            // print received message (timestamp)\n            System.out.println(message);\n        }\n\n        @Override\n        public String sendMessage() {\n            // send timestamp as message\n            Timestamp ts = new Timestamp(new Date().getTime());\n            return \"Another instance launch attempted: \" + ts.toString();\n        }\n        \n        /* It is not mandatory to override this method\n         * By default, the stack trace is printed\n         */\n        @Override\n        public void handleException(Exception exception) {\n            // display the exception message\n            System.out.println(exception.getMessage());\n        }\n\n        /* It is not mandatory to override this method\n         * By default, the subsequent instance simply exits\n         * This method is not invoked if AUTO_EXIT is turned off\n         */\n        @Override\n        public void beforeExit() {\n            // display exit message\n            System.out.println(\"Exiting subsequent instance.\");\n        }\n    };\n   \n\u003cbr\u003e\n\nAlternatively, you can declare to turn off automatic exit for subsequent instances.\n\n    // create unique instance with AUTO_EXIT turned off\n    Unique4j unique = new Unique4j(APP_ID,\n                                   false) // second parameter is for AUTO_EXIT (false turns it off)\n    { \n        ...\n        // Note: beforeExit() method, even if overridden, is never invoked if AUTO_EXIT is turned off\n    }\n    \n\u003cbr\u003e\n\nSending list of strings instead of a single string message.\n    \n    // create Unique4j instance\n    Unique4j unique = new Unique4jList(APP_ID) {\n        @Override\n        protected List\u003cString\u003e sendMessageList() {\n            List\u003cString\u003e messageList = new ArrayList\u003cString\u003e();\n\n            messageList.add(\"Message 1\");\n            messageList.add(\"Message 2\");\n            messageList.add(\"Message 3\");\n            messageList.add(\"Message 4\");\n\n            return messageList;\n        }\n\n        @Override\n        protected void receiveMessageList(List\u003cString\u003e messageList) {\n            for (String message : messageList) {\n                System.out.println(message);\n            }\n        }\n    };\n\n\u003cbr\u003e\n\nSending map of string key-value pairs instead of a single string message.\n    \n    // create Unique4j instance\n    Unique4j unique = new Unique4jMap(APP_ID) {\n        @Override\n        protected Map\u003cString, String\u003e sendMessageMap() {\n            Map\u003cString, String\u003e messageMap = new HashMap\u003cString, String\u003e();\n            \n            messageMap.put(\"key1\", \"Message 1\");\n            messageMap.put(\"key2\", \"Message 2\");\n            messageMap.put(\"key3\", \"Message 3\");\n            messageMap.put(\"key4\", \"Message 4\");\n            \n            return messageMap;\n        }\n    \n        @Override\n        protected void receiveMessageMap(Map\u003cString, String\u003e messageMap) {\n            for (Entry\u003cString, String\u003e entry : messageMap.entrySet()) {\n                System.out.println(entry.getKey() + \" : \" + entry.getValue());\n            }\n        }\n    };\n\n\u003cbr\u003e\n\nTry to obtain a lock using the \u003ccode\u003eUnique\u003c/code\u003e object.\n    \n    // try to obtain lock\n    boolean lockFlag = false;\n    try {\n        lockFlag = unique.acquireLock();\n    } catch (Unique4jException e) {\n        e.printStackTrace();\n    }\n    \n    // perform long running tasks here\n    \n\u003cbr\u003e\n\nFree the lock using the \u003ccode\u003eUnique\u003c/code\u003e object.\n    \n    // long running tasks end here\n    \n    // try to free the lock before exiting program\n    boolean lockFreeFlag = false;\n    try {\n        lockFreeFlag = unique.freeLock();\n    } catch (Unique4jException e) {\n        e.printStackTrace();\n    }\n\n\u003cbr\u003e\n\n## Demonstration\n\nTo put it all together, the following is a simple example of the basic usage of Unique4j.\n\n    import java.sql.Timestamp;\n    import java.util.Date;\n    \n    import tk.pratanumandal.unique4j.Unique4j;\n    import tk.pratanumandal.unique4j.exception.Unique4jException;\n    \n    public class Unique4jDemo {\n    \n        // unique application ID\n        public static String APP_ID = \"tk.pratanumandal.unique4j-mlsdvo-20191511-#j.6\";\n\n        public static void main(String[] args) throws Unique4jException, InterruptedException {\n\n            // create unique instance\n            Unique4j unique = new Unique4j(APP_ID) {\n                @Override\n                public void receiveMessage(String message) {\n                    // print received message (timestamp)\n                    System.out.println(message);\n                }\n\n                @Override\n                public String sendMessage() {\n                    // send timestamp as message\n                    Timestamp ts = new Timestamp(new Date().getTime());\n                    return \"Another instance launch attempted: \" + ts.toString();\n                }\n\n                @Override\n                public void handleException(Exception exception) {  // this method is optional\n                    // display the exception message\n                    System.out.println(exception.getMessage());\n                }\n\n                @Override\n                public void beforeExit() {  // this method is optional\n                    // display exit message\n                    System.out.println(\"Exiting subsequent instance.\");\n                }\n            };\n\n            // try to obtain lock\n            boolean lockFlag = unique.acquireLock();\n\n            // sleep the main thread for 30 seconds to simulate long running tasks\n            Thread.sleep(30000);\n\n            // try to free the lock before exiting program\n            boolean lockFreeFlag = unique.freeLock();\n\n        }\n\t\n    }\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprat-man%2Funique4j","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprat-man%2Funique4j","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprat-man%2Funique4j/lists"}