Skip to main content

Are You Receiving Me?

Posted by editor on September 1, 2006 at 7:50 AM PDT


More on Swing and closures

So I had another one of those "jeez, I wish I had closures" moments last night. But first, let me digress into how a lot of desktop people are talking about the closure proposal right now. Over on the forum thread Closures and Swing, there's a pretty wide-ranging discussion of the various ways to wire behavior to a button, from anonymous class ActionListeners to Actions, to what I think is a prooposed onAction() method for JXButton (my apologies for not getting this right... JXButton seems to be in the Attic, so it's hard to tell what is and isn't real in SwingX-land). These options are then compared to closures with an eye to whether closures provide any genuine value beyond syntactic sugar, and if that's the case, whether it's still worth doing because the existing ActionListener scheme is so difficult for beginners to understand.

That's all well and good, but while we're still talking about function pointers, the idea of enclosing state, one of the key concepts of a closure, really doesn't matter in this kind of discussion, which makes it debatable as to whether this is the right argument to have about closures -- if closures help the hard cases, and are just an option for the easy ones, is that a good reason to add them? Or will they end up making the easy cases hard, which most agree is exactly what Java doesn't need right now.

OK, here's my case, which makes a stronger argument for real closures. I'm working through a QuickTime for Java bug with some Apple engineers and they want me to duplicate my bug -- a crash when bringing up a settings dialog -- in their code. Their code is an Applet, mine's an Application. What I want to do is to get their app running and then, after Applet.start() has done all its work, simulate an asynchronous launching of the dialog. So I decide what I want to do is to just wait five seconds and then bring up the dialog.

So, I need an inner thread. And the inner thread needs to be able to get two local variables (one to stop and start the capture and the other to show the native settings dialog) that are local to Applet.start(). So I have to write an ugly hack like this:

final SequenceGrabber mySG = sg;
final SGVideoChannel myVC = vc;
Thread waitAndDialog = new Thread() {
    public void run() {
        // lots of try-catches removed for clarity
        Thread.sleep(5000);
        mySG.stop();
        myVC.settingsDialog();
        mySG.startRecording();
    }
}
waitAndDialog.start();

Now this is the kind of thing I need a closure for. It's not just that I need a function pointer to describe the threaded functionality, it's that that code also needs two local variables whose values I only know at the time I create the Thread. The final gives me a way to do it, so I'm grateful it's possible without major code reorganization, but it's pretty ugly. The less ugly (but far more verbose) alternative would be a named inner class, which would take the SequenceGrabber and SGVideoChannel in its constructor... split off from the section of code where it matters, this option seems even worse.

Is this worth doing closures for? Hard to say -- was it worth it to add generics so the compiler could pre-emptively prevent ClassCastExceptions from the collections classes? I never thought it was that big a deal (because I could either account for every use of my collection, or because it didn't kill me to throw in an "if X instanceof Y"-type check), but a lot of others thought this was worth changing the language for.

So how many people are doing these kinds of crazy one-off anonymous asynchronous Threads anyways? Are you, say, firing off a database query (with some query arguments only available as local variables) and populating a Swing JTable asynchronously when the results come back? Are there other good use-cases for closures?


In Java Today,
the Enterprise-class WSRP Open Source Project aims to develop a WSRP v1 producer and consumer, support registries that allow publishing and discovery of remote portlets, and support propagation of user identity and other security capabilities between producer and consumer. The project is part of the larger Portal Open Source Project.

A recent "meet the engineer" article on SDN features an interview with java.net blogger Tom Ball. In Meet Tom Ball, Technical Director at Sun Microsystems, he talks about Jackpot's ability to transform and re-engineer Java code and the accessibility of the Jackpot engine. He also advocates writing "boring code" whenever possible, talks about the surprising history of the JDK's Java Sound implementation, and shares stories about the early development of Java.

A recent item on the Java Web Services and XML community page considers Four Alternatives for Distributed SOA:
"Service-oriented Architecture (SOA) provides the vehicle to evolve from server-centric to network-distributed computing. But why would organizations want to do this? Business-wise, distributed SOA aligns technology with business goals which in turn fuels organization capability and productivity. From a technology perspective, it enables broad system integration, practical code reuse and speeds up application development. In other words, SOA offers a value proposition to achieve demonstrable, quantifiable competitive advantage for the organization."


The latest java.net Poll asks "Do you use Groovy?" Cast your vote on the front page, then check out the results page for current tallies and discussion.


Project organization is on the agenda in today's Forums,
In
HTTP Load Balancer availability and enhancements in GlassFish v2, pj126383 writes:
"GlassFish v2, introduces a software based HTTP Load Balancer. As a core functionality it provides support to, policy based, load balance HTTP requests across cluster/group of Sun Java System Application Server 9.1 (GlassFish v2) instances. Please provide your feedback/comments on the proposed enhancements to Sun Java System Application Server 9.1 (GlassFish v2) HTTP Load Balancer. The document enlisting these enhancements is made available at LB-onepager.txt The feedback/review period closes 2 weeks from today. Any comments received after that would not be considered for this release (GlassFish v2)."

kwalsh announces that
JAX-WS 2.0.1 nightly builds to java.net jax-ws project have resumed!:
"JAX-WS nightly builds and postings of binary and source installer jars have resumed! Please download JAX-WS 2.0.1 nightly bits and test them out. If you find problems(bugs)submit them in issues tracker for timely resolution. Of course, if you need greater stability download the more stable milestone release As always, thanks for your patience."


Fernando Lozano does some seriously heavy lifting for you in today's Weblogs. In
Afraid of trying JPackage?, he writes:
"Most Linux developers and sysadmins I talk to think JPackage is great, but repackaging Sun Java looks like mission impossible to them. So I created a shell script, that does everything you need to bootstrap your JPackage environment except for downloading Sun Java."

Joshua Marinacci pushes more pixels in
Windows L&F Bugs: Part 2, in which
"Joshua covers some Windows Look and Feel bugs indepth, with more screenshots."

Finally, Richard Bair blogs on
XMLHttpRequest and Swing
"Say that again? XMLHttpRequest and Swing? Yep, you heard me right. Yesterday and today I've been experimenting with implementing a version of the esteemed XMLHttpRequest (from the Ajax world, of course!) for use with Swing apps. Actually, today's blog goes a lot futher that simply XMLHttpRequest. It also traverses the little-explored corner of the country known as JSONandSwingland. Wow, it is a good thing I brought my hiking boots. And some spare water."


In today's java.net
News Headlines
:

Registered users can submit news items for the href="http://today.java.net/today/news/">java.net News Page using our
news submission
form
. All submissions go through an editorial review before being
posted to the site. You can also subscribe to the href="http://today.java.net/pub/q/news_rss?x-ver=1.0">java.net News RSS
feed.


Current and upcoming Java
Events
:

Registered users can submit event listings for the href="http://www.java.net/events">java.net Events Page using our href="http://today.java.net/cs/user/create/e">events submission form.
All submissions go through an editorial review before being posted to the
site.


Archives and Subscriptions: This blog is delivered weekdays as
the Java
Today RSS feed
. Also, once this page is no longer featured as the
front page of java.net it will be
archived along with other past issues in the href="http://today.java.net/today/archive/">java.net Archive.

More on Swing and closures