Monday 27 July 2009

The Buffer and the Stream

This might seem like the world's most banal post on serialization. It deals with the issue of how to write a Java object to a file (and the equally important task of reading it). However the use of the "oos" is not without its subtleties, mainly relating to the concepts of "flushing" and "draining". A "drain" of an "oos" is in some sense an incomplete "flush" of the self-same "oos".

String filePath = "/(directory)/MyClass.sel";
FileOutputStream fos = new FileOutputStream( filePath );
ObjectOutputStream oos = new ObjectOutputStream( fos );
oos.writeObject( instanceObject );
oos.close();

Also, we must be careful to catch any IOExceptions. writeObject writes the state of an object (readObject restores it). Incidentally, it is worth remarking that the above code has certain poetic qualities due to the repitition of "oos".

The "oos" can be flushed and it can also be drained. In the case of a flush, the command will write any buffered bytes and flush through to the underlying stream, in the case of a drain, the bytes are not flushed thru to underlying stream. In each case there are two actors, the buffer and the stream. The buffer is the boat that floats on the stream.

The "oos" also has specialist write methods e.g.

writeBoolean(boolean val)
writeByte(int val) - writes an 8 bit byte
writeShort(int val) - write 16 bit short
etc

Tuesday 7 July 2009

Mock Objects Made Easy, But WHY?

Check out EasyMock generates objects on-the-fly using Java's proxy mechanism. It was presented at OOPSLA 2001.

Thursday 25 June 2009

Finding Bugs in Java Native Interface Programs Requires Research

Kondoh San and Ondera San from IBM Research Tokyo outline in a research paper some techniques to find bugs in JNI programs. Read their research! JNI is itself not intractably complex to program but some of the problems it can throw up in real systems can seem intractable. Also read up on IBM Research Tokyo. Research areas include Systems Software and Microdevices.

Saturday 20 June 2009

Sun's Bloggers Get Bored of Blogging on JMX (And who can blame them)?

JMX is a monitoring technology. JMX is part of Java platform from J2SE 5.0. JMX can be used in conjunction with jconsole. Also check out Daniel Fuchs blog on JMX and SNMP (though lately - and to his credit! - he seems to have lost interest in it). A fundamental concept of the JMX API (which I think of as Java Narnia) is managed beans or "mbeans". Creating an mbean is as simple as creating an interface and an implementing class where each of the class methods represent an operation on the mbean - what could be simpler? The interface for the implementing class must be the name of the class with the suffix MBean added to it. It sounds very silly. That's because it is. Things become more interesting maybe when we start to talk about JMX agents.

Thursday 11 June 2009

The Sceptics Guide to Spring 3.0

The 50MB download of Spring Framework 3.0 has begun. More comprehensive REST support and a codebase fully updated for Java 5 await. What more could a Java aficionado desire?

Want to pry into the mind of a Spring-meister? Don't ask me, let Rod Johnson be your guide. Spring feels so much like the next JBoss. I'm not saying Spring and JBoss are equivalent, their completely not, but the whole upstart, hyped-up nature of Spring is just so reminiscent of JBoss, which points hard to its inevitable obsolescence. Given this outlook, why even bother trying to learn Spring? Well, because it is a "thermometer of our times" and the software concepts in Spring may well find life in some future products so the learning effort, we might assert, will not be entirely wasted. If we can overcome this emotional antipathy towards Spring, and overlook its incongruous rhyming nature with another, very familiar Java toolkit (Swing) we might be able to make some headway in terms of learning this technology.

To comprehend the incredible complexity of Spring, you need to first walk the path of dependency injection and "inversion of control". Nobody likes jargon, least of all pretenious, newly-coined terms like "inversion of control", but alas, one needs to have an acquaintance with aforementioned terms as a prerequisite to proceeding with Spring. It is just one of those rites of passage. Think of it as a bridge that once crossed will render the perceived complexity of Spring powerless.

Dependency injection is a specific example of "inversion of control" (which is a very vague general term that can be used for a lot of code patterns). Fowler prefers the term dependency injection in order to be more clear. Dependency injection, to put it crudely, is storing settings in an xml config file which can be read into a Java object (I will not use the term POJO as it sounds ridiculous imho) which is then used to initialise other objects. Dependency injection is no different from what you see in any software application that has configuration information. It's just another word for "config".

Dependency injection is like any other design pattern, a fancy name for something programmers have been doing all along.

Eclipse RCP

There is a slew of awesome commercial RCP applications available, check out the online application inventory. Among them is SpiderMonkey for online SEC filings.

Here is a tutorial to get started (by Ed Burnette).

"Linking" to Third Party Jars in Eclipse (the Correct Way using Java Build Path)

How

Select Project->Properties in Eclipse.

Alt-P (Project Menu) -> Pause -> P (Properties)

Select "Java Build Path" from section on left. Choose "Libraries" tab and "Add External Jars".

Does it make sense to talk about "linking" to a jar

We are just referencing code, but then again, isn't that what linking is.

Creating Jars in Eclipse

Frequently we may wish to build an open-source Java library and export it as a JAR. Asssuming we have created a new workspace in Eclipse, e.g. Apache Commons, composed of multiple projects e.g. Lang (which we have imported into Package Explorer using "Create Java Project from Existing Ant Build File") we can create a JAR for the Lang project as follows:

1. Right click on Project in Package Explorer
2. Select Export..
3. Select Java/JAR file
4. Enter a name for the file e.g. project name and version number, like commons-lang-2.4.jar

Once the JAR is built (by default it will be in the directory for the current workspace), you can inspect the file as follows:

jar -tvf commons-lang-2.4.jar | grep class | wc -l

will return a count of 127 class files.

Tuesday 9 June 2009

Apache Commons - A Staple in the Java Open Source World

Apache Commons is a staple in the Java open source world.

commons-beanutils.jar (contains everything to do with beanies)
commons-collections-3.2.1 (contains apache commons collections)
commons-lang-2.4.jar (contains additional classes for java.lang)
commons-logging.jar
commons-CLI.jar (command-line parsing)

There is also a Commons sandbox of projects-in-the-making. Amongst these are:

CLI2 - redesign of Commons CLI
CSV - module for reading/writing CSV
OpenPGP - for signing and verifying data using OpenPGP
Javaflow - to capture the state of an application. Means of "checpointing" an application using the idea of "continuations" borrowed from Scheme.

Commons dormant contains stuff no longer maintained, like Clazz for introspection, and Cache for object caching services.

Saturday 23 May 2009

Implementing Aspect Oriented Programming by Hacking Java Class Files Programmatically

This is an Apache-licensed Java library for manipulation of .class files which can be useful when hacking together aspect-oriented extensions to Java. The Apache license allows you to use BCEL for commerical projects. The heart of BCEL is the JavaClass class, which represents a Java class in memory. A ConstantPool class represents the constant pool. BCEL supports something called load-time reflection, whereby these classes all become useful. I'll keep coming back to this point, because it is something that goes beyond the scope of the Reflection API. It can be used to fundamentally change the behaviour of Java classes.
  • BCEL supports load-time reflection. Run-time reflection is built into Java via the Reflection API, load-time reflection allows you to modify the bytecode instruction set at the time of loading the class. How does this differ from a custom classloader? Well, you still need to write a custom class loader, which instead of passing bytecode directly to JVM, first passes it through your run-time system using BCEL API.
  • It has a Static API to map structures described in the JVM specification. It also has a Generic API to create and transform class files dynamically, its heart is the ClassGen class, which allows you to create new class files, and add methods and attributes dynamically.
  • Comes with a bytecode verifier called JustIce.
  • Comes with a Class2HTML utility to generate html bytecode documentation for classfiles. This is interesting to see e.g. how synchronized code is implemented at the bytecode level.

So, if you want to write some bytecode manipulation programs, don't do it from scratch, but use BCEL as a basis!

Friday 22 May 2009

Java versus C++ Benchmarks

Here is an old, University of Southern California, study done on relative performance between Java and C++.

There are some academic reasons why Java may be faster than C++, in some situations.

Thursday 21 May 2009

How real-time is javax.realtime?

The Java real-time specification - at last! Real-time brought to Java. But how real-time is real-time? How is the garbage collector tamed?

Hype warning: "javax.realtime (pronounced JARVAX-DOT-REALTIME) ...the one, the ONLY incredible javax base package you will need for all your real time Java needs!"

Let us try to prove or disprove the HYPE-othesis.

The class RealtimeThread extends Thread and adds various real-time services. The thread can be created using a SchedulingParameters object. NoHeapRealtimeThread is a specialization of RealtimeThread.

An instance of NoHeapRealtimeThread (NHRTT) may immediately preempt any implemented garbage collector, logic in its run() is NEVER allowed to allocate or reference any object allocated in the heap. Byte-code-wise, it is illegal for a reference to a heap-alloc'ed object to manifest in the operand stack for the NHRTT.

The JamaicaVM is an implementation of RTSJ. It provides hard real-time guarantees for all primitive Java operations, as well as hard realtime garbage collector. It runs on Linux, Windows and Solaris and various OS's including VxWorks.

Wednesday 20 May 2009

Centering a JFrame

The old way to center a JFrame was to use the default java.awt.Toolkit object and call getScreenSize then explicitly call setLocation on the JFrame.


Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int x = frame.getSize().width;
int y = frame.getSize().height;


Since 1.4 the better way is to call setLocationRelativeTo(null). This will center the JFrame. If you supply a Component object to the function, and the component is say on the left side of the screen, your JFrame will appear to the right of the screen.

Monday 18 May 2009

An Alternative to IBM Codecs

FOBS, the open source king of codecs.

Sunday 10 May 2009

Creating Executable Jar Files

Java 1.2 brought with it the ability to create executable jar files. You can do this from eclipse using "Jar Export" wizard.

The Essence of Static Initializers

SIB Explained

If you write a "static" Java class, by which I mean a class containing only static methods and static data, you may need a static initializer block (SIB) to initialize some of the private data (for example, java.util.HashMap that stores some mappings).

SIB is used to init static variables when the class is loaded. It resembles a method with no name, no arguments and no return type (not allowed to contain a return statement).

It is executed by VM when class is loaded. An alternative to SIBs is to write a private static method to perform initialization.

Problems in Static Initializers

An exception inside a static initializer will result in an ExceptionInInitializerError (which is a java.lang.LinkageError). One solution is to catch the exception inside the SIB and handle appropriately. This error may happen if initialise a data structure statically (after the SIB block). In this case, the data structure may not exist if accessed from within the SIB block.

Java Packages

To create a package, put a package statement at the top of each source file containing the package types. This statement must be the first line in each source file.

Without the package statement, types end up in unnamed packages. This is acceptable only for small or temporary applications when you are just beginning the development process.

Package names tend to:
1. be written in lowercase
2. use reversed internet domain convention

Thursday 7 May 2009

What is the size of Eden?

What should the size of Eden be? Is 128mb sufficient?
What is the difference between a minor and major collection?

In Java memory is managed in generations, which are memory pools for holding objects of different ages.

GC occurs in each generation when it fills up.

Objects are ALLOCATED in Eden, and most objects die there. When Eden fills up, a minor collection occurs, during which some objects move to an older generation. When older objects need to be collected, a major collection occurs (this is often slower since it involves all living objects).

Saturday 2 May 2009

Running a Java Program from the Command Line: NCDF "Exception"

This is the most rudimentary task every Java newbie struggles with (and may take hours to resolve!) This post will aim to delve into the knowledge needed to succeed in this simple task, which requires a surprising amount of knowledge.

Having written (and compiled) a simple Java program how do we run it from the command-line? How many times has one tried:

java classname

only to be greeted by the all-too-familiar java.lang.NoClassDefFoundError. In an enterprise environment this error is almost never seen, since virtually every company running a large Java infrastructure implements a run script to solve all the

* issues of classpath
* package names/relative paths (the tight coupling between directory names and package names is a particular source of initial frustration and bewilderment when running Java programs for the first time).

This contrasts heavily with the relative simplicity of compiling and running C programs.

Java programs, as we know are compiled into class files. Suppose I have written a Java game called DinoGame that is compiled into DinoGame.class, which is stored in a directory called dinosaurs. To view contents of the DinoGame class, I do the following:

* cd dinosaurs
* javap DinoGame (NOT javap DinoGame.class)

This will tell me: "O i've been compiled from DinoGame.java and here are my methods". One of my methods is public static void main which takes java.lang.String[] (1-d array) i.e. the output of javap will always display fully-qualified package names.

Now I try running DinoGame by typing: java DinoGame from the dinosaurs directory. Oh dear! java.lang.NoClassDefFoundError: Could the find the main class: DinoGame. Program will exit. However!! java provides us with a useful hint (wrong name: dinosaurs/DinoGame). The java interpreter sees the class file in the current directory but it won't be able to execute it because it has the wrong name.

* cd out of the dinosaurs directory
* java -classpath . dinosaurs/DinoGame OR java -cp . dinosaurs.DinoGame (again NOT dinosaurs.DinoGame.class)

The -classpath . (or -cp .) is needed here since there are no class files in the current directory, so there are no reference points for the Java interpreter to use in generating useful error messages. To infer a problem such as the lack of a fully qualified classname would require the Java interpreter (run from a specific directory) to recursively search all subdirectories from the current directory which
could be very inefficient.

The tricky thing here is you need to think in terms of fully qualified package names. As we have seen from javap output, this is what the interpreter uses to refer to class files.

Now even if you get the above sequence correct (java -cp . class-with-fully-qualified-package-name), if you have a broken java.exe process running in the background (at least in Windows environment) whatever sequence of characters you type may not work and still crash with NCDF Exception (ok, ok I know it's written as NoClassDefFound ERROR, but what triggers it is the java.lang.ClassNotFound EXCEPTION, so for conciseness let's refer to it as NCDF EXCEPTION, since an exception is the underlying generator of this error).

Another useful point to remember is whenever you see a .java or .class file, remember that the Java toolset invariably requires you to OMIT the .java or .class extension when using your Java files as arguments to the tools (read the code in openJDK to see how many times String.concat(".class") appears! Forgetting this simple rule will result in frustration!

It is worth delving a little into the rather ungainly stack trace that appears when you get an NCDF error. It gives some insight into the core packages of Java (not just java.lang). It shows a tight interplay between java.security and java.net packages.

NCDF error is generated by CNF exception which is generated by java.net.URLClassLoader$1.run(Unknown source). The java.net.UCL will provide a starting point for some interesting analysis.

java.net.UCL (see source)is implemented by David Connelly (around 600 lines of code, well-worth reading, every single line provides essential knowledge) was introduced in Java 2 and implements the generified findClass method (generified in the sense it returns a Class object of wildcarded type); as a contextual note, remember generics were only introduced in Java 5. UCL.findClass is the fella that winds up throwing the "class not found" exception. It is effectively one call into native code: AccessController.doPriviliged(privileged action, created as an anonymous class).

This stream of events begins with java.lang.ClassLoader. method loadClassInternal (a synchronized method invoked by the virtual machine to load a class) which calls auxiliary methods loadClass(String name)->loadClass(String name, bool resolve) (classes must be resolved before they can be used). What does it mean to resolve a class, I hear you ask? Read the "Execution" chapter of the Java Language Specification. Resolution involves resolving symbolic references to other classes by loading thoses classes and checking the references are valid. Resolution is optional at the time of initial linkage.

Footnote:
Java security comprises a very large set of APIs and if you want a lowdown your best bet is right here).

Tuesday 28 April 2009

The Recursive Mechanics of java.lang.ThreadGroup

A ThreadGroup is a set of threads. Containment - a ThreadGroup can contain other ThreadGroups (which will be like "subtrees" of the original ThreadGroup). Threads within a group can share properties e.g. setMaxPriority will set max priority in group (method is called recursively, for every ThreadGroup in the ThreadGroup). A thread may only access information about its ThreadGroup.

ThreadGroup has a final checkAccess method to determine if the currently running Thread has permission to modify the ThreadGroup. If not, a SecurityException is thrown.

BeanInfo class - to control behaviour of new components/beans in Visual Editor

Implement java.bean.BeanInfo class to provide information about your bean. SimpleBeanInfo is a support class to make it easy to make BeanInfo classes. It has a method getBeanDescriptor which by default returns null. A BeanDescriptor provides global information about a bean. This is needed by builder tools.

Other critical things to know about JavaBeans:
1. Beans use events to communicate with other beans. Beans wanting to receive events (listener beans) register interest with source beans (which fire events).
2. Persistence enable JavaBeans to save their state so they can be restored later.

JavaBean configuration may be defined in a java.util.ResourceBundle object (which contains locale-specific objects). When a JavaBean needs a local-specific resource it can load it from a ResourceBundle for the specific locale. This has been around since JDK 1.1 and is consequently a very basic building block of Java.

The Legacy Oswego Libraries and their Manifestation in Modern Day Java

The CopyOnWriteArrayList of Java 1.5 has its origins in the legendary Oswego by Doug Lea. This is a thread-safe ArrayList implemented by (expensively) copying the underlying array for every mutative operation. It implements the generic List interface ("sequence" interface).

Another oswego staple imported into 1.5 is the ThreadFactory, an object that creates new threads on demand. The newThread method takes a Runnable interface and returns a Thread.

The Stronghold of Mark Reinhold - Java Collection Classes

Yoda may be a master Jedi, but Mark Reinhold is the primary implementor behind many of the Java collection classes. He has also been involved in the area of NIO high-performance APIs.

A recent interesting class of Mark's I stumbled upon recently (using jdb and typing 'classes' to pull a list of the currently known classes) is the PreHashedMap.java collection class in "package sun.util". This is build on top of java.util.AbstractMap (a skeletal implementation of the Map interface) and has been in orbit since JDK 1.5.

PreHashedMap use memoization to store precomputed hashes. Hashes are computed as the hashCode of an Object, right shifted by a fixed offset and binary AND'ed with a mask. The mask and offset are configured at startup in the constructor of the PreHashedMap.

Sunday 26 April 2009

PMD - "Project Meets Deadline"

PMD, also known by its backronym "Project Meets Deadline", is a static code checker for Java (kind of a Java lint if you will) currently on version 4.2.5.

You run pmd via bin/pmd java-source-file text rulesets/unusedcode.xml, as an example. There are lots of different rulesets and you can even roll your own ruleset. It is quite interesting to see how pmd defines rules.

I ran pmd on some of my own modest programming efforts using options: basic,imports,unusedcode and found no problems.

Tuesday 7 April 2009

The final keyword in Java can be applied to almost anything

The final keyword can be applied to many types of "code objects" in Java, classes, methods and variables. For example:

final class - implies that the class that cannot be subclassed
final method - implies that the method that cannot be overriden
final value - implies that the value that cannot be reassigned (but is NOT, I repeat NOT, immutable)

final has nothing to do with immutability. It also has no meaning in packages (unless I suppose all the containing classes are final).

Saturday 21 March 2009

Friday 20 March 2009

java.lang.OutOfMemoryError

Applications may sometimes terminate with java.lang.OutOfMemoryError. The error is thrown when there is insufficient space to allocate an object.

Why do I get this exception?

First, you must inspect the FULL error message: what comes after the java.lang.OutOfMemory error?

* example 1: Java heap space. Two possibilities here. Perhaps the specified heap size is insufficient for the application...in which case bump up the heap size using the -Xmx option. The alternative possibility is a problem in garbage collection e.g. objects that are no longer needed cannot be garbage collected because the application is unintentionally holding references to them. A common source of this error can be excessive use of finalizers such that the thread to invoke the finalizers cannot keep up with the rate of addition of finalizers to the queue. jconsole can monitor the number of objects pending finalization.

* example 2: Java PermGen space. "PermGen space" indicates the permanent generation is full. This is the area of the heap where the JVM stores its metadata. If an application loads a large number of classes the permanent generation may need to be increased (i.e. -XX:MaxPermSize=n, where n is the size).

* example 3: Requested array size exceeds VM limit. e.g. if max heap size is 256MB (2^8), an you try to allocate an array of 512MB (2^9 i.e. double the size) then you get the "array size exceeds VM limit" problem.

To diagnose OutOfMemoryErrors best tools to use are Heap Analysis Tool (HAT), jconsole management tool and the jmap tool with -histo option.

Saturday 7 March 2009

Multi-Language Java: Welcome to "Il Mondo Java"

The Internet is a huge resource for Java programming in multiple languages. Here's a primer on getting the hang of these resources.

Italian Java Phrases:

Sviluppatore - Developer
SO - Italian for "Operating System" (Sistema operativo)
Genere - software genre (e.g. Matematica, Sistemi operativi, emulazione, videoconferenza, compilatori, crittografia/crittazione)
Licenza - license (e.g. Gratis, BSD)
Il mondo Java - java world.
aggiungere e rimuovere - add and remove (e.g. plug-ins, add-ins)
estensioni - extensions
ambiente Java - Java environment
un computer potente - powerful computer
interfacce grafiche - GUI

Wednesday 4 March 2009

Dalla GreenThreads al NativeThreads

Does it make sense to talk about the Java threading model? Java, is of course, a naturally multi-threaded languages, but the threading model is mandated NOT by the spec but by the VM implementation, surely? In particular, what is the relationship between the Solaris threading model and the Java threading model?

threadingmodel( _Java, 1.1, GreenThreads). ? GreenThreads = simulatedThreadsInVm.
threadingmodel( _Java, 1.2, NativeThreads).

advantage( GreenThreads, _Linux, time(X), reason(Y)). ? X = earlyDaysOfJava, Y = saveCost(Z), Z=spawn(A, B), A=process, B=nativeThread.

threadingmodel( _solaris, X). ? X = manytomany, X = onetoone.
alias( manytomany, (_LWP|boundthread) ), alias( onetoone, _SolarisThreads ).
has_a( _LWP, kernelthread ). schedule(kernel, task(X)) :- on_a( X, _LWP ).
too_few(LWP) :- starvation(X), thread(X).


Sun's JVM also maps 1:1 to Windows threads (not multiple processes or fibers).

Thursday 26 February 2009

Welcome to "Il Mondo JavaBeans" (including "Il Mondo EJB")

Java beans are just Java classes that conform to a convention. Multiple objects are encapsulated into a single object (the bean).

Class must:
1. have a public default constructor
2. class properties must have get, set that obey a specific convention
3. class should be readily serializable - allow applications and frameworks to reliably save and restore the bean's state independent of vm and platform

GUI toolkits like AWT and Swing use JavaBeans conventions for their components.

Beans == way of writing component-oriented software in Java programming language.

Enterprise JavaBeans is the use of Java bean components to build an "enterprise app" in a modular way. The components must conform to the EJB specification (javax.ejb, javax.ejb.spi, javax.interceptor).
EJBs include entity beans (which essentially represent relational database tables), session beans and message-driven beans (MDBs). MDBs can receive messages asynchronously.

Monday 23 February 2009

Thinking like Eckel: Objects are Like Trains

Basics First: The wait/notify dynamic

One fine day, an instance of the java.lang.Object class had its wait() method invoked and application X came to a standstill. Why and wherefore did this happen?

void wait()

wait throws an InterruptedException. It causes the current thread to WAIT until another thread calls notify or notifyAll on the same object.

wait() == wait(0) == wait(0,0)

which means invoke wait with a zero millisecond timeout (i.e. never timeout). The current thread must own the object's monitor. Think of it as a UNIX process that sleeps until some event occurs or a bus that stops at traffic lights and must be "notified" either by the traffic lights or by a car honking from behind it! An object, to put it in baby terms, is like a "choo-choo train"! A "choo-choo" train with locks that can only be manipulated by synchronized methods! Tampering illegaly with the locks results in a vandalism-detection mechanism being triggered.

Some notes on wait/notify

There's a little more to wait/notify than meets the eye and some of this "extra insight" can be obtained from reading good Java books such as Bruce Eckel's Thinking in Java (Bruce Eckel is both a "Java Black Belt" and also founding member of the C++ Standards Committee, the two facts being not entirely unrelated). One key point he makes is that synchronized methods of an object can be called on an object in a wait state, precisely because calling wait releases the lock on the object. Because wait does jiggery-pokery with the object, calling wait outside of a synchronized method results in an IllegalMonitorStateException (present since JDK 1.0 so don't act all surprised!)

Saturday 21 February 2009

Java New IO: Channels

Channels (which came in JDK1.4) represent connections to entities capable of performing I/O operations e.g. files and sockets; and selectors, for multiplexed non-blocking I/O operations. It uses an idiom from telecommunications.

What is this business of multiplexing I/O operations?

In electronics, a multiplexer is an electronic component that performs multiplexing: selects one of many digital or analog input signals and outputs to a single line. Demultiplexers do the reverse. Ther term originated in telegraphy and became used widely in the context of computer networks. The two main forms of multiplexing are time-division (turn-taking) multiplexing (TDM) and frequency-division multiplexing (non-overlapping frequency ranges for different signals) aka FDM. Both are methods of "sharing a medium" / "sharing the airwaves".

The Java Selector class is a multiplexor of SelectableChannel objects. These implement the Channel interface, a nexus for I/O operations (nexus = form of connection. the hub).

Some of the interesting types of Channels are GatheringByteChannels and ScatteringByteChannels. Within this melee of channels we also see DatagramChannel and ServerSocketChannel in the mix.

Read the original JSR for the NIO library. NIO2 is currently in development (the expert group includes Sun, Doug Lea, IBM Google). By the way, Doug has a very interesting paper on design patterns for avionics control systems

To see how NIO may change in Java7 see the Draft API documentation.

Tuesday 17 February 2009

Debugging the Java Heap (jmap and other tools)

How to debug the Java heap?

There are many tools, one is jmap (shows you the heap, gc algorithm etc).

jmap -histo pid

shows the number of objects of each class in a given java process.

Debug Java Threads

jstack -l -m

-m means mixed mode (Java and native C/C++ stack frames)

Jargon

In output of jmap you'll see some jargon. For example:

CMS - Concurrent Mark Sweep (aka low latency collector). computes transitive closure of live references for garbage collection.
gc "ergonomics" - automatically selecting a gc algo based on conditions at runtime

Other Java Tools

jinfo - tells you the VM info you are running with
jps - ps for java processes, jps -l gives you the fully qualified classname
jstat - performance statistics for JVM

Monday 16 February 2009

Java 6 Changes

JCF Changes

The JCF introduced the Deque interface.

public interface Deque<E>extends Queue<E>


Methods to add/remove elements at both ends of the queue.

Back to Basics: Copying Arrays and other Array Operations

Copying Arrays

One way is to use the clone method which (by default) just does a shallow copy (so it's fine only for primitive one-dimensional arrays).

Another way is to use System.arraycopy(src, 0, dest, 0, length).

Arrays.copyOf(src, nitems) will copy nitems, filling in nulls where appropriate, and copyOfRange(src, a, b) will copy src from a to b. copyOf and copyOfRange are meant to be more efficient than System.arrayCopy.

Array Utils

java.util.Arrays - contains various methods for manipulating arrays (sorting and searching). Arrays has been around since Java2, and is part of the JCF.

Arrays.binarySearch( array, key ). Can be float array, double array, int array.
Arrays.fill(array, value)
Arrays.asList returns the array presented as a java.util.List.

Sunday 15 February 2009

Swing Vocabulary

JFrame, JPanel, JButton, BorderLayout, ActionListener

Axis of Apache

Java Web Services Technology
http://wiki.apache.org/ws/FrontPage/Axis

Axis uses SOAP protocol (Simple Object Access) for web services. It's a kind of XML-RPC.

WSDL is a specification to describe a web service (series of messages travelling between two endpoints). It is independent of the underlying protocol (e.g. SOAP).

In Axis, there is a java program, WSDL2Java, which generates client bindings for web service, described by WSDL. For .NET there is a corresponding program, wsdl.exe. Client-side Axis is built using JAX-RPC specification, a "key technology for web services integration".

Monday 9 February 2009

Back to Basics: The java.util.List, java.lang.StringBuffer and java.lang.StringBuilder

java.util.List is an interface that extends Collection. Lists allow duplicates, unlike sets.

Fundamental methods:
add(Object)
add(index, Object)
get(index)

index goes from 0 to size-1. List also implements indexOf, which gives the index of the first occurence of some object.

java.lang.StringBuffer is a mutable sequence of characters. The binary string concatenation operator + is implemented with StringBuffers by the compiler. Key methods on SBuffers are append, insert, reverse (which returns a new StringBuffer) and toString. The thing about StringBuffer is you can't do stuff like sb+="some string", you NEED to use sb.append!

As of Java 1.5, StringBuffer implements the Appendable interface, designating an object to which char sequences and values can be appended. StringBuffer is closely related to the (java.util.) Formatter interface.

StringBuilder is a drop-in replacement for StringBuffer when no synchronisation is required.

Saturday 7 February 2009

IntelliJ Settings

Cntrl-Alt-S Open Settings

IDE Settings->General
IDE Settings->Appearance: Set "Use antialiased font in editor", Change L&F (Idea4.5 default).

Java keywords in IntelliJ are highlighted in a horrendous dark blue colour. To change them to a more palatable light grey we can do the following:

Cntrl-Alt-S Open Settings

IDE Settings->Colors & Fonts

Set the font to Consolas, and set Java:Keyword:Foreground to light grey. Then when you read the code you are not obstructed by the "noise" of Java keywords and can focus on the functionality.

Wednesday 4 February 2009

Debugging the Java Debug Wire Protocol: No Transports Initialized

JDWP sometimes kicks up problems.

e.g.

FATAL ERROR in native method: JDWP No transports initialized, jvmtiError=AGENT_ERROR_TRANSPORT_INIT

(transports allow the debugger or VM to act as a server).

Asynchronous protocol, debugger sends 14 bytes, VM replies with 14 bytes (handshake). Communication then takes place via command packets (which can be sent by debugger or VM) and reply packets. Reply packets are sent in response to command packets (to indicate success or failure).

All fields and data is sent in big-endian format (higher-order byte at the lowest address, big end comes first). Just like Adobe Photoshop. Little endian seems more natural.

JDWP facilitates the remote debugging of Java programs.

Tuesday 3 February 2009

Prepare to be Bombarded by Java Marketing! Available NOW!

JavaFX is here, Java Buddies!!! The best marketing concoction from Sun since coinage of the name Java! Rock on, Java dudes! Immersive media written in Java??!! Bring it on!!!

Need a more techie take on this brazen new technology? Check out InfoWorld's article "Java Fights Flash". Hoo-ya!

Wednesday 28 January 2009

Attaining Ultimate Mastery of the Java Alphabet Soup

Java anagrams:

  • MMAPI - Mobile Media API. Audio/video API for resource-constrained devices.
  • Java WSDP (Wiz-dip)- Web Services Developer Pack
  • JAX-RPC - Java API for XML web services (part of Glassfish, open source application server). Glassfish versus JBoss is discussed here.
  • UTF-8 (UCS/Unicode Transformation Format) - preferred encoding for web. Backwards compatible with ASCII - hence popularity. Backwards compatibility is a great feature in computing. It allows one installed base to immediately transfer to a new installed base. Think C/C++. Java 6 Update 11 had a new UTF-8 charset implementation.
  • UCS - Universal Character Set. abstract charset on which many charsets are based.
  • JCP - Java Community Process
  • JSR - Java Specification Request

Acronyms for Java debugging architecture:

  • JDWP - Java Debug Wire Protocol
  • JDI - Java Debug Interface
  • JVMTI - Java VM Tool interface

Saturday 24 January 2009

Getting Groovy with SwingWorker TV

javax.swing.SwingWorker is the generic type for SwingWorker, which came in Java 1.6. Various incarnations of the SwingWorker existed prior to 1.6 though. T is the result type returned by various methods, and V is the type used for carrying out various intermediate results. The class implements the RunnableFuture interface.

The primary purpose of a SwingWorker is to do lengthy GUI-interacting tasks in a dedicated thread. Subclasses of SwingWorker must implement the doInBackground() method to perform background computation.

The concept of a Future<V> is an asynchronous computation. Methods exist to check if the computation is complete, to wait for completion and retrieve the result of the computation.

A FutureTask<V> is a cancellable asynchronous computation. This class also implements the RunnableFuture interface.

Thursday 22 January 2009

ClassLoaders 'R Us

The class ClassLoader is an abstract class. Given the NAME of a class, a class loader should attempt to LOCATE or GENERATE data that constitutes the definition of a class.

For more information, consult the JLS.

Saturday 17 January 2009

Castor

To quote castor.org, Castor is an "Open Source data binding framework for Java[tm]", the "shortest path between Java objects, XML documents and relational tables". We have Java-to-XML binding, Java-to-SQL persistence and more. Looked at in reverse, you can think of it as a Java persistence layer. It was developed by some guys at Intalio, Inc. Castor uses JUnit to build and test components.

Sunday 11 January 2009

Spring Framework and Aspect-Oriented Programming

IntelliJ has full support for the Spring framework which supports Aspect-Oriented Programing
(AOP).

AspectJ is another AOP framework.

The idea of AOP is an application or service has various "aspects" such as logging, perfomance monitoring, exception handling etc.

Dependency injection and AOP are complementary techniques.

IBM have a series of articles called AOPAtWork. Here is a sample article: "Say goodbye to scattered and tangled monitoring code, as Ron Bodkin shows you how to combine AspectJ and JMX for a flexible, modular approach to performance monitoring".

Thursday 8 January 2009

Java Debugging - The Java Debug Wire Protocol

The JDWP (JD WordPerfect or "Java Debug Wire Protocol") is the protocol a debugger uses to communicate with a JVM (which may be on a different machine). It's one layer within the JPDA. It's worth knowing the basic architecture of the JPDA.

the joys of java.util.concurrent

Since java ONE-FIVE we have had the java.util.concurrent package, which introduced the remarkable BlockingQueue interface (which extends java.util.Queue). The "B&Q" interface has a wide range of implementations:
  • ArrayBlockingQueue
  • DelayQueue
  • LinkedBlockingQueue (a bq based on linked nodes)
  • PriorityBlockingQueue
  • SynchronousQueue

An ArrayBlockingQueue is a BOUNDED blocking queue backed by an array. It orders elements using FIFO discipline. This is the classic "bounded buffer" in which a fixed-size array holds elements inserted by producers and extracted by consumers. The head of the queue is the element that has been on the queue the longest time.

These data structures are all part of the wonderful Java Collections Framework. Enjoy the benefits of enhanced-for loop, generics and auto-boxing.

Here's a precanned example of enhanced-for-loop in action, utilising the colon operator:

void cancelAll(Collection c) { for (TimerTask t : c) t.cancel();}

Wednesday 7 January 2009

Java Mobile Madness!

Nokia Series 40 Java SDK (50MB download), JavaME SDK,  EclipseME (install Eclipse then use the built-in updater), Antenna (Ant for wireless Java projects). It was created by the MTJ project team.

When you first run EclipseME you will need to create (at least one) Device Definition.

Blog Archive