This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-kamelets-examples.git
commit 5adf86d2ba7af0e904ff13a5d31d3d89567b0cbb Author: Claus Ibsen <[email protected]> AuthorDate: Tue Aug 9 13:06:11 2022 +0200 Add http streaming example --- jbang/http-streaming/MyHttpServer.java | 51 ++++++++++++++++++++++++++++ jbang/http-streaming/README.adoc | 61 ++++++++++++++++++++++++++++++++++ jbang/http-streaming/my-stream.yaml | 9 +++++ 3 files changed, 121 insertions(+) diff --git a/jbang/http-streaming/MyHttpServer.java b/jbang/http-streaming/MyHttpServer.java new file mode 100644 index 0000000..4f4652e --- /dev/null +++ b/jbang/http-streaming/MyHttpServer.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import com.sun.net.httpserver.HttpContext; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpServer; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; + +public class MyHttpServer { + + public static void main(String[] args) throws Exception { + HttpServer server = HttpServer.create(new InetSocketAddress(8500), 0); + HttpContext context = server.createContext("/"); + context.setHandler(MyHttpServer::handleRequest); + System.out.println("http://localhost:8500"); + server.start(); + } + + private static void handleRequest(HttpExchange exchange) throws IOException { + exchange.sendResponseHeaders(200, 0); + OutputStream os = exchange.getResponseBody(); + for (int i = 0; i < 100; i++) { + String response = "Hello " + i + "\n"; + os.write(response.getBytes()); + os.flush(); + System.out.println("... " + i); + try { + Thread.sleep(1000); + } catch (Exception e) { + // ignore + } + } + os.close(); + } +} \ No newline at end of file diff --git a/jbang/http-streaming/README.adoc b/jbang/http-streaming/README.adoc new file mode 100644 index 0000000..36946c7 --- /dev/null +++ b/jbang/http-streaming/README.adoc @@ -0,0 +1,61 @@ +== HTTP Streaming + +This example shows how Camel can continuously read HTTP streaming data, from a remote HTTP server. +The HTTP server is expected to send data back in chunks separated by new-line (HTTP streaming). + +=== Install JBang + +First install JBang according to https://www.jbang.dev + +When JBang is installed then you should be able to run from a shell: + +[source,sh] +---- +$ jbang --version +---- + +This will output the version of JBang. + +To run this example you can either install Camel on JBang via: + +[source,sh] +---- +$ jbang app install camel@apache/camel +---- + +Which allows to run CamelJBang with `camel` as shown below. + +=== How to run + +You need to run the HTTP server first which can be done using Java: + +[source,sh] +---- +$ java MyHttpServer.java +---- + +This starts a local HTTP server on port 8500 + +Then you can run this example using: + +[source,sh] +---- +$ camel run my-stream.yaml +---- + +Or run with JBang using the longer command line (without installing camel as app in JBang): + +[source,sh] +---- +$ jbang camel@apache/camel run mystream.yaml +---- + +=== Help and contributions + +If you hit any problem using Camel or have some feedback, then please +https://camel.apache.org/community/support/[let us know]. + +We also love contributors, so +https://camel.apache.org/community/contributing/[get involved] :-) + +The Camel riders! diff --git a/jbang/http-streaming/my-stream.yaml b/jbang/http-streaming/my-stream.yaml new file mode 100644 index 0000000..f1d17f8 --- /dev/null +++ b/jbang/http-streaming/my-stream.yaml @@ -0,0 +1,9 @@ +# camel-k: language=yaml + +- from: + uri: "stream:http" + parameters: + httpUrl: "http://localhost:8500" + scanStream: true + steps: + - log: "${body}"
