preparing message bus
This commit is contained in:
@@ -12,6 +12,7 @@ application{
|
||||
|
||||
dependencies{
|
||||
implementation(project(":bookmark"));
|
||||
implementation(project(":bus"));
|
||||
implementation(project(":company"))
|
||||
implementation(project(":contact"))
|
||||
implementation(project(":core"))
|
||||
|
||||
@@ -19,6 +19,7 @@ import de.srsoftware.umbrella.files.FileModule;
|
||||
import de.srsoftware.umbrella.legacy.*;
|
||||
import de.srsoftware.umbrella.markdown.MarkdownApi;
|
||||
import de.srsoftware.umbrella.message.MessageSystem;
|
||||
import de.srsoftware.umbrella.messagebus.MessageApi;
|
||||
import de.srsoftware.umbrella.notes.NoteModule;
|
||||
import de.srsoftware.umbrella.project.ProjectModule;
|
||||
import de.srsoftware.umbrella.stock.StockModule;
|
||||
@@ -63,6 +64,7 @@ public class Application {
|
||||
var server = HttpServer.create(new InetSocketAddress(port), 0);
|
||||
try {
|
||||
new Translations().bindPath("/api/translations").on(server);
|
||||
new MessageApi().bindPath().on(server);
|
||||
new MessageSystem(config);
|
||||
new UserModule(config).bindPath("/api/user").on(server);
|
||||
new TagModule(config).bindPath("/api/tags").on(server);
|
||||
|
||||
@@ -43,7 +43,7 @@ subprojects {
|
||||
implementation("de.srsoftware:configuration.api:1.0.2")
|
||||
implementation("de.srsoftware:tools.jdbc:2.0.7")
|
||||
implementation("de.srsoftware:tools.http:6.0.5")
|
||||
implementation("de.srsoftware:tools.mime:1.1.3")
|
||||
implementation("de.srsoftware:tools.mime:1.1.4")
|
||||
implementation("de.srsoftware:tools.logging:1.3.2")
|
||||
implementation("de.srsoftware:tools.optionals:1.0.0")
|
||||
implementation("de.srsoftware:tools.util:2.0.4")
|
||||
|
||||
@@ -3,10 +3,11 @@ package de.srsoftware.umbrella.messagebus;
|
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||
|
||||
public class Event {
|
||||
|
||||
public enum Type{
|
||||
CREATE,
|
||||
UPDATE,
|
||||
DELETE
|
||||
DELETE;
|
||||
}
|
||||
|
||||
private UmbrellaUser initiator;
|
||||
@@ -20,4 +21,12 @@ public class Event {
|
||||
this.payload = payload;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String json() {
|
||||
return "TODO"; // TODO
|
||||
}
|
||||
|
||||
public String type(){
|
||||
return type.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,61 @@
|
||||
package de.srsoftware.umbrella.messagebus;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import de.srsoftware.tools.MimeType;
|
||||
import de.srsoftware.tools.Path;
|
||||
import de.srsoftware.umbrella.core.BaseHandler;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class MessageApi implements EventListener{
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static java.net.HttpURLConnection.HTTP_OK;
|
||||
|
||||
public class MessageApi extends BaseHandler{
|
||||
|
||||
private static final System.Logger LOG = System.getLogger(MessageApi.class.getSimpleName());
|
||||
private List<Event> eventQueue = new LinkedList<>();
|
||||
|
||||
@Override
|
||||
public void onEvent(Event event) {
|
||||
LOG.log(System.Logger.Level.DEBUG,"received {0}",event.getClass().getSimpleName());
|
||||
public boolean doGet(Path path, HttpExchange ex) throws IOException {
|
||||
var headers = ex.getResponseHeaders();
|
||||
headers.add(CONTENT_TYPE, MimeType.MIME_EVENT_STREAM);
|
||||
headers.add(CACHE_CONTROL,NO_CACHE);
|
||||
headers.add(CONNECTION,KEEP_ALIVE);
|
||||
ex.sendResponseHeaders(HTTP_OK,0);
|
||||
try (var os = ex.getResponseBody(); var stream = new PrintWriter(os)){
|
||||
var counter = 0;
|
||||
var id = 0L;
|
||||
while (true){
|
||||
Thread.sleep(100);
|
||||
if (eventQueue.isEmpty()){
|
||||
counter++;
|
||||
if (counter > 300) counter = sendBeacon(stream);
|
||||
} else {
|
||||
sendEvent(stream,id++,eventQueue.remove(0));
|
||||
counter = 0;
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
throw new UmbrellaException("EventStream broken").causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendEvent(PrintWriter out, long id, Event event) {
|
||||
if (event == null) return;
|
||||
out.printf("event: %s%n", event.type());
|
||||
out.printf("id: %s%n", id);
|
||||
out.printf("data: %s%n", event.json());
|
||||
out.print("\n"); // terminate message with empty line
|
||||
out.flush();
|
||||
}
|
||||
|
||||
private int sendBeacon(PrintWriter stream) {
|
||||
stream.print(": keep-alive\n\n");
|
||||
stream.flush();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,10 +20,12 @@ public class Constants {
|
||||
public static final String BODY = "body";
|
||||
public static final String BOOKMARK = "bookmark";
|
||||
|
||||
public static final String CACHE_CONTROL = "Cache-Control";
|
||||
public static final String CODE = "code";
|
||||
public static final String COMMENT = "comment";
|
||||
public static final String COMPANY = "company";
|
||||
public static final String COMPANY_ID = "company_id";
|
||||
public static final String CONNECTION = "Connection";
|
||||
public static final String CONTENT = "content";
|
||||
public static final String CONTENT_DISPOSITION = "Content-Disposition";
|
||||
public static final String CONTENT_TYPE = "Content-Type";
|
||||
@@ -67,7 +69,9 @@ public class Constants {
|
||||
|
||||
public static final String JSONARRAY = "json array";
|
||||
public static final String JSONOBJECT = "json object";
|
||||
public static final String KEY = "key";
|
||||
|
||||
public static final String KEEP_ALIVE = "keep-alive";
|
||||
public static final String KEY = "key";
|
||||
|
||||
public static final String LANGUAGE = "language";
|
||||
public static final String LAST_CUSTOMER_NUMBER = "last_customer_number";
|
||||
@@ -80,12 +84,13 @@ public class Constants {
|
||||
public static final String MESSAGES = "messages";
|
||||
public static final String MODULE = "module";
|
||||
|
||||
public static final String NAME = "name";
|
||||
public static final String NAME = "name";
|
||||
public static final String NEW_MEMBER = "new_member";
|
||||
public static final String MIME = "mime";
|
||||
public static final String NO_INDEX = "no_index";
|
||||
public static final String NOTE = "note";
|
||||
public static final String NUMBER = "number";
|
||||
public static final String MIME = "mime";
|
||||
public static final String NO_CACHE = "no-cache";
|
||||
public static final String NO_INDEX = "no_index";
|
||||
public static final String NOTE = "note";
|
||||
public static final String NUMBER = "number";
|
||||
|
||||
public static final String OFFSET = "offset";
|
||||
public static final String OPTIONAL = "optional";
|
||||
|
||||
@@ -3,4 +3,5 @@ description = "Umbrella : Tasks"
|
||||
dependencies{
|
||||
implementation(project(":bus"))
|
||||
implementation(project(":core"))
|
||||
implementation(project(":project"))
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ public class Constants {
|
||||
public static final String TABLE_TASK_DEPENDENCIES = "task_dependencies";
|
||||
public static final String TABLE_TASKS = "tasks";
|
||||
public static final String TABLE_TASKS_USERS = "tasks_users";
|
||||
public static final String TASK = "task";
|
||||
public static final String TASKS = "tasks";
|
||||
public static final String TASK_ID = "task_id";
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.notFound;
|
||||
import static de.srsoftware.umbrella.core.model.Status.*;
|
||||
import static de.srsoftware.umbrella.project.Constants.*;
|
||||
import static de.srsoftware.umbrella.task.Constants.*;
|
||||
import static java.lang.System.Logger.Level.*;
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
import de.srsoftware.tools.jdbc.Query;
|
||||
|
||||
Reference in New Issue
Block a user