Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active February 3, 2026 20:26
Show Gist options
  • Select an option

  • Save dacr/69caa9bf4283f799e0768cdf1d6bd088 to your computer and use it in GitHub Desktop.

Select an option

Save dacr/69caa9bf4283f799e0768cdf1d6bd088 to your computer and use it in GitHub Desktop.
ZIO learning - zhttp - streaming from a java stream / published by https://github.com/dacr/code-examples-manager #de9fa625-5d52-4e22-99de-bb847c7a8353/f73748eb384e0f83269507376e342838acbc7492
// summary : ZIO learning - zhttp - streaming from a java stream
// keywords : scala, zio, learning, pure-functional, http-server, zhttp, streams, @testable, @exclusive, @fail
// publish : gist
// authors : David Crosson
// license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
// id : de9fa625-5d52-4e22-99de-bb847c7a8353
// created-on : 2024-01-05T18:44:19+01:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// test-with : curl http://127.0.0.1:8080/
// ---------------------
//> using scala "3.4.2"
//> using dep "dev.zio::zio-http:3.0.0-RC4"
// ---------------------
import zio.*, zio.http.*, zio.stream.*
import java.util.stream.{Stream => JStream}
// SEE https://github.com/zio/zio-http/issues/2584
object Main extends ZIOAppDefault {
def getJavaStream: Task[JStream[String]] = ZIO.attempt {
throw RuntimeException("failure")
//JStream.of("1", "2", "3", "4")
}
override def run =
for {
_ <- ZIO.logInfo("started")
stream = ZStream.fromJavaStreamZIO(getJavaStream).flatMap(s => ZStream.fromIterable(s.getBytes))
app = Routes(Method.GET / "stream" -> handler(Response(body = Body.fromStream(stream)))).toHttpApp
_ <- Server.serve(app).provide(Server.default)
} yield ()
}
Main.main(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment