11 ms·
A Sinatra-inspired micro web framework for Java
- nahname 13y agoHave people dropped the 'com' from java projects now? Not arguing in favor, the whole thing is ridiculous to me (src/main/com/actual_files). Just curious.
- mbell 13y agoDepends on the dependency management they want to support. If you want to get your package into maven central making it easy to use, you seem to still have to own the domain in reverse order. aka if you want to publish a package with a com.foo.* you need to actually own foo.com.
- brazzy 13y agoIt would actually be src/main/com/organization/project/actual_files And there are good reasons for that - naming packages after internet domains prevents namespace clashes and is consistently used by most Java projects. It's actually projects that diverge from this convention that stand out like a sore thumb, and many that did have adopted it eventually, like JUnit.
- film42 13y agoThis looks really cool, I can't wait to hack around with this! Question for the community: I don't have enough experience to know how this stacks up against other servers out there. Also, how do the people HN enjoy using java as a webserver (lang-wise, scaling-wise, etc)? Any feedback would be much appreciated!!
- mugenx86 13y agoI've never enjoyed using Java. If you have time look at Scala (Scalatra/Play Framework) or Clojure (Ring). You can reduce the size of your codebase dramatically.
- codenut 13y agoIve enjoyed writing code in scala, not until I compile it. It takes a long to compile. I agree with you, java is not enjoyable but we cannot deny the fact it still gets the job done.
- hrjet 13y agoThat used to be true earlier, but the compile times in Scala are pretty good these days. Also I hear that a faster compiler is expected with the 2.11 release. Incremental compilation in Eclipse is already real-time. But in general, IMO, compile times shouldn't be a major factor in choosing a language; expressiveness and run-times are more important.
- philbarr 13y ago> compile times shouldn't be a major factor in choosing a language Not sure about that, I'm using C# to write office plugins and it takes forever to start up each time. Often I forget what I'm supposed to be looking at by the time it's started.
- programminggeek 13y agoThat's only partially true. Compile times are absolutely a factor in development time for the same reason that how long your test suite takes to run is a factor. A faster feedback loop can shorten dev cycles. If nothing else, it's more fun to have faster compiles. This is one reason Go is so appealing to many people. The compile times make it "feel" like ruby or python or php in terms of "hit refresh and the change is there" style of development. That is a huge difference from, hit save and wait a minute for my java project to compile, and push out to tomcat a minute later. Even with JRebel the best I've seen is a 2-5 second page change refresh cycle in Scala and a minimum 5 second test suite reload time using SBT. Scala is a beautiful language, but compared to Go, it's a very slow dev cycle.
- spacebe4time 13y agoPlay! is hardly a micro framework. Play is fun until you try using its JSON library. I think I've been scarred for life.
- pjmlp 13y agoI enjoy using it, except when we are forced to target Websphere. JBoss, Jetty and Tomcat are pretty good in terms of taking care of load in heavy sites. Most JVMs (Oracle, IBM, Azul, Aonix, ...) are able to achieve very good performance levels. There is the tooling one gets, not only in terms of IDE support, but also for monitoring the state of the server processes and how load is being affected. Finally if Java the language, doesn't appeal you, there are plenty of languages that target the JVM as well.
- fauigerzigerk 13y agoJava scales well as long as memory is not the issue. Java's problem is memory usage and above all a deeply ingrained culture of complexity.
- trailfox 13y agoOn modern server hardware memory is seldom an issue, but I would agree about the culture of complexity BeanSingletonProxyAbstractWorkerListener
- fauigerzigerk 13y agoMemory is the dominant cost factor when you rent a server, but my main issue with Java's memory usage is that a single (Oracle) VM instance becomes incresingly unusable above 4GB of RAM due to garbage collector pauses. If you can distribute your work over many VMs that's fine, but if the application needs a lot of data in memory then Java just isn't a viable solution. At least not the Oracle VM and Azul is too expensive.
- johnyzee 13y agoI would use Java for GWT, if nothing else. Being able to write a huge and complex application for the web, using Java both for the client and server code is something I would not want to live without (links in my profile for examples). But besides that, Java is extremely mature, has great tooling, huge community and of course an enormous ecosystem with all the third party tools and libraries that come with it. The language is a bit primitive (but improving with each release), and the 'enterprise consultant' mindset of complexity for its own sake can be a turn-off, but overall, yeah, I like using Java.
- mindcrime 13y agoAlso, how do the people HN enjoy using java as a webserver (lang-wise, scaling-wise, etc)? I mostly like Java, and find a lot of the criticisms to be somewhat misguided, or reducing to quibbling over minutiae. But the Java ecosystem has a lot going for it, and despite how some people think "enterprise-y" is a pejorative, a lot of those "enterprise" features are actually very handy when you're, well, building a serious enterprise solution. Sure, if you're building the n thousandth MVP of a new cat-picture-sharing site, you don't need Java. But if you're building Amazon.com (the ecommerce side, not AWS) you might just find that JTA, JMS, JMX, JAX-RS, and some of the other "enterprise-y" stuff in Java is pretty fucking useful. From a scalability standpoint, using modern appservers, Java scales horizontally just fine. And since Java runs on just about everything from watches to IBM zSeries mainframes, it also scales vertically pretty well. All of that said, I prefer to do my actual coding in Groovy, as opposed to "pure" Java, but Groovy is almost a strict superset of Java, just with nicer syntax and some new features. But whether you're using Groovy or Java, everything I said above still applies, since you're still part of that overall JVM ecosystem.
- vorg 13y agoGroovy's much slower than Java. The statically typed mode shipped in Groovy 2 a year ago was written by one lone programmer they brougt in, is still buggy, and isn't even used in the Grails codebase afaik.
- mindcrime 13y agoYou're right, it is slower. But for my purposes, it's fast enough. YMMV, of course.
- stevvooe 13y agoWhere does this stand against dropwizard?
- mbell 13y agoPersonally, I would use dropwizard. Spark's key point seems to be avoiding annotation based constructs. Compare (Spark): public class HelloWorld { public static void main(String[] args) { get(new Route("/hello") { @Override public Object handle(Request request, Response response) { return "Hello World!"; } }); } } to (JAX-RS, which dropwizard uses) @Path("/hello") public class HelloWorld { @GET public String get() { return "Hello World"; } }
- newobj 13y agoAnnotations are great until you want to do ANYTHING dynamic. "It's a trap!" Everytime without fail I want to do this kind of thing I end up ripping it all out later when I need more flexibility. If Dropwizard lets me fall back to non-annotations, then awesome. Really, five minutes ago I didn't know either of these projects existed, so right now I'm feeling a little giddy either way you slice it!
- mbell 13y agoI don't know what you mean by dynamic but dropwizard is just a selection of other libraries wired together into a framework with supporting code (config, etc) which is built for high performance HTTP services serving JSON, but it can do HTML rendering. The above code is compliant with any JAX-RS implementation, JAX-RS is probably the most well done Java spec I've ever worked with which isn't saying much, but it doesn't suck.
- lmartel 13y agoBy dynamic I think he just means that annotations are a really hacky way to do things. You use annotations in place of java code, and while you can tweak java code you can't tweak what an annotation does or how it does it.
- Zolomon 13y agoI wrote a semi-realtime implementation of the planning poker estimation technique at [1] using Spark and some template engine I found. This was for a course in software project management with some time restraints, so only focused on getting it done. [1] https://github.com/Zolomon/PlanningPoker https://github.com/Zolomon/PlanningPoker
- trailfox 13y agoSpeaking of Sinatra-inspired web frameworks on the JVM have a look at Scalatra: http://scalatra.org/ http://scalatra.org/ https://github.com/scalatra/scalatra https://github.com/scalatra/scalatra
- k__ 13y agoI liked most of it. But when it came to this atmosphere stuff, it was vietnam all over again! What's wrong with those Scala developers? They could make beautiful frameworks and APIs, always start to create crazy method names. > "org.scalatra" %% "scalatra-atmosphere" % "2.2.1", > send(("author" -> "system") ~ ("message" -> "Only json is allowed") ~ ("time" -> (new Date().getTime.toString ))) What's wrong with them?!
- dscrd 13y agoSame symptom is shown in Haskell circles. I suspect it might be due to the enthusiasts of these languages often being mathematicians. And mathematicians love crazy sigils, as we know.
- k__ 13y agoprobably :\ And this is rather sad. You got a language which allows you to write nearly litteral code and still build stuff only mathematicans can understand.
- the1 13y agoyah they should've done something like: send(JSONSerialization.with(StringSerializer.class, DateTimeSerializer.class).serialize(JSONObjectFactory.put(PairFactory.of(String.class, String.class).getInstance("author", "system"), PairFactory.of(String.class, String.class).getInstance("message", "Only json is allowed"), PairFactory.of(String.class, DateTime.class).getInstance("time", new Date())).getInstance())) no, it's just json4s dsl for constructing json. If you wanted, you could've used constructors yourself: send(pretty(render(JObject(List(JField("author", JString("system")), JField("message", JString("Only json allowed")), JField("time", JString(new DateTime().getTime().toString())))) usually scala libraries come with an easy to read/write DSL version of the API along with a more verbose java-style API shown above. Many libraries don't document with a plenty of examples. But, it really takes 10 minutes to learn DSL version in many cases. And, it gives you wings. There are plenty of benefits of DSL. You can research online about them.
- drawkbox 13y agoDefinitely something that was needed, microframeworks are great. Restlet I used long ago but still very Java enterprisey. Play! is like Java's Django/Rails and now Spark is the Sinatra, simple and minimal. Scalatra mentioned here looks great as well, all these wonderful minimal Java toys.
- Shank 13y agoIt really bugs me that they decided to clone the Sinatra README page, keeping most of the docs on one giant list instead of a more traditional multi-page doc approach. See here: http://www.sparkjava.com/readme.html http://www.sparkjava.com/readme.html In addition, some of their docs link to a Google Code page, which keeps some examples on its index too: https://code.google.com/p/spark-java/ https://code.google.com/p/spark-java/ Lastly, it appears they're using GitHub for actual code, and, while they have a Github wiki created, it has no information. I wish I could stress just how important it is to have all of the docs in one centralized location. Somewhere along the line, one of the five places they have examples will get out of date, and it'll be difficult to play spot the differences when it's replicated everywhere.
- waffenklang 13y agowould love to see such a mf for c/c++...
- papaf 13y agohttps://github.com/d5/node.native https://github.com/d5/node.native
- waffenklang 13y agonot exactly a microframework, but an interesting approach to build a basis for it, like rack for sinatra.
- kitd 13y agoVery nice! Had a look at this a year or so ago and it would be a great base for mocking up backends IMHO. In a similar vein is Google SiteBricks: http://sitebricks.org/#home http://sitebricks.org/#home Slightly heavier weight but with more features (templating, annotations, etc). Depends on Guice.
- 6ren 13y agoI think it's simpler to tribute Sinatra in Java by annotating methods: @get('/hi') String myMethod() { return "Hello World!"; } I made a prototype of this (including extracting arguments Sinatra-style, which I think is its coolest bit), and didn't encounter any serious problems with this approach.
- jebblue 13y agoI like reducing the number of annotations which is one reason I like the SparkJava approach.
- 6ren 13y agoJava 8 lambdas might shorten their approach too.
- jebblue 13y agoI've reviewed Lambdas and still don't think the syntax is clearer to me than just using an Anonymous Class. Either way, less configuration and less annotations, whatever lets me do that I could buy into as long as the debugging isn't a mess.
- scorchin 13y agoThere's also webbit-rest: https://github.com/webbit/webbit-rest https://github.com/webbit/webbit-rest
- f4stjack 13y agoOkay. I am holding the idiot ball here: How can we deploy an app developed with spark? What are our options? Heroku? AWS? I haven't seen anything regarding to deployment in the documentation.
- msluyter 13y agoWell, there's a howto on running on tomcat: http://www.sparkjava.com/readme.html#title17 http://www.sparkjava.com/readme.html#title17 As for Heroku, I found this: https://gist.github.com/Fitzsimmons/2490382 https://gist.github.com/Fitzsimmons/2490382 Generally, for a lot of apps the embedded Jetty webserver might be sufficient.
- pwendel 13y agoCheck Running Spark on a Web Server, e.g. Tomcat in Readme section.
- quicksilver03 13y agoAt the bottom of http://www.sparkjava.com/readme.html http://www.sparkjava.com/readme.html there are 2 options, an embedded web server (Jetty) and a configuration snippet for a standard web.xml file.
- chotachetan 13y agoIt is a nice one. I use Jelastic(www.jelastic.com) for my startup. What if we can have the same functionality there integrated. But this is needed at least the microframeworks are nice. Thinking that the same kind of thing can be also achieved with he framework integration in Jelastic.
- Oculus 13y agoFinally a web framework I can use. All the Java web frameworks I tried to use are way too enterprise-y.
- edwinnathaniel 13y agoYou should expand your horizon once in a while. JAX-RS has ben her for a while. Ditto with Spring MVC.
- pron 13y agoThis project is nice but it's important to mention that there is actually a "Java micro web framework" standard called JAX-RS that is pretty widely adopted (with solid open-source implementations by JBoss, Apache, Oracle and others). Here's a rather old comparison of JAX-RS implementations: http://www.infoq.com/news/2008/10/jaxrs-comparison http://www.infoq.com/news/2008/10/jaxrs-comparison Its most notable implementation is perhaps Jersey (https://jersey.java.net/documentation/latest/getting-started.html https://jersey.java.net/documentation/latest/getting-started...), which is used, for example, by Dropwizard (mentioned in another comment). Some of these implementations include important features like health and performance monitoring.
- hawleyal 13y agoJAX-RS still has the same problem with nesting and redundancy, doesn't fit the word "micro" or draw parallels with Sinatra.
- pron 13y agoTo quote another commenter, here's JAX-RS code: @Path("/hello") public class HelloWorld { @GET public String get() { return "Hello World"; } } Seems very Sinatra-like to me.
- hawleyal 13y agoYou don't see all the redundancies in that code? I see the term "get" twice. The class name is redundant with the resource path. I suppose not a huge problem because many MVC frameworks require classes, but not even close to Sinatra. Sinatra: get '/hi' do "Hello World!" end
- meddlepal 13y agoYou're counting required language keywords towards what makes a micro-framework? That's bullshit. You're never going to get away from some of the keywords in Java, nor does that make JAX-RS less 'micro' Here is an example in Groovy using JAX-RS: @Path("/hello") class HelloWorld { @GET def sayHello() { "Hello, world!" } } Almost as clean. The braces and parens make it a bit uglier; but those are just part of the language.
- hawleyal 13y agoSomehow having that much nesting and redundancy doesn't fit the word "micro" or draw parallels with Sinatra. Sinatra: get '/hi' do "Hello World!" end Spark: public class HelloWorld { public static void main(String[] args) { get(new Route("/hello") { @Override public Object handle(Request request, Response response) { return "Hello World!"; } }); } }
- tga 13y agoAll the nesting and redundancy come from Java itself. Verbose as it may be, after you use any language for a while you will see right through the syntax and it will not add (much) cognitive load to your work. I think the value here is in having a micro framework that allows you to put together a web app without having to juggle a horribly complex abstraction hierarchy for even the simplest of tasks.
- hawleyal 13y agoSyntax is what makes the framework micro. Following your point about the cruft being from the language, Spring MVC is as "micro" as Spark. In fact, it is shorter. Spring MVC: @Controller public class HelloController { @RequestMapping(value = "/hello", method = RequestMethod.GET) public @ResponseBody String hello() { return "Hello World!"; } } Spark: public class HelloWorld { public static void main(String[] args) { get(new Route("/hello") { @Override public Object handle(Request request, Response response) { return "Hello World!"; } }); } }
- tga 13y agoYou don't call a framework "micro" based on the amount of code it takes to achieve certain tasks. You call it "micro" because it's small enough that you can learn and truly master in a short time (and you get that by cutting out as many non-essential features as possible). Therefore, Micro: http://www.sparkjava.com/readme.html http://www.sparkjava.com/readme.html Not micro: http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/ http://docs.spring.io/spring/docs/3.2.x/spring-framework-ref... The downside when using a micro framework is that you always walk a fine line between simplicity and code reuse. Because of this, as the application grows you do end up writing more code than with a complex framework because you slowly start reinventing the wheel -- this is the old argument you hear over and over (Flask vs Django, Sinatra vs Rails, BackboneJS vs AngularJS, etc.)
- ShabbyDoo 13y agoI had looked at Spark a couple of weeks ago. I'm looking for a lightweight service-y sort of framework which just happens to have a good HTTP service available out-of-the-box. My application does back-end processing and coordinates a bunch of data feeds, thick-client HTTP requests, background processing, etc. all together. Spark was too web-centric (not a bad thing, just not what I need). OSGi was an attempt to be what I want, but its requirement of classpath isolation (a good idea conceptually) is onerous in practice. Right now, I'm using a homespun amalgam of Grizzly, Guice, and java.util.concurrent (for thread pooling), and other stuff. I really didn't want to build my own container. I'm intrigued by Apache Karaf, but OSGi is a huge pill to swallow (If you haven't debugged OSGi classloading issues, you'd be in for a treat). In a nutshell, I want a service lifecycle container which allows me to write small, lightweight, modular services which depend on each other. The container should provide for cross-cutting concerns like monitoring, management, configuration, logging, auditability (I have concrete definitions for these things -- they're not just abstract biz-speak to me). HTTP should be an out-of-the-box, optional module. A service which exposes another service via a RESTful interface and depends upon the HTTP service should be another. For my application, services which listen to multicast data streams are just as important interfaces to the world as JSON-over-HTTP-via-REST. I want to write the HelloWorld method body and be able to do stuff like: expose it via a RESTful interface, invoke it every N seconds, inject an interface exposing the HelloWorld contract into other services, etc. When I want to know how my HelloWorld service is performing, there's a pre-built web interface which provides New Relic-esque views. I'd like to capture audit trails of the transactional flows through my services from an origination point (HTTP call, scheduled job, etc.) so I can translate failures, poor performance, usage rates, etc. into meaningful information (I wrote a poor man's version of this myself, and it's been quite useful). Does anything like this exist? I sure can't find it. Modern JBoss (now Wildfly) might actually be closer to my requirements than I think. Perhaps I should look at the work they're doing on Version 8.
- pron 13y agoUsing Grizzly/Netty as the core comm component seems like a good idea. Jersey integrates will with Grizzly, I think. Dropwizard packages together Jersey on top of Jetty with good monitoring, but doesn't take care of other stuff like multicast. Nothing prevents you from adding Netty/Grizzly into the mix, though. Actually, the modularity, composability and cross-cutting concerns you mention suggest Spring – not lightweight by any means, but neither are your requirements :) I think you can even use Jersey with Spring: http://www.infoq.com/articles/springmvc_jsx-rs http://www.infoq.com/articles/springmvc_jsx-rs
- bhauer 13y agoFor what it's worth, Spark performs quite well in terms of request routing when compared to other JVM frameworks of various sizes [1]. [1] http://www.techempower.com/benchmarks/#section=data-r6&hw=ec2&test=json&l=6g4 http://www.techempower.com/benchmarks/#section=data-r6&hw=ec...
- habosa 13y agoThis is fantastic. I know a lot of programming languages and I love them all for different reasons, but I think I am most productive and most likely to produce error-free programs in Java. All the verbosity that people hate is a feature to me, I know exactly what my program is doing. However, I have done 95% of my web development in Ruby due to the Sinatra and Rails frameworks. The developer experience is too good to ignore. I'm hoping this can replace my Sinatra use case ... I don't think the magic of Rails could ever be replicated in Java.
- rubiquity 13y agoI feel like Keurig would be a no brainer as a name for a Java micro Web framework.
- matt__ring 13y agoI love it! Just whipped up a couple of toy web services in 15 mins using Spark. They will be part of a technical interview coding task. True, Spark may not be better/more concise than JAX-RS, but fun to try anyway.
- msgilligan 13y agoRatpack Framework is a Sinatra-inspired framework for the JVM worth noting: http://www.ratpack-framework.org http://www.ratpack-framework.org Groovy: ratpack { handlers { get('hello') { response.send "Hello world!" } } } It can also be used (with somewhat less brevity) in good, old-fashioned Java. An 0.9.0 release is expected in a few weeks and the project is undergoing rapid (and sometimes API-changing) development, but it looks very promising.