aboutsummaryrefslogtreecommitdiff
path: root/src/Store/Book.java
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.com>2024-11-21 23:54:32 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.com>2024-11-21 23:54:32 +0530
commit3b16f36d497151ea1628491a057da292855a46f1 (patch)
tree65f15c93f550f86a64ff58816a3c8dbf11f6cc3c /src/Store/Book.java
parent279415dfe3386e624727d204b304430299d4a6cf (diff)
added multithreading because... yesHEADmaster
Diffstat (limited to 'src/Store/Book.java')
-rw-r--r--src/Store/Book.java72
1 files changed, 50 insertions, 22 deletions
diff --git a/src/Store/Book.java b/src/Store/Book.java
index 4232d77..fe2d63e 100644
--- a/src/Store/Book.java
+++ b/src/Store/Book.java
@@ -164,38 +164,66 @@ public class Book {
public static DataListener<JSONMessage> getListHandler(boolean availableOnly) {
return ((client, data, ackSender) -> {
- ArrayList<Book> books = getAll(availableOnly);
- client.sendEvent(availableOnly ? "availableBooksList" : "allBooksList", books);
+ Thread t = new Thread(() -> {
+ ArrayList<Book> books = null;
+ try {
+ books = getAll(availableOnly);
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ client.sendEvent(availableOnly ? "availableBooksList" : "allBooksList", books);
+ });
+
+ t.start();
+ t.join();
});
}
public static DataListener<JSONMessage> issueHandler() {
return ((client, data, ackSender) -> {
- Map d = data.getData();
-
- int userId = (int) d.get("id");
- Book b = new Book((int) d.get("bookId"));
-
- if (b.issue(userId)) {
- client.sendEvent("issued");
- } else {
- client.sendEvent("alreadyIssued");
- }
+ Thread t = new Thread(() -> {
+ Map d = data.getData();
+
+ int userId = (int) d.get("id");
+ Book b = new Book((int) d.get("bookId"));
+
+ try {
+ if (b.issue(userId)) {
+ client.sendEvent("issued");
+ } else {
+ client.sendEvent("alreadyIssued");
+ }
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ });
+
+ t.start();
+ t.join();
});
}
public static DataListener<JSONMessage> returnHandler() {
return ((client, data, ackSender) -> {
- Map d = data.getData();
-
- int userId = (int) d.get("id");
- Book b = new Book((int) d.get("bookId"));
-
- if (b.returnBook(userId)) {
- client.sendEvent("returned");
- } else {
- client.sendEvent("returnFailed");
- }
+ Thread t = new Thread(() -> {
+ Map d = data.getData();
+
+ int userId = (int) d.get("id");
+ Book b = new Book((int) d.get("bookId"));
+
+ try {
+ if (b.returnBook(userId)) {
+ client.sendEvent("returned");
+ } else {
+ client.sendEvent("returnFailed");
+ }
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ });
+
+ t.start();
+ t.join();
});
}