From d57e9170056480ca71267957325cf469b09fbdc3 Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Mon, 8 Jul 2024 23:23:04 +0200 Subject: [PATCH] feat: add http streaming support --- jetty/pom.xml | 75 +++++++++++++++++++ .../bsc/langgraph4j/JettyStreamingServer.java | 74 ++++++++++++++++++ jetty/src/main/webapp/streaming.html | 33 ++++++++ pom.xml | 1 + 4 files changed, 183 insertions(+) create mode 100644 jetty/pom.xml create mode 100644 jetty/src/main/java/org/bsc/langgraph4j/JettyStreamingServer.java create mode 100644 jetty/src/main/webapp/streaming.html diff --git a/jetty/pom.xml b/jetty/pom.xml new file mode 100644 index 0000000..92513a8 --- /dev/null +++ b/jetty/pom.xml @@ -0,0 +1,75 @@ + + + 4.0.0 + + org.bsc.langgraph4j + langgraph4j-parent + 1.0-SNAPSHOT + + + jetty + + war + Servlet 3 Webapp with Jetty Plugin + + + 1.8 + 1.8 + UTF-8 + 12.0.11 + + + + + + + + + + + + + org.eclipse.jetty + jetty-server + ${jetty.version} + + + + + org.eclipse.jetty.ee10 + jetty-ee10-servlet + ${jetty.version} + + + + org.junit.jupiter + junit-jupiter + test + + + + + + + org.codehaus.mojo + exec-maven-plugin + 3.3.0 + + java + + + org.bsc.langgraph4j.JettyStreamingServer + + + + + diff --git a/jetty/src/main/java/org/bsc/langgraph4j/JettyStreamingServer.java b/jetty/src/main/java/org/bsc/langgraph4j/JettyStreamingServer.java new file mode 100644 index 0000000..fad0a78 --- /dev/null +++ b/jetty/src/main/java/org/bsc/langgraph4j/JettyStreamingServer.java @@ -0,0 +1,74 @@ +package org.bsc.langgraph4j; + +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.eclipse.jetty.ee10.servlet.ServletContextHandler; +import org.eclipse.jetty.ee10.servlet.ServletHolder; +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.server.handler.ResourceHandler; +import org.eclipse.jetty.util.resource.ResourceFactory; + +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.concurrent.TimeUnit; + +public class JettyStreamingServer { + + public static void main(String[] args) throws Exception { + Server server = new Server(); + ServerConnector connector = new ServerConnector(server); + connector.setPort(8080); + server.addConnector(connector); + + ResourceHandler resourceHandler = new ResourceHandler(); + Path publicResourcesPath = Paths.get( "jetty", "src", "main", "webapp" ); + resourceHandler.setBaseResource(ResourceFactory.of(resourceHandler).newResource(publicResourcesPath)); + resourceHandler.setDirAllowed(true); + + ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); + context.setContextPath("/"); + // Add the streaming servlet + context.addServlet(new ServletHolder(new StreamingServlet()), "/stream"); + + Handler.Sequence handlerList = new Handler.Sequence(resourceHandler, context ); + + server.setHandler(handlerList); + + server.start(); + server.join(); + } + + public static class StreamingServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/plain"); + response.setCharacterEncoding("UTF-8"); + + final PrintWriter writer = response.getWriter(); + + // Start asynchronous processing + request.startAsync(); + + // Simulate a long-running process + new Thread(() -> { + try { + for (int i = 0; i < 10; i++) { + writer.println("Line " + i); + writer.flush(); + TimeUnit.SECONDS.sleep(1); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } finally { + writer.close(); + } + }).start(); + } + } +} \ No newline at end of file diff --git a/jetty/src/main/webapp/streaming.html b/jetty/src/main/webapp/streaming.html new file mode 100644 index 0000000..e4a562b --- /dev/null +++ b/jetty/src/main/webapp/streaming.html @@ -0,0 +1,33 @@ + +streaming + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index a13b829..6c659ea 100644 --- a/pom.xml +++ b/pom.xml @@ -33,6 +33,7 @@ agent-executor image-to-diagram adaptive-rag + jetty