Skip to content

Instantly share code, notes, and snippets.

@snower
Created May 11, 2023 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snower/f8ef25e57c72f9b41fb31ee8b164193b to your computer and use it in GitHub Desktop.
Save snower/f8ef25e57c72f9b41fb31ee8b164193b to your computer and use it in GitHub Desktop.
Spring Boot Distributed Deferred Event
package com.aecg.cdtx.controller;
import io.github.snower.jaslock.Event;
import io.github.snower.jaslock.SlockClient;
import io.github.snower.jaslock.exceptions.SlockException;
import org.bson.types.ObjectId;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;
import java.io.IOException;
@RestController
@RequestMapping("/event/test")
public class EventTestController {
private final SlockClient slockClient;
public EventTestController() {
this.slockClient = new SlockClient("192.168.5.198");
this.slockClient.enableAsyncCallback();
try {
this.slockClient.open();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@PostMapping("/create")
public String createEvent() throws SlockException {
String eventId = (new ObjectId()).toHexString();
Event event = slockClient.newEvent(eventId,5, 500, true);
event.clear();
return eventId;
}
@PostMapping("/set")
public String setEvent(@RequestBody String eventId) throws SlockException {
Event event = slockClient.newEvent(eventId,5, 500, true);
event.set();
return eventId;
}
@GetMapping("/wait")
public DeferredResult<Boolean> waitEvent(@RequestParam String eventId) throws SlockException {
Event event = slockClient.newEvent(eventId,5, 500, true);
DeferredResult<Boolean> deferredResult = new DeferredResult<>(15000L);
event.wait(10, r -> {
try {
deferredResult.setResult(r.getResult());
} catch (SlockException e) {
deferredResult.setResult(false);
}
});
return deferredResult;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment