Have you hugged your StackTraceElement today?

More J2SE revelations today as I discovered the method, StackTraceElement[] Throwable.getStackTrace(), introduced in J2SE 1.4. So what’s a StackTraceElement? Essentially, everything you’ve been seeing in a line of a printed stack trace for years – class name, method name, line number, and a convenient toString() method that returns the link you’re used to seeing in your traces.

Clearly, this sort of information has been hidden somewhere for years, kudos to the folks that put this into the spec for exposing to developers. I can think of a lot of things this could be abused – some people might use the source of the exception in place of error codes in designing how they handle the exception (if traceElement.getClassName, or worse, lineNumber == “com.moron.App” then handle one way, else handle another). One scenario that comes to my mind is for tracking down bugs. Suppose you have an Exception that’s unexpectedly being thrown at infrequent intervals, but when it happens you want to give it immediate attention, and suppose it’s a commmon exception type. Rather than changing your code, you could write an AOP interceptor that traps that common exception, checks if the source of that exception was the problem spot, and takes some notification action (perhaps logging to an SMTP log appender or something). I’d be curious what scenarios others more familiar with this distant corner of Java have done

Are you going to use this every day, week, or month? Not at all. But I’m thinking it’s a handy thing to keep in the back of your mind.


"Rediculous" – Spelling word of the day

The correct spelling is ridiculous, derived from the same root as ridicule – few people write redicule. I’ve seen this abomination in spelling become very common online recently. Maybe you have friends who pronounce it reee-diculous, but that doesn’t change the spelling. I never cease to be amazed at how many college educated people I know with not a prayer in the world of spelling simple, common words. Don’t even ask about writing coherent sentences. How do we turn out so many highly-educated people with so little ability to use our primary language, much less learn a second language? It’s so common that it’s ridiculous.


Ashlee Simpson (train wreck) Roundup

Much of the U.S. is fascinated by train wrecks, both figurative and literal. From the “rubbernecking” phenomenon in traffic to the obsessive tabloids at the grocery store, we are fascinated by sudden and overwhelming destruction. I’m usually NOT the one obsessed with celebrity culture, and the one who actually attempts to focus on the road ahead more than the wreckage in the emergency lane, but the interruption of an otherwise boring, lopsided national title game between Oklahoma and USC by a travesty of a halftime show has me fascinated for pure, unintentional humor value.

I despise the invasion of privacy conducted by rags like the Enquirer, InTouch Weekly, etc, simply to sell magazines, but public performances are fair game. Clearly, technical difficulties hampered all 3 performers in Tuesday’s Orange Bowl – Kelly Clarkson, Chet Adkins, and Ashlee Simpson, but the train wreck of the night was Ashlee Simpson (which is saying something, given the Drubbing the Oklahoma Sooners received). Her performance made viewers WISH she was lip-synching like the incident that embarrassed her on Saturday Night Live. From the start of her singing career, it has been clear that she should have simply continued the acting career she began on 7th Heaven. On her MTV show, her vocal strains attempting to record “Pieces of Me” should have been clear indications that her singing career should have been short-lived, but MTV flexed their marketing muscle to the masses who are clearly tone-deaf, and a platinum-selling album was born. (As an aside, I grant her sister Jessica, points for having vocal talent. A simple listen to Jessica’s first single demonstrates that the girl has some pipes, even if lacking some common sense and originality.) I don’t wish failure on anyone, but I resent being sold a fake bill of goods, which is what much of modern music is designed to do. Thus I enjoy those rare moments when it’s crystal clear that the emporer has no clothes.

That being said, there are a few links I found this evening that are well worth sharing:


Gracefully Shutting Down Java Apps

In J2EE, many components such as EJB and Servlets give us convenient object lifecycle events to handle setup and cleanup of resources, but did you know that J2SE supports similar functionality? Of course you did for setup. Constructors, static initializers, and member variable initialization give you several options for setup. But in the case of objects, sometimes finalize() just doesn’t cut it. In Windows, I ran a java class with a finalizer and used a ctrl-C to exit. The finalizer didn’t run, since finalization on exit is not guaranteed by default, and may be unsafe. finalize() only executes on garbage collection, and is not guaranteed.

So in a J2SE application, how can you ensure that resources are cleaned up due to an abnormal shutdown? Well, there’s a little gem java.lang.Runtime.addShutdownHook() that takes an unstarted thread as an argument and runs that thread when the system shuts down. So if you had something like a connection pool in a J2SE application, and you needed those connections to die gracefully, if possible, you could define a shutdown hook to close those connections as the JVM shuts down. A kill -9 in *nix or “terminate process” in Windows will still bypass this, but a gentler request such as a kill -3, ctrl-c, or End Task will trigger your cleanup code and let your app die gracefully.

This feature has been available since J2SE 1.3, and while it only solves a few specific types of problem, it’s an interesting tool to add to your bag of tricks.