Commit d8a33d2b authored by Mohamad Bashar Desoki's avatar Mohamad Bashar Desoki

init Distributed Queue

parents
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
<component name="ArtifactManager">
<artifact type="jar" name="Consumer">
<output-path>$PROJECT_DIR$/out/artifacts/Consumer</output-path>
<root id="archive" name="DQueue.jar">
<element id="module-output" name="DQueue" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-common/4.1.94.Final/netty-common-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/commons-io/commons-io/2.11.0/commons-io-2.11.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-transport-native-unix-common/4.1.94.Final/netty-transport-native-unix-common-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/ch/qos/logback/logback-core/1.2.10/logback-core-1.2.10.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-boringssl-static/2.0.61.Final/netty-tcnative-boringssl-static-2.0.61.Final-osx-aarch_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-boringssl-static/2.0.61.Final/netty-tcnative-boringssl-static-2.0.61.Final-windows-x86_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-boringssl-static/2.0.61.Final/netty-tcnative-boringssl-static-2.0.61.Final-linux-aarch_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-boringssl-static/2.0.61.Final/netty-tcnative-boringssl-static-2.0.61.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-transport-classes-epoll/4.1.94.Final/netty-transport-classes-epoll-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-boringssl-static/2.0.61.Final/netty-tcnative-boringssl-static-2.0.61.Final-osx-x86_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-buffer/4.1.94.Final/netty-buffer-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-resolver/4.1.94.Final/netty-resolver-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/apache/zookeeper/zookeeper/3.9.1/zookeeper-3.9.1.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/apache/yetus/audience-annotations/0.12.0/audience-annotations-0.12.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-transport/4.1.94.Final/netty-transport-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-classes/2.0.61.Final/netty-tcnative-classes-2.0.61.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-codec/4.1.94.Final/netty-codec-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-handler/4.1.94.Final/netty-handler-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/apache/zookeeper/zookeeper-jute/3.9.1/zookeeper-jute-3.9.1.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-boringssl-static/2.0.61.Final/netty-tcnative-boringssl-static-2.0.61.Final-linux-x86_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/ch/qos/logback/logback-classic/1.2.10/logback-classic-1.2.10.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-transport-native-epoll/4.1.94.Final/netty-transport-native-epoll-4.1.94.Final-linux-x86_64.jar" path-in-jar="/" />
</root>
</artifact>
</component>
\ No newline at end of file
<component name="ArtifactManager">
<artifact type="jar" name="Producer">
<output-path>$PROJECT_DIR$/out/artifacts/Producer</output-path>
<root id="archive" name="DQueue.jar">
<element id="module-output" name="DQueue" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-common/4.1.94.Final/netty-common-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/commons-io/commons-io/2.11.0/commons-io-2.11.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-transport-native-unix-common/4.1.94.Final/netty-transport-native-unix-common-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/ch/qos/logback/logback-core/1.2.10/logback-core-1.2.10.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-boringssl-static/2.0.61.Final/netty-tcnative-boringssl-static-2.0.61.Final-osx-aarch_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-boringssl-static/2.0.61.Final/netty-tcnative-boringssl-static-2.0.61.Final-windows-x86_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-boringssl-static/2.0.61.Final/netty-tcnative-boringssl-static-2.0.61.Final-linux-aarch_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-boringssl-static/2.0.61.Final/netty-tcnative-boringssl-static-2.0.61.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-transport-classes-epoll/4.1.94.Final/netty-transport-classes-epoll-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-boringssl-static/2.0.61.Final/netty-tcnative-boringssl-static-2.0.61.Final-osx-x86_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-buffer/4.1.94.Final/netty-buffer-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-resolver/4.1.94.Final/netty-resolver-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/apache/zookeeper/zookeeper/3.9.1/zookeeper-3.9.1.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/apache/yetus/audience-annotations/0.12.0/audience-annotations-0.12.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-transport/4.1.94.Final/netty-transport-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-classes/2.0.61.Final/netty-tcnative-classes-2.0.61.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-codec/4.1.94.Final/netty-codec-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-handler/4.1.94.Final/netty-handler-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/apache/zookeeper/zookeeper-jute/3.9.1/zookeeper-jute-3.9.1.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-tcnative-boringssl-static/2.0.61.Final/netty-tcnative-boringssl-static-2.0.61.Final-linux-x86_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/ch/qos/logback/logback-classic/1.2.10/logback-classic-1.2.10.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/io/netty/netty-transport-native-epoll/4.1.94.Final/netty-transport-native-epoll-4.1.94.Final-linux-x86_64.jar" path-in-jar="/" />
</root>
</artifact>
</component>
\ No newline at end of file
node_modules/
dist/
vendor/
cache/
.*/
*.min.*
*.test.*
*.spec.*
*.bundle.*
*.bundle-min.*
*.*.js
*.*.ts
*.log
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="com.obiscr.chatgpt.settings.EasyCodeState">
<option name="projectFiles" value="$PROJECT_DIR$/src/main/java/org/ds/interfaces/Queue.java;D:/HIAST Library/Teaching/DS/2025/Labs/Lab9/DQueue/DQueue/src/main/java/org/ds/tester/Consumer.java;D:/HIAST Library/Teaching/DS/2025/Labs/Lab9/DQueue/DQueue/src/main/java/org/ds/tester/Producer.java;D:/HIAST Library/Teaching/DS/2025/Labs/Lab9/DQueue/DQueue/src/main/java/org/ds/utils/Constants.java;D:/HIAST Library/Teaching/DS/2025/Labs/Lab9/DQueue/DQueue/src/main/java/org/ds/utils/DistributedLock.java;D:/HIAST Library/Teaching/DS/2025/Labs/Lab9/DQueue/DQueue/src/main/java/org/ds/utils/Helper.java;D:/HIAST Library/Teaching/DS/2025/Labs/Lab9/DQueue/DQueue/src/main/java/org/ds/utils/LockType.java;D:/HIAST Library/Teaching/DS/2025/Labs/Lab9/DQueue/DQueue/src/main/java/org/ds/utils/QueueFactory.java;D:/HIAST Library/Teaching/DS/2025/Labs/Lab9/DQueue/DQueue/src/main/java/org/ds/LockQueue.java;D:/HIAST Library/Teaching/DS/2025/Labs/Lab9/DQueue/DQueue/src/main/java/org/ds/NoLockQueue.java;D:/HIAST Library/Teaching/DS/2025/Labs/Lab9/DQueue/DQueue/pom.xml" />
<option name="forceFullIndex" value="false" />
<option name="fileSummaryMaps" value="{&quot;D:/HIAST Library/Teaching/DS/2025/Labs/Lab9/DQueue/DQueue&quot;:&quot;{\&quot;/src/main/java/org/ds/LockQueue.java\&quot;:\&quot;This file implements a distributed queue using Apache ZooKeeper. It contains the following classes and functions:\\n\\n`LockQueue`: A class that implements a distributed queue using ZooKeeper, with methods to enqueue and dequeue items.\\n\\n`enqueue(String item)`: A method that creates a new item node in the queue with the given item data.\\n\\n`dequeue()`: A method that retrieves and removes the oldest item from the queue, using a distributed lock to ensure thread-safe access.\\n\\nThe file also imports several classes from the `org.apache.zookeeper` and `org.example` packages, which are likely used to interact with ZooKeeper and provide utility functions for distributed systems.\&quot;,\&quot;/src/main/java/org/ds/tester/Consumer.java\&quot;:\&quot;This file is a Java program that implements a distributed queue using Apache ZooKeeper. It allows consumers to dequeue items from the queue and print them to the console.\\n\\n`Consumer`: The main class that connects to the ZooKeeper instance, creates the necessary nodes, and creates a distributed queue using the `QueueFactory`.\\n\\n`main`: The entry point of the program that initializes the `Consumer` and starts the dequeuing process.\&quot;,\&quot;/src/main/java/org/ds/utils/Helper.java\&quot;:\&quot;This file contains a utility class `Helper` with a single static function `createNodeIfDoesNotExists`.\\n\\n`Helper`: A utility class that provides helper methods for working with Apache ZooKeeper.\\n\\n`createNodeIfDoesNotExists`: Creates a node in ZooKeeper if it doesn\\u0027t already exist.\&quot;,\&quot;/src/main/java/org/ds/tester/Producer.java\&quot;:\&quot;This file is a Java program that demonstrates the usage of a distributed queue implemented using Apache ZooKeeper. It creates a ZooKeeper connection, instantiates a `NoLockQueue` object, and continuously enqueues items into the queue.\\n\\n`Producer`: This class is the main entry point of the program, which sets up the ZooKeeper connection and the distributed queue, and then enqueues items into the queue in a loop.\\n\\n`main(String[] args)`: This function is the main entry point of the `Producer` class, which connects to the ZooKeeper instance, creates a `NoLockQueue` object, and then enqueues items into the queue in a loop.\&quot;,\&quot;/src/main/java/org/ds/utils/QueueFactory.java\&quot;:\&quot;`QueueFactory`: A class that creates either a `LockQueue` or a `NoLockQueue` based on the specified `LockType`.\\n\\n`GetQueue`: A function that returns a `Queue` implementation based on the specified `LockType`.\\n\\nThis file defines a `QueueFactory` class that creates either a `LockQueue` or a `NoLockQueue` based on the specified `LockType`. The `GetQueue` function is used to retrieve the appropriate `Queue` implementation.\&quot;,\&quot;/src/main/java/org/ds/utils/LockType.java\&quot;:\&quot;This file contains an enumeration `LockType` that defines two values: `NoLock` and `Lock`. The purpose of this enumeration is to provide a way to represent different types of locks that can be used in a system.\\n\\n`LockType`: An enumeration that defines two types of locks: `NoLock` and `Lock`.\&quot;,\&quot;/pom.xml\&quot;:\&quot;This file is a Maven project configuration file (pom.xml) for a project named \\\&quot;DQueue\\\&quot;. It specifies the project\\u0027s metadata, such as the group ID, artifact ID, and version, as well as the project\\u0027s dependencies. The file does not contain any classes or functions, but rather configures the project\\u0027s build settings and dependencies.\\n\\nThe main purpose of this file is to manage the project\\u0027s dependencies, specifically the Apache Zookeeper library, version 3.9.1. This library is likely used in the project to provide distributed coordination and synchronization capabilities.\&quot;,\&quot;/src/main/java/org/ds/interfaces/Queue.java\&quot;:\&quot;This file defines an interface named `Queue` that provides methods for enqueuing and dequeuing elements.\\n\\n`Queue`: An interface that defines the basic operations of a queue data structure.\\n\\n`enqueue(String item)`: Adds the specified item to the end of the queue.\\n\\n`dequeue()`: Removes and returns the element at the front of the queue.\&quot;,\&quot;/src/main/java/org/ds/utils/Constants.java\&quot;:\&quot;This file contains a single class named `Constants` that defines several constants used throughout the application.\\n\\n`Constants`: A class that defines various constants used in the application, such as the root node for locks, the queue path, and the Zookeeper URL.\\n\\nThe file does not contain any functions, but rather defines several static variables that are used to store configuration-related information for the application.\&quot;,\&quot;/src/main/java/org/ds/NoLockQueue.java\&quot;:\&quot;This file contains a single class, `NoLockQueue`, which implements a queue data structure using Apache ZooKeeper without the need for distributed locks.\\n\\n`NoLockQueue`: A queue implementation that uses ZooKeeper to store and retrieve items without the need for distributed locks.\\n\\n`NoLockQueue(ZooKeeper, String)`: Initializes the `NoLockQueue` instance with a ZooKeeper client and the path to the queue in ZooKeeper.\\n\\n`enqueue(String)`: Adds an item to the queue by creating a new node in ZooKeeper with the item data.\\n\\n`dequeue()`: Retrieves and removes the oldest item from the queue by sorting the list of item nodes in ZooKeeper and deleting the first one.\&quot;,\&quot;/src/main/java/org/ds/utils/DistributedLock.java\&quot;:\&quot;`DistributedLock`: A class that implements a distributed lock using Apache ZooKeeper.\\n\\n`DistributedLock(ZooKeeper, String)`: Constructs a `DistributedLock` instance with the provided ZooKeeper client and lock path.\\n\\n`lock()`: Attempts to acquire the distributed lock by creating an ephemeral sequential znode under the lock path and checking if it is the first in the list.\\n\\n`unlock()`: Releases the distributed lock by deleting the lock node.\\n\\nThis file provides a utility class `DistributedLock` that allows multiple processes to coordinate access to a shared resource using a distributed lock mechanism backed by Apache ZooKeeper.\&quot;}&quot;}" />
<option name="fileSkeletonMaps" value="{&quot;D:/HIAST Library/Teaching/DS/2025/Labs/Lab9/DQueue/DQueue&quot;:&quot;{\&quot;/src/main/java/org/ds/interfaces/Queue.java\&quot;:{\&quot;file_path\&quot;:\&quot;/src/main/java/org/ds/interfaces/Queue.java\&quot;,\&quot;file_name\&quot;:\&quot;Queue.java\&quot;,\&quot;methods\&quot;:[{\&quot;method_name\&quot;:\&quot;enqueue\&quot;,\&quot;method_parameters\&quot;:\&quot;(String item)\&quot;,\&quot;parameter_declarations\&quot;:[{\&quot;type\&quot;:\&quot;String\&quot;,\&quot;name\&quot;:\&quot;item\&quot;}],\&quot;return_type\&quot;:\&quot;void\&quot;,\&quot;scope\&quot;:[[3,4],[3,54]]},{\&quot;method_name\&quot;:\&quot;dequeue\&quot;,\&quot;method_parameters\&quot;:\&quot;()\&quot;,\&quot;parameter_declarations\&quot;:[],\&quot;return_type\&quot;:\&quot;String\&quot;,\&quot;scope\&quot;:[[4,4],[4,45]]}]},\&quot;/src/main/java/org/ds/tester/Consumer.java\&quot;:{\&quot;file_path\&quot;:\&quot;/src/main/java/org/ds/tester/Consumer.java\&quot;,\&quot;file_name\&quot;:\&quot;Consumer.java\&quot;,\&quot;methods\&quot;:[{\&quot;method_name\&quot;:\&quot;main\&quot;,\&quot;method_parameters\&quot;:\&quot;(String[] args)\&quot;,\&quot;parameter_declarations\&quot;:[{\&quot;type\&quot;:\&quot;\&quot;,\&quot;name\&quot;:\&quot;args\&quot;}],\&quot;return_type\&quot;:\&quot;void\&quot;,\&quot;scope\&quot;:[[10,4],[38,5]]}]},\&quot;/src/main/java/org/ds/tester/Producer.java\&quot;:{\&quot;file_path\&quot;:\&quot;/src/main/java/org/ds/tester/Producer.java\&quot;,\&quot;file_name\&quot;:\&quot;Producer.java\&quot;,\&quot;methods\&quot;:[{\&quot;method_name\&quot;:\&quot;main\&quot;,\&quot;method_parameters\&quot;:\&quot;(String[] args)\&quot;,\&quot;parameter_declarations\&quot;:[{\&quot;type\&quot;:\&quot;\&quot;,\&quot;name\&quot;:\&quot;args\&quot;}],\&quot;return_type\&quot;:\&quot;void\&quot;,\&quot;scope\&quot;:[[8,4],[24,5]]}]},\&quot;/src/main/java/org/ds/utils/Constants.java\&quot;:{\&quot;file_path\&quot;:\&quot;/src/main/java/org/ds/utils/Constants.java\&quot;,\&quot;file_name\&quot;:\&quot;Constants.java\&quot;,\&quot;methods\&quot;:[]},\&quot;/src/main/java/org/ds/utils/DistributedLock.java\&quot;:{\&quot;file_path\&quot;:\&quot;/src/main/java/org/ds/utils/DistributedLock.java\&quot;,\&quot;file_name\&quot;:\&quot;DistributedLock.java\&quot;,\&quot;methods\&quot;:[{\&quot;method_name\&quot;:\&quot;DistributedLock\&quot;,\&quot;method_parameters\&quot;:\&quot;(ZooKeeper zk, String path)\&quot;,\&quot;parameter_declarations\&quot;:[{\&quot;type\&quot;:\&quot;ZooKeeper\&quot;,\&quot;name\&quot;:\&quot;zk\&quot;},{\&quot;type\&quot;:\&quot;String\&quot;,\&quot;name\&quot;:\&quot;path\&quot;}],\&quot;return_type\&quot;:\&quot;\&quot;,\&quot;scope\&quot;:[[17,4],[22,5]]},{\&quot;method_name\&quot;:\&quot;lock\&quot;,\&quot;method_parameters\&quot;:\&quot;()\&quot;,\&quot;parameter_declarations\&quot;:[],\&quot;return_type\&quot;:\&quot;boolean\&quot;,\&quot;scope\&quot;:[[24,4],[45,5]]},{\&quot;method_name\&quot;:\&quot;unlock\&quot;,\&quot;method_parameters\&quot;:\&quot;()\&quot;,\&quot;parameter_declarations\&quot;:[],\&quot;return_type\&quot;:\&quot;void\&quot;,\&quot;scope\&quot;:[[47,4],[49,5]]}]},\&quot;/src/main/java/org/ds/utils/Helper.java\&quot;:{\&quot;file_path\&quot;:\&quot;/src/main/java/org/ds/utils/Helper.java\&quot;,\&quot;file_name\&quot;:\&quot;Helper.java\&quot;,\&quot;methods\&quot;:[{\&quot;method_name\&quot;:\&quot;createNodeIfDoesNotExists\&quot;,\&quot;method_parameters\&quot;:\&quot;(ZooKeeper zooKeeper, String nodePath)\&quot;,\&quot;parameter_declarations\&quot;:[{\&quot;type\&quot;:\&quot;ZooKeeper\&quot;,\&quot;name\&quot;:\&quot;zooKeeper\&quot;},{\&quot;type\&quot;:\&quot;String\&quot;,\&quot;name\&quot;:\&quot;nodePath\&quot;}],\&quot;return_type\&quot;:\&quot;void\&quot;,\&quot;scope\&quot;:[[14,4],[22,5]]}]},\&quot;/src/main/java/org/ds/utils/LockType.java\&quot;:{\&quot;file_path\&quot;:\&quot;/src/main/java/org/ds/utils/LockType.java\&quot;,\&quot;file_name\&quot;:\&quot;LockType.java\&quot;,\&quot;methods\&quot;:[]},\&quot;/src/main/java/org/ds/utils/QueueFactory.java\&quot;:{\&quot;file_path\&quot;:\&quot;/src/main/java/org/ds/utils/QueueFactory.java\&quot;,\&quot;file_name\&quot;:\&quot;QueueFactory.java\&quot;,\&quot;methods\&quot;:[{\&quot;method_name\&quot;:\&quot;QueueFactory\&quot;,\&quot;method_parameters\&quot;:\&quot;(ZooKeeper zk, String queuePath)\&quot;,\&quot;parameter_declarations\&quot;:[{\&quot;type\&quot;:\&quot;ZooKeeper\&quot;,\&quot;name\&quot;:\&quot;zk\&quot;},{\&quot;type\&quot;:\&quot;String\&quot;,\&quot;name\&quot;:\&quot;queuePath\&quot;}],\&quot;return_type\&quot;:\&quot;\&quot;,\&quot;scope\&quot;:[[12,4],[15,5]]},{\&quot;method_name\&quot;:\&quot;GetQueue\&quot;,\&quot;method_parameters\&quot;:\&quot;(LockType lockType)\&quot;,\&quot;parameter_declarations\&quot;:[{\&quot;type\&quot;:\&quot;LockType\&quot;,\&quot;name\&quot;:\&quot;lockType\&quot;}],\&quot;return_type\&quot;:\&quot;Queue\&quot;,\&quot;scope\&quot;:[[17,4],[22,5]]}]},\&quot;/src/main/java/org/ds/LockQueue.java\&quot;:{\&quot;file_path\&quot;:\&quot;/src/main/java/org/ds/LockQueue.java\&quot;,\&quot;file_name\&quot;:\&quot;LockQueue.java\&quot;,\&quot;methods\&quot;:[{\&quot;method_name\&quot;:\&quot;LockQueue\&quot;,\&quot;method_parameters\&quot;:\&quot;(ZooKeeper zk, String queuePath)\&quot;,\&quot;parameter_declarations\&quot;:[{\&quot;type\&quot;:\&quot;ZooKeeper\&quot;,\&quot;name\&quot;:\&quot;zk\&quot;},{\&quot;type\&quot;:\&quot;String\&quot;,\&quot;name\&quot;:\&quot;queuePath\&quot;}],\&quot;return_type\&quot;:\&quot;\&quot;,\&quot;scope\&quot;:[[13,4],[17,5]]},{\&quot;method_name\&quot;:\&quot;enqueue\&quot;,\&quot;method_parameters\&quot;:\&quot;(String item)\&quot;,\&quot;parameter_declarations\&quot;:[{\&quot;type\&quot;:\&quot;String\&quot;,\&quot;name\&quot;:\&quot;item\&quot;}],\&quot;return_type\&quot;:\&quot;void\&quot;,\&quot;scope\&quot;:[[19,4],[23,5]]},{\&quot;method_name\&quot;:\&quot;dequeue\&quot;,\&quot;method_parameters\&quot;:\&quot;()\&quot;,\&quot;parameter_declarations\&quot;:[],\&quot;return_type\&quot;:\&quot;String\&quot;,\&quot;scope\&quot;:[[25,4],[67,5]]}]},\&quot;/src/main/java/org/ds/NoLockQueue.java\&quot;:{\&quot;file_path\&quot;:\&quot;/src/main/java/org/ds/NoLockQueue.java\&quot;,\&quot;file_name\&quot;:\&quot;NoLockQueue.java\&quot;,\&quot;methods\&quot;:[{\&quot;method_name\&quot;:\&quot;NoLockQueue\&quot;,\&quot;method_parameters\&quot;:\&quot;(ZooKeeper zk, String queuePath)\&quot;,\&quot;parameter_declarations\&quot;:[{\&quot;type\&quot;:\&quot;ZooKeeper\&quot;,\&quot;name\&quot;:\&quot;zk\&quot;},{\&quot;type\&quot;:\&quot;String\&quot;,\&quot;name\&quot;:\&quot;queuePath\&quot;}],\&quot;return_type\&quot;:\&quot;\&quot;,\&quot;scope\&quot;:[[12,4],[16,5]]},{\&quot;method_name\&quot;:\&quot;enqueue\&quot;,\&quot;method_parameters\&quot;:\&quot;(String item)\&quot;,\&quot;parameter_declarations\&quot;:[{\&quot;type\&quot;:\&quot;String\&quot;,\&quot;name\&quot;:\&quot;item\&quot;}],\&quot;return_type\&quot;:\&quot;void\&quot;,\&quot;scope\&quot;:[[18,4],[22,5]]},{\&quot;method_name\&quot;:\&quot;dequeue\&quot;,\&quot;method_parameters\&quot;:\&quot;()\&quot;,\&quot;parameter_declarations\&quot;:[],\&quot;return_type\&quot;:\&quot;String\&quot;,\&quot;scope\&quot;:[[24,4],[38,5]]}]},\&quot;/pom.xml\&quot;:{\&quot;file_path\&quot;:\&quot;/pom.xml\&quot;,\&quot;file_name\&quot;:\&quot;pom.xml\&quot;,\&quot;methods\&quot;:[]}}&quot;}" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ArtifactsWorkspaceSettings">
<artifacts-to-build>
<artifact name="Consumer" />
<artifact name="Producer" />
</artifacts-to-build>
</component>
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="1cd303de-8197-4240-98f1-ed0399d5054e" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/artifacts/Consumer.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/artifacts/Producer.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/easycode.ignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/easycode/codebase-v2.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/encodings.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/org/ds/LockQueue.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/org/ds/NoLockQueue.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/org/ds/interfaces/Queue.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/org/ds/tester/Consumer.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/org/ds/tester/Producer.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/org/ds/utils/Constants.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/org/ds/utils/DistributedLock.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/org/ds/utils/Helper.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/org/ds/utils/LockType.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/org/ds/utils/QueueFactory.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/resources/logback.xml" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="ProjectColorInfo">{
&quot;associatedIndex&quot;: 7
}</component>
<component name="ProjectId" id="2pT3WtKEjgINHUhCUNJ3g0o52pl" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"Application.C1.executor": "Run",
"Application.C2.executor": "Run",
"Application.Consumer.executor": "Run",
"Application.P1.executor": "Run",
"Application.P2.executor": "Run",
"Application.Producer.executor": "Run",
"RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
"git-widget-placeholder": "master",
"kotlin-language-version-configured": "true",
"last_opened_file_path": "D:/HIAST Library/Teaching/DS/2025/Labs/Lab9/DQueue/DQueue/src/main/resources",
"project.structure.last.edited": "Artifacts",
"project.structure.proportion": "0.15",
"project.structure.side.proportion": "0.2850575",
"settings.editor.selected.configurable": "preferences.lookFeel"
}
}]]></component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="D:\HIAST Library\Teaching\DS\2025\Labs\Lab9\DQueue\DQueue\src\main\resources" />
<recent name="D:\HIAST Library\Teaching\DS\2025\Labs\Lab9\DQueue\DQueue\src\main\java\org\ds" />
</key>
</component>
<component name="RunManager" selected="Application.C2">
<configuration name="C1" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="org.ds.tester.Consumer" />
<module name="DQueue" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="C2" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="org.ds.tester.Consumer" />
<module name="DQueue" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Producer" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="org.ds.tester.Producer" />
<module name="DQueue" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<list>
<item itemvalue="Application.Producer" />
<item itemvalue="Application.C1" />
<item itemvalue="Application.C2" />
</list>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="1cd303de-8197-4240-98f1-ed0399d5054e" name="Changes" comment="" />
<created>1732776272916</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1732776272916</updated>
</task>
<servers />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.ds</groupId>
<artifactId>DQueue</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package org.ds;
import org.apache.zookeeper.*;
import org.ds.interfaces.Queue;
import org.ds.utils.DistributedLock;
import org.ds.utils.Helper;
import java.util.List;
public class LockQueue implements Queue {
private ZooKeeper zk;
private String queuePath;
public LockQueue(ZooKeeper zk, String queuePath) throws InterruptedException, KeeperException {
this.zk = zk;
this.queuePath = queuePath;
Helper.createNodeIfDoesNotExists(zk, queuePath);
}
public void enqueue(String item) throws Exception {
// Create an item node under the queue node with the item data
}
public String dequeue() throws Exception {
return "";
}
}
package org.ds;
import org.apache.zookeeper.*;
import org.ds.interfaces.Queue;
import org.ds.utils.Helper;
import java.util.List;
public class NoLockQueue implements Queue {
private ZooKeeper zk;
private String queuePath;
public NoLockQueue(ZooKeeper zk, String queuePath) throws InterruptedException, KeeperException {
this.zk = zk;
this.queuePath = queuePath;
Helper.createNodeIfDoesNotExists(zk, queuePath);
}
public void enqueue(String item) throws Exception {
// Create an item node under the queue node with the item data
}
public String dequeue() throws Exception {
return "";
}
}
package org.ds.interfaces;
public interface Queue {
public void enqueue(String item) throws Exception;
public String dequeue() throws Exception;
}
package org.ds.tester;
import org.apache.zookeeper.ZooKeeper;
import org.ds.interfaces.Queue;
import org.ds.utils.Constants;
import org.ds.utils.Helper;
import org.ds.utils.LockType;
import org.ds.utils.QueueFactory;
public class Consumer {
public static void main(String[] args) throws Exception
{
LockType lockType = LockType.Lock;
if(args.length > 0){
String lock = args[0];
lockType = LockType.valueOf(lock);
}
// Connect to the ZooKeeper instance running on localhost:2181
ZooKeeper zk = new ZooKeeper(Constants.zookeeperUrl, 20000, null);
Helper.createNodeIfDoesNotExists(zk, Constants.queuePath);
if(lockType == LockType.Lock){
Helper.createNodeIfDoesNotExists(zk, Constants.lockRootNode);
}
QueueFactory queueFactory = new QueueFactory(zk, Constants.queuePath);
// Create a distributed queue
Queue queue = queueFactory.GetQueue(lockType);
while (true){
String dequeValue = queue.dequeue();
if(dequeValue != null){
System.out.println(dequeValue);
}
}
}
}
package org.ds.tester;
import org.apache.zookeeper.ZooKeeper;
import org.ds.NoLockQueue;
import org.ds.interfaces.Queue;
import org.ds.utils.Constants;
public class Producer {
public static void main(String[] args) throws Exception {
// Connect to the ZooKeeper instance running on localhost:2181
ZooKeeper zk = new ZooKeeper(Constants.zookeeperUrl, 20000, null);
// Create a distributed queue and enqueue items
Queue queue = new NoLockQueue(zk, Constants.queuePath);
int i = 0;
while (i < Integer.MAX_VALUE){
System.out.println("going to produce item"+i);
queue.enqueue("item"+i);
Thread.sleep(1000);
i++;
}
// Close the ZooKeeper connection
zk.close();
}
}
package org.ds.utils;
public class Constants {
// The root node of the lock hierarchy
public static String lockRootNode = "/lock";
// Define the queue path and create necessary nodes
public static String queuePath = "/queue";
// Defines the zookeeper endpoint
public static String zookeeperUrl = "172.29.3.101:2181";
}
package org.ds.utils;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import java.util.List;
public class DistributedLock {
// The ZooKeeper client instance
private final ZooKeeper zk;
// The path of the node where the lock is created
private final String nodePath;
// The name of the lock node
private String lockNode;
public DistributedLock(ZooKeeper zk, String path) throws InterruptedException, KeeperException {
this.zk = zk;
this.nodePath = Constants.lockRootNode + path;
Helper.createNodeIfDoesNotExists(zk, Constants.lockRootNode);
Helper.createNodeIfDoesNotExists(zk, this.nodePath);
}
public boolean lock() throws InterruptedException, KeeperException {
// Create an ephemeral sequential znode under the lock znode
lockNode = zk.create(nodePath + "/lock-", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
// Get the list of all znodes under the lock znode
List<String> children = zk.getChildren(nodePath, false);
// Sort the list of znodes in ascending order
children.sort(String::compareTo);
// Get the index of the lock node in the list
int index = children.indexOf(lockNode.substring(lockNode.lastIndexOf('/') + 1));
// If the lock node is the first one in the list, then we have acquired the lock
if(index == 0){
return true;
}else{
zk.delete(lockNode, -1 );
return false;
}
}
public void unlock() throws InterruptedException, KeeperException {
zk.delete(lockNode, -1);
}
}
package org.ds.utils;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
public class Helper {
/**
* Create a node in ZooKeeper if it doesn't exist.
*
* @param zooKeeper the ZooKeeper instance to use
* @param nodePath the path of the node to create
*/
public static void createNodeIfDoesNotExists(ZooKeeper zooKeeper, String nodePath) {
try {
if (zooKeeper.exists(nodePath, false) == null) {
zooKeeper.create(nodePath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
} catch (Exception e) {
}
}
}
package org.ds.utils;
public enum LockType {
NoLock,
Lock,
}
package org.ds.utils;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.ds.LockQueue;
import org.ds.NoLockQueue;
import org.ds.interfaces.Queue;
public class QueueFactory {
private ZooKeeper zk;
private String queuePath;
public QueueFactory(ZooKeeper zk, String queuePath) {
this.zk = zk;
this.queuePath = queuePath;
}
public Queue GetQueue(LockType lockType) throws InterruptedException, KeeperException {
if(LockType.Lock == lockType){
return new LockQueue(this.zk, this.queuePath);
}
return new NoLockQueue(this.zk, this.queuePath);
}
}
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%kvp- %msg%n</pattern>
</encoder>
</appender>
<root level="error">
<appender-ref ref="STDOUT" />
</root>
</configuration>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment