Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
Fixed RequestHandler#queueRequest
Browse files Browse the repository at this point in the history
modified the version
  • Loading branch information
JoshiCodes committed Jun 11, 2023
1 parent 5c8ef20 commit a303228
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.joshicodes</groupId>
<artifactId>rja</artifactId>
<version>1.0-beta.1</version>
<version>1.0-beta.1a</version>

<description>
Simple Java API for Revolt.
Expand Down
30 changes: 18 additions & 12 deletions src/main/java/de/joshicodes/rja/requests/RequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import java.lang.reflect.Parameter;
import java.net.URISyntaxException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

public class RequestHandler {
Expand All @@ -35,14 +38,14 @@ public class RequestHandler {
private final List<EventListener> listeners;
private final List<IncomingEvent> events;

private final HashMap<RestAction<?>, Pair<Consumer<Object>, Consumer<Throwable>>> actionQueue;
private boolean processingQueue = false;
private final ConcurrentHashMap<RestAction<?>, Pair<Consumer<Object>, Consumer<Throwable>>> actionQueue;
private AtomicBoolean processingQueue = new AtomicBoolean(false);

public RequestHandler(RJABuilder rja, List<EventListener> listeners, List<IncomingEvent> events) throws URISyntaxException {
this.rja = rja;
this.listeners = listeners;
this.events = events;
this.actionQueue = new HashMap<>();
this.actionQueue = new ConcurrentHashMap<>();
rja.getLogger().info("Connecting...");
this.socket = new RequestSocket(this);
}
Expand Down Expand Up @@ -74,7 +77,7 @@ public void run() {
*
* @deprecated Use {@link #fetchRequest(RJA, RestRequest)} instead
*/
@Deprecated(forRemoval = true, since = "1.1-alpha.1")
@Deprecated(forRemoval = true, since = "1.0-beta.1")
public <T> T sendRequest(final RJA rja, RestRequest<T> request) {
final RJABuilder builder = this.rja;
Pair<Integer, JsonElement> multi = builder.makeRequest(request);
Expand All @@ -87,28 +90,31 @@ public <T> T sendRequest(final RJA rja, RestRequest<T> request) {
}

private void processQueue() {
if(processingQueue || actionQueue.isEmpty())
if(processingQueue.get() || actionQueue.isEmpty())
return;
processingQueue = true;
processingQueue.set(true);
new Thread(() -> {
Iterator<RestAction<?>> iterator = actionQueue.keySet().iterator();
while(iterator.hasNext()) {
final RestAction<?> action = iterator.next();
while (iterator.hasNext()) {
RestAction<?> action = iterator.next();
final Pair<Consumer<Object>, Consumer<Throwable>> pair = actionQueue.get(action);
iterator.remove();
try {
Object result = action.complete();
if(pair.getFirst() != null) {
if (pair.getFirst() != null) {
pair.getFirst().accept(result);
}
} catch (Exception e) {
e.printStackTrace();
if(pair.getSecond() != null) {
if (pair.getSecond() != null) {
pair.getSecond().accept(e);
}
}
iterator.remove();
}
processingQueue = false;
processingQueue.set(false);
if(!actionQueue.isEmpty()) {
processQueue();
}
}).start();
}

Expand Down

0 comments on commit a303228

Please sign in to comment.