jump to navigation

J2EE Still Rules the Roost 19 August 2009

Posted by manniwood in J2EE, Java, Programming.
1 comment so far

Two blog posts in a row today!

This is another one about my on-going job search, and things I’ve been learning.

In Beating the Averages, Paul Graham talked about how using a high-level language like Lisp helped make ViaWeb a success. In one of his other essays, Great Hackers, he talks about how which language a business chooses to develop in determine the quality of programmers they can hire (on the assumption that great hackers are more willing to learn off-the-beaten-path languages).

And I must say, at the startup where I currently work, switching from J2EE to PythonDjango definitely made a difference with getting our product completed.

But the Python jobs are few, and the J2EE jobs are many.

Python and Ruby haven’t really broken through yet. Books like Bruce Tate’s Beyond Java (already four years old!) still remain hypothetical, describing a possible future that hasn’t arrived yet.

I’m still glad I know Python, and still glad we switched to it on my current project. Learning a new language gives you a new way of looking at the same problems. And hey: at least my knowing Python shows that I’m not a one-trick pony. When I say I like learning new things, my knowledge of Python supports that.

I still eagerly interview for Python positions when they come up.

But Python and Ruby, in terms of popularity, are still nibbling at firmly-entrenched Java. Python is a great language, and I hope it goes far, but Java is still where the market and the jobs are at.

I never want to program without a REPL again 5 August 2009

Posted by manniwood in J2EE, Programming, Python.
add a comment

I’m hacking code in Python right now, and I just said to myself “I never want to program without a REPL again”.

I just read a blog entry at artfulcode.net, where the author said “Vim 7 has auto-complete, although I rarely use it. I’m much more comfortable with an enormous stack of O’Reilly books.” As someone who spends a lot of money on dead trees every year (you’re welcome, O’Reilly), this rang very true for me.

But in addition to consulting lots of programming books, I spend a lot of time with a python interpreter open, typing exploratory code. Often, this is quicker than picking up a book, because I’m already pretty sure what a bit of code will do—Python’s REPL just lets me test it to be certain I have the syntax right, etc, etc.

Now that I think about it, I spend a lot of days with a psql session open, too, so I can type exploratory SQL. There’s really no substitute for being able to turn “I wonder what would happen if…” into “Let’s find out right now!”.

Not too many months ago, when I was still hacking J2EE, I used Eclipse to help me through Java. For such a verbose language, a good development environment is really key, because there’s no REPL, and the APIs are gargantuan.

With Python, I’m back to hacking code in Vim again (sort of full circle, seeing as I used Vim back in my Perl/CGI days) because Python is so productive that a Python IDE isn’t that big of a win over a good editor, a stack of books, and… the REPL! I think a REPL has to be part of the baseline of any new language. (Sort of the way Java added garbage collection to the new baseline back in its day. After Java, you really couldn’t introduce a new general-purpose programming language without garbage collection.) The fact that I couldn’t do exploratory Java programming in a REPL was a real productivity killer that not even Eclipse could mitigate.

With the popularity of Python and Ruby, the baseline has moved up again: any new language must be interpreted as well as compiled. Eclipse tried really hard to compile Java on the fly, to make up for the fact that there was no Java interpreter to speed up development, but I always found myself stopping Tomcat, doing a build, and restarting, just to be certain that each iteration really worked and wasn’t using old class files.

Truth be told, I find myself doing the same thing with Python/Django: I run the full Apache/mod_wsgi/Django stack, and start and stop between iterations… but there’s no compile cycle! Huge restart difference! Every once in a while I run a ‘make clean’ target just to ensure all my .pyc files are fresh, but my stop/start cycle is practically instantaneous, because Python doesn’t compile the way Java does, and Apache is faster than Tomcat.

Of course, as I look to the future, I can easily see myself saying “I will never program without Lisp-style macros again!” The baseline keeps moving up.

I think Paul Graham is on to something when he says general-purpose programming languages are evolving towards lisp. (I just wonder if Lisp’s syntax can be decoupled from Lisp’s feature set.)

But for now, I’ll enjoy my REPL.

From J2EE to Django: Observations from Porting a Web App 8 July 2009

Posted by manniwood in Django, J2EE, Java.
15 comments

I ported a database-backed web application from Java Servlets to Python/Django, and I’m so very happy that I did. I was nearing the end of work on a medium-sized web app when one of my programmers left for greener pastures. I decided that I could make up for the missing programmer by porting our J2EE app to Python, and then finish adding the final features to the Python port. The assumption was that Python would make me so much more productive that I’d finish faster even including the porting time.

It turns out I was right.

If you’ve taken to reading my blog at all, you’ll know that I love RDBMSs, and that my application designs use the RDBMs as the final arbiter of what the application’s data model is. So really, I wasn’t translating a huge amount of code for my port, because I could leave the database alone.

Of course, because I think the database is king, I opted out of using Django’s ORM on top of my carefully designed database. Instead, I ported iBATIS (which I was using in my J2EE version) to Pybatis, and used that for all of my database access needs.

I originally wanted to make Django’s templating system work with Pybatis (and maybe I still will some day), but I could not find an easy way to make Django’s templating system run well outside of Django, and I wanted Pybatis to be able to run in Python apps that were not neccesarily Django-based. So, I turned to the Jinja 2 templating engine, and liked it so much that I am now using it in both Pybatis and my Django app.

You may wonder what parts of Django this leaves behind, but, in my opinion, the parts of Django that I’m using are the parts that I had no desire to write. I love the way Django maps urls to functions, for instance.

I love the way Django renders templates to the browser. There’s no complicated forwarding from servlet to JSP as there is in Java-land; just a method call to the template engine, which you can hand whatever data structures you need for page rendering. It may sound like a small difference, but somehow, in practice, it’s a big difference. Like many things Java, Servlets and JSPs feel over-architected after using Django.

I also love using mod_wsgi and Apache with Django. It feels like a more natural fit than Apache with Tomcat. My production environment is Linux, so Apache is therefore process-based and not threaded. I prefer processes over threads as a general rule (I agree with Eric Raymond that threads are a performance hack; you can’t always avoid threads, but it’s nice when you can). So getting threaded Tomcat (or any other Servlet container) to talk to process-forked Apache always struck me as a bit of a mismatch.

With Django, there’s no mismatch: through mod_wsgi, each Apache process contains its own Python interpreter, and each Python interpreter contains a single copy of the web app. All kinds of good things come from this. For instance, my Python code does not have to be thread safe: each Apache process handles one request at a time, and each Apache process contains exactly one instance of the web app, so there’s no contention for resources. (Except on the database, but databases are designed to deal with that.)

With this setup, you don’t need connection pooling. Because Apache processes handle one request at a time, and because each process contains its own copy of the web app, each web app instance only needs to be configured with a single connection to the database. You just write your code to use The Connection, and the rest is taken care of for you by Apache’s pre-forking goodness. You just need to configure your database to handle as many open connections as your Apache setup will fork.

So not only does all this feel cleaner and simpler (though perhaps a tad more resource hungry—there are always tradeoffs), it’s also got horizontal scaling built in!

Well, mostly. There’s the problem of sessions. Apparently, Django can be configured to persist session data to shared memory, so that it doesn’t matter which Apache process responds to a user’s request, it has access to that user’s session. I decided to write my own session implementation that persists all session data in the database. (I don’t imagine this would work with large traffic volume, but for our product’s projected traffic volumes, this was fortunately an approach we could take.) That way, it wouldn’t matter if a user established a session on one Apache process and then returned to another Apache process on a different machine in our server farm: with sessions being stored in the database, they would be findable by any Apache process on any machine.

All in all, I’ve been very happy with the switch from J2EE to Django. Although there aren’t always Python equivalent to Java libraries I got used to using, I also found that porting to Python only the parts of Java libraries that I used took very little time.

The realities of the market are such that my next project may be a J2EE project: it’s still the most popular stack, so the opportunity to use Python is not always there. But if I’m in a position to choose, I’ll choose Python. If I’m in a position to offer a recommendation, I’ll recommend Python if I think the library support is there, and the project can benefit from that choice.