Skip to content

Category Archives: Core Java

Java Logging best practices using slf4j and logback

Overview Slf4j to logging is what JDBC is to Database access: an interface that streamlines logging api, plugs into several implementations at deploy time, and promotes best practices of logging. When should you use slf4j? If you believe coding for Interfaces works well in your project(s), then you need no explanation. Though slf4j is “not [...]

Generating XML Schema and Classes using JaxB and Maven pom

Xml Binding via JaxB is a standard way to work with Xml objects in Java 6 and beyond. While there are ant tools and cmd line utilities to automate this, externalizing the automation in a dedicated maven module, along with nice wrapper methods for a fluent api would be nice. Below are the steps to [...]

Add session user info to log4j logging (custom file appender)

Ever wondered how to add additional session related info in log4j logging? I like to see logged in user, along with session-id info in logging messages in applications, where analyzing secure operations becomes critical (healthcare, financial apps). Here’s a sample filter log4j’s MDC to add diagnostic context in log messages. package com.cramer.srf.web.filter; import java.io.IOException; import [...]

Java PURL (Personalized URL) Generator encoder/decoder example

Have you ever noticed the unscubscribe url pattern when you unsubscribe from an online promotion or retail marketing campaign? It looks something like this: www.somesite.com/unsubscribe/u/NSFAHPF/aW5aaW1pdHk Ideally, you would want the user to click that (personalized) URL and the system should automatically unsubscribe, or perform any simple non-secure operations like registering for a poll. How would [...]

an interesting class loading issue with quartz scheduler timing (NoClassDefFoundError)

My quartz job schedulers had a startDelay of 0 ms and a repeatInterval of 10 mins and 5 mins each. On my local environment (win/mac) and sandbox (unix) the scheduler works just fine. But in our production environment (unix), we were getting spurious java.lang.NoClassDefFoundError: Could not initialize class Foo. Now Foo was one of the [...]

The best 59$, a java developer can ever invest

If you don’t buy this now, you’re either dumb. Or just plainly stupid. For the impatient: it’s a small jar that lets you compile/run/deploy java instantly, like php. I’ts the real HOT DEPLOY that jvm lacks out of the box. No more restarting the server, ever again, even after reconfiguring Spring beans. I used this [...]

The next generation of building Swing Applications

A lot of you (including me) have forgotten about Swing, ever since Web 2.0 has taken precedence over rich clients. However, good is coming back! I was browsing through the Java 7 features the other day and came across Swing Application Framework. It brings in the best of the breed concepts of Web and Desktop [...]

Horrible Java Generics syntax

Class Formatter public interface Formatter <t extends Foo> { public List<list<string>> format(List<t> foos); } Class FooFormatter implements Formatter and is strongly typed public class FooFormatter<foo> implements Formatter { public List<list<string>> format(List<foo> beans) { return null; } } When you compile the above (or see the red marks in your IDE), you get the following compiler [...]

Converting checked exceptions to Runtime, as a language feature

You might already know how frustrating it is to add a try catch block on those java.io api and date parsing API. The reality is, when such exceptions occur, something terrible most probably happened and you don’t want your application to proceed normally. Ofcourse, you can have a try catch block and convert checked exceptions [...]

The Prototype Dollar

I have been slowly picking up javascript recently. I’m, well, not really impressed, but sometimes I find things I like about it. Here’s something I like; the Prototype library adds a function, the dollar function, that looks like this: function $() { var elements = new Array(); for (var i = 0; i < arguments.length; [...]