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
This diff is collapsed.
<?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