Jazoon 2012: Messaging in the cloud – why do i care?

29. June, 2012

In his talk “Messaging in the cloud – why do i care?“, Oleg Zhurakousky showed some examples why you should know about messaging even if you don’t use cloud computing.

What is messaging? When a producer sends a message to a consumer over a channel/transport.

What kinds of messaging are there? Point-to-point (P2P) and publish/subscribe. An example of the former is writing a file to hard disk – you don’t expect that file to appear in several places. The latter is used in mailing lists.

P2p can be active or passive. In the active scenario, the consumer gets the message immediately. Example: Watching a web page in your browser. You wouldn’t want the browser to tell you “go drink some coffee, I’ll let you know when it’s done.”

In the passive case, the message is stored somewhere so the consumer can process it at its leisure. You mailbox is an example for this (off- and on-line).

All messaging systems are only one-way. If the consumer can reply, the implementations always make the consumer a producer. Think web sites. Your browser (producer) send a message to the server (consumer): “I want to see this page”. Then the server becomes the new producer when it sends data to the browser (new consumer).

As you can see, we’re using message based system all the time. What makes them so interesting?

They are easy to set up, easy to maintain and easy to make fault tolerant. For example, you can have these generic kind of consumer in your network:

  • Transformers – Turn one kind of message in another. XML to JSON or CSV, binary data to text, insert data into a database
  • Filters to ignore some messages without changing the code of the consumer
  • Routers to redirect messages to consumers that are interested in them
  • Splitter that can copy (parts of) a message to several consumers (distribute part in map-reduce framework)
  • Aggregators that can join several messages into a single one (reduce part in map-reduce framework)
Messages also allow you some nifty tricks like sending a message again after a timeout. If you keep a claim check, you can easily make sure that the receiver will get only a single copy of the message.

Related:


Jazoon 2012: Syntactic Salt and Sugar

29. June, 2012

Syntactic Salt and Sugar was a presentation given by James Gould and Alex Holmes. They were talking about some recent developments and whether they are good (sugar) or bad (salt).

DSLs

DSLs are becoming ubiquitous. Everyone wants, needs and does DSLs today. But think of this for a moment: Is SQL a DSL?

Scary thought, eh? It’s certainly a limited language but since it’s Turing complete, the limits are more in the pain writing queries and not in the fact that it’s a language designed to query data sets.

The advantage of DSLs is that you can fine tune them to your domain. That can help to avoid a lot of confusion.

But …

  • There are five people on this planet who can develop a nice syntax that is easy to use, easy to read, easy to understand and mostly consistent. Guido van Rossum is one of them. You’re not.
  • It’s easy to fall for the “one more feature” trap in a DSL. The most important property of a DSL is that it’s limited. It’s not a general purpose programming language.
  • Getting the syntax right is very, very hard. It’s easy to define syntax in the Xtext grammar editor – as long as you blissfully ignore the consumers of your DSL. As soon as you try to make their lives easier, all hell will break loose. Do you allow trailing commas? How do you handle ambiguities? Did you make sure all error messages make sense? Is it still readable? Can you add features without breaking all existing code?
  • YALTL – Yet another language to learn

Default Methods in Java 8

In Java 8, you can add method bodies to methods defined in interfaces:

public interface Foo {
String getName() default { return "Foo"; }
}

Finally, you can have mixins in Java. Yay ^_^

Now, some people will wonder: Isn’t that multiple inhertiance?

Yup. And as usual, because of some “features” of Java, they had to implement this in a … surprising way. What does this code print?

public interface A {
    String getName() default { return "A"; }
}

public interface B {
    String getName() default { return "B"; }
}

public class C implements A, B {
    public void main() {
        System.out.println(new C().getName());
    }
}
Nothing – it doesn’t compile because the compiler can’t decide which method to call. But this one compiles:
public interface A {
    String getName() default { return "A"; }
}

public interface B {
    String getName() default { return "B"; }
}

public interface C extends B {}

public class D implements A, C {
    public void main() {
        System.out.println(new C().getName());
    }
}

If you’re wondering: Instead of inheriting directly from “B”, I added a new interface “C”. Now, “A” is “closer” and it will print “A”.

That means changes in A or C can modify the behavior of D. If you’re lucky, the compiler will refuse to compile it. *sigh*

No Free Lunch

Again, it’s easy to see that each feature comes with a cost attached.


Java Pitfalls: Covariant Return Types

25. June, 2012

Sometimes, covariant return types (= methods overriding existing methods which return a sub-type) can bite you. Especially when you want them to have annotations.

Ralf Stuckert wrote a good blog post about the reasons: The Covariant Return Type Abyssal


Eclipse Suddenly Takes Long to Start Unit Tests

10. May, 2012

When starting a JUnit test in Eclipse suddenly takes ages (the process starts quickly as you can see in the Console view but it takes ages until the tree of tests appears in the JUnit view), you might experience troubles with IPv6.

The background of the issue: Modern OSs assign your network cards two addresses, one for the old IPv4 and one for the new IPv6. When IPv6 isn’t configured correctly, Eclipse will try to connect via this route and it will take some 30 seconds for Eclipse and the JUnitRunner process to begin talking to each other.

The quick fix is to disable IPv6 or to tell Java to prefer IPv4:


Fun With Generics: Return Anything

13. January, 2012

If you want to return anything in Java, you usually use Object – with Java 5 and autoboxing, this even allows to return primitive types.

But with Generics, you can do better: You can return whatever the caller wants.

How? Like so:

     public <T> T get() {
        @SuppressWarnings( "unchecked" )
        T result = (T) ...calculate the result...;
            
        return result;
     }

Use cases: Calling unknown methods or reading field values with the Reflection API. Instead of cluttering your code with casts, just write (where ReflectionUtils.invokeMethod() uses the trick above):

     Map<String, List<Map<String, String>> map = Maps.newHashMap();
     Set<List<Map<String, String>> entries = ReflectionUtils.invokeMethod( map, "entrySet" );

Cool, eh?

Unfortunately, Sun’s javac isn’t smart enough to determine a common upper bound between int and Object. So if you need to return int, you still need a cast:

     Map<String, List<Map<String, String>> map = Maps.newHashMap();
     int size = ReflectionUtils.invokeMethod( map, "size" );

Debugging v2.0 With Chronon

1. December, 2011

“… Okay, let’s just set a breakpoint there … run again … continue continue continue next next next *argh* run again … continue continue continue next next … Why is it still null?? Who changes this field? Oh, damn … breakpoint … run again … where is that coming from? … oh no …”

Sound familiar? How often do you rerun your code just because you can’t step backwards in time? How often did you ask yourself “who is calling this method?” Or “where did this value come from?”

Meet Chronon. In a nutshell, Chronon runs your code, saves all state changes (method calls, variable assignments) somewhere and allows you to browse the result.

So with this tool, the text above would have been: “… Okay, it’s null here. Where does that value come from? *click* Oh, ok. Who called this method? *click* Oh, I see.”

This makes you find the source of an error much more quickly. Other questions that Chronon can answer:

  • Find all instances of a class that were created. A special window lists all exceptions that were thrown.
  • Step backwards (just like stepping forward)
  • Go to the code that printed a certain output on the Console (just click on the output in the Console window!)
  • Show me all the values of some variable (Post Execution Logging)

The tool is surprisingly fast. You’d expect something like that to hog your computer but collecting the data is pretty quick (just a small increase in the time it takes to execute the code; I didn’t time it).

It does take a mental leap, though. Stop thinking like you were executing the code. It’s more a specialized database browser where the data is all the states of your application.

Things that I don’t like:

  1. Opening a recording takes a few moments (<< 1 minute) while it “decompresses” the recording. I’m not sure what happens here. It seems stupid to compress the latest recording because chances are that you probably want to use it soon. But I guess this stage creates a couple of indexes so the UI can quickly navigate the data, so it’s just an unhappy label.
  2. The plug-in messes with my eclipse.ini! Bad plug-in, down! I understand that Chronon needs lots of RAM. And I think it does take your total memory into account (it allocated 2.5GB of my 8GB). Still, it should ask before doing something like that. And it absolutely should not do it again, after I reset the values to something sane. GCing 2.5GB of RAM does take a long time!

Peace Between Java and SQL

23. November, 2011

There are various attempts to get Java and SQL to behave with each other. We have JDBC, OR mappers like Hibernate and EclipseLink, language support like in Groovy. All of those have advantages and drawbacks.

JDBC is powerful but low-level. The API is not really friendly. You need to write a lot of boiler plate code for even simple tasks.

Languages like Groovy wrap JDBC to make simple tasks simple. The code becomes much more readable but changes in the database schema become runtime errors.

OR mappers try to turn a relational database into a OO database. It works better than you’d expect but it also causes odd problems and leaks into design of your code: You must no’t use the ID field in equals, hiding the session in a thread-local variable can cause exceptions when you use lazy loading, failing to understand the requirements of the OR mapper causes spurious bugs. At least the OR mappers will complain when the schema changes.

Enter jOOQ. It’s like a OO wrapper for JDBC:

  • You get all the power of JDBC if you need it
  • The readability of a fluent interface
  • The database schema is part of the code (so you get compile time errors if it changes)
  • You can iterate over results as if they were a plain Java collection

Related:


ZK

27. October, 2011

I’ve been developing web applications for the past few years. Recently, we evaluated many of the web frameworks out there to select one for the next major release of our product.

We looked at Wicket, GWT, Vaadin, ZK, Roo and a couple of others.

Wicket was quickly dropped from the list. It’s a nice framework but it lacks one important feature: A library of reusable, cross browser components. Yeah, there are a couple but they are all very basic and building complex UIs with Wicket is done in HTML and that’s just painful.

Roo is too slow and immature and suffers from the same problems as Wicket. Maintaining cross-browser compatibility for a complex web application is something that a small team of developers can’t do anymore today.

GWT was dropped because Vaadin is based on it.

That left Vaadin and ZK.

Vaadin looks good, it’s free and based on GWT which is backed by Google. The main issue with Vaadin is that the technology is … unapproachable. There is a decent set of components but tweaking them is a pain. Plus the Java -> JavaScript compilation takes a lot of time. It’s better than most other frameworks but doesn’t compare to ZK.

Why?

ZK has a big set of well designed components with a huge documentation how to tweak them. The docs explain in detail how the components are built from HTML elements, which CSS styles are used and how you can override them. There is even a visual CSS editor that helps doing this.

When working with ZK, you often run into situations where something doesn’t work but so far, I’ve found a good solution within a short time. This might also be possible with Vaadin but it didn’t happen for me.

There are a lot of powerful layouts to arrange your UIs. You can cleanly mix ZUL (the UI descriptions) with Java code to get the best from both worlds: A clean UI description and compact code to attach listeners, publish events and connect components.

Also, ZK hides the request cycle. This is one of the biggest source of problems with developers. Yeah, the request/response cycle makes it easier to write web browsers and server frameworks but it’s a major pain for application developers. With ZK, you’re writing code that looks like a desktop application. It’s a bit like GWT in this respect but GWT feels … “proprietary”. In ZK, a lot of the API is public. In GWT, a lot of the API is hidden away in final static factory methods buried in calls between 50 classes.

You can use JavaScript but the JavaScript isn’t hidden in pseudo-native functions. That ZK5 is based on jQuery is an additional bonus if you really need to get your hands dirty.

The demo page contains lots of useful examples (instead of simply listing the available components like many other frameworks). There is a sandbox where you can modify small ZK projects online.

ZK does have its share of problems, too. Testing is a weak point. Selenium probably works well enough for mouse-driven apps but if you have components which can be controlled by keyboard alone, Selenium doesn’t cut.

The documentation on the web site could be better; for beginners, it’s especially confusing that the documentation for ZK3 and ZK5 is hard to tell apart.

But all in all, I’ve been faster to solve any problem I’ve had so far with ZK than with any other web framework.

Well done.


Human-Readable File Sizes

6. September, 2011

Every now and then, you need to show a number to a human. In some cases, those numbers can range from very small to huge. File sizes are an example.

Here is a very short code sample how to get a nice file size for human consumption: How to convert byte size into human readable format in java?

Best of all: It’s four lines of code but it can handle SI and non-SI units.


Useful JUnit Helper Method: ignoreUntil()

16. August, 2011

We all know the pattern: A test fails but you can’t fix it right away. What do you do? Let it stay red? @Ignore it?

All those approaches have drawbacks. Red tests make me nervous. But when I add an @Ignore, I sometimes forget to remove it in a timely fashion. Enter stage ignoreUntil():

/**
 * Ignore a test until a certain date.
 *
 * <p>This method will make the test silently fail
 * (as if it had been ignored) until a certain
 * date.
 *
 * <p>For documentation purposes, you can give a
 * message that explains why it's ignored.
 */
public static void ignoreUntil(
    String date_YYYY_MM_DD,
    String message
) {
    Date date;
    SimpleDateFormat format
        = new SimpleDateFormat( "yyyy-MM-dd" );
    try {
        date = format.parse( date_YYYY_MM_DD );
    } catch( ParseException e ) {
        throw new RuntimeException(
            "Can't parse date [" 
            + date_YYYY_MM_DD
            + "] using format "
            + format.toPattern()
        );
    }
    Assume.assumeTrue( new Date().after( date ) );
}

Usage:

@Test
public void someTest() throws Exception {
    ignoreUntil( "2011-09-15", "See issue #78" );
}

Happy testing!


%d bloggers like this: