简体   繁体   中英

How to configure tomcat in http4s

I wonder if there is any option to configure tomcat when using http4s server API. Tomcat builder allows to change some basic options, but besides those there is no much what can be set. Could I somehow provide a server.xml file? Or get access to tomcat instane?

Tomcat, AFAIR is a server running applications defined as WARs. That is: your app is not a server, your app is logic bound to functionalities provided by this external layer.

Http4s server talks to the external world directly, it manages requests lifecycle through FS2 streams, if you use it then you probably talk to DB through eg Doobie or some other Cats library, which also manages its own thread pools and transactions instead of relying on ThreadLocal s and other JavaEE-like and JPA-like conventions.

Long story short: these models are incompatible.

You would have to rip-off 90% of Http4s to leave something that could be agnostic to implementation, and then wire it to Tomcat, but that would leave only, IDK, DSLs for building requests? And for that you'd have better chances to eg define things using Endpoints4s or Tapir and implementing interpreter which binds things to Tomcat.

But at that point there would be probably 0 benefits from using Tomcat or any other servlet container: in Java EE it is usually easy to monitor things because you have 1 thread per 1 ongoing request, and all DB connections and stuff is put into ThreadLocal s as request-scoped things. Meanwhile virtually all IO monads use thread pools (separate for different boundaries) so your operation can span across several threads. All conventions that containers rely on to monitor things (which are quite inflexible performance-wise) go to hell.

Meanwhile Http4s has its own ways of tracking things (through middleware), similarly you can add instrumentation to DB queries. So things provided by servlet container are also available there, though it requires a bit of effort to configure them.

The bottom line is: if you are using Http4s, you don't need servlet container, and if you are using servlet container, then you don't have a nice integration with anything that is using any IO monad ( scala.concurrent.Future , cats.effect.IO , monix.execution.Task , zio.ZIO , etc) as they aren't guaranteed to run whole operation in the same thread (invalidating a lot of assumptions made by certain Java frameworks).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM