Misfiring Neurons Just another geek with a blog

8Apr/090

Annotation-based dispatch for Spring ApplicationEvents

I created a little extension that allows you to dispatch Spring application context events in a clean and type-safe way to POJO beans. Instead of implementing the ApplicationListener interface, one can simply create a handler as follows:

public class EventListener {
    @EventHandler
    public void onAppEvent(MyCustomEvent event) {
        // ...
    }
}

The code is available as a zipped up Maven project or via Git:

git clone http://pavel.tcholakov.net/code/eventhandler-annotation.git/

Related Spring forum thread.

26Dec/080

Quick and easy image resizing with Java ImageIO

There are quite a lot of examples of image resizing floating around the web but they all seem to access all sort of hidden features and classes deep inside the JDK to achieve their goal. Here is the most straightforward method that I could come up with - all in all just 5 lines of code. It requires ImageIO and requires Java 1.4 or later to run. If the ImageIO.write() method accepted plain ol' AWT Image arguments it could have been reduced further still to three lines, but you have to give it a BufferedImage instance as input.

The snippet below assumes that there exist the following variables (coloured green in the code):

  • an input stream to read from
  • a desired width variable (specifying the height as -1 tells the toolkit to preserve the original aspect ratio)
  • and an output stream to write the encoded image to.

Sample code:

import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
// ...

BufferedImage sourceImage = ImageIO.read(inputStream);
Image thumbnail = sourceImage.getScaledInstance(width, -1, Image.SCALE_SMOOTH);
BufferedImage bufferedThumbnail = new BufferedImage(thumbnail.getWidth(null),
                                                    thumbnail.getHeight(null),
                                                    BufferedImage.TYPE_INT_RGB);
bufferedThumbnail.getGraphics().drawImage(thumbnail, 0, 0, null);
ImageIO.write(bufferedThumbnail, "jpeg", outputStream);
21Jun/080

Effective Java Updated

Brush up on your Java 5 bag of tricks with this talk by Joshua Bloch based on his updated Effective Java book (can't recommend it highly enough).

A colleague of mine commented that this book has about the same information density as maths textbooks, so don't be mislead by the low number of pages - it is excellent value for money ;-)

10Jun/080

QCon Enterprise Service Bus Presentation

Jim Webber and Martin Fowler discuss ESBs, agile methods, and... ASCII man-boob p0rn. Must see!

Video at InfoQ.

3Jun/080

JetBrains’ Dmitry Jemerov on Scala

I can rave about JetBrains' IntelliJ IDEA until the cows come home - it is simply a superbly executed and very well focused product. What I found especially interesting  is the following quote on Scala becoming a dominant language for the JVM:

I don't believe that, however: Scala is very complicated, it's tricky, and has a lot of surprises and edge-cases. I would say that Scala is at least as complicated as C++, and with C++ you need a hundred-page style guide before you even start writing C++ code, otherwise you'll end up writing C++ code that nobody will understand.

Scala gives an impression of great elegance and simplicity at first glance but the same goes for Perl as well. The one feature of Scala I really like is the language-level support for traits which allow reusing multiple concrete implementations similarly to multiple inheritance, but with fewer gotchas.

Via Artima: JetBrains' Dmitry Jemerov on IntelliJ 8, Flex, and Scala.