The JAXB XJC schema binding compiler transforms, or binds, a source XML schema to a set of JAXB content classes in the Java programming language.
But sometimes we need more specific stuff in the generated java class.
For exmaple, the JAXB2 Basics Plugins says “Schema compiler (XJC) produces schema-derived classes which can be used to turn XML into object structures and back. However, generated classes lack by default lack convenience and utility methods like equals(…), hashCode(…), toString() and so on.”

We can use Metro’s JAXB 2.0 commons project to save time.

Reference:
1. JAXB 2.0 commons project
2. Using JAX-WS With Maven
3. JAX-WS Metro and JBossWS
4. Java EE5 JAXB tutorial

There are two principal architectures for Web service interfaces: synchronous Web services and asynchronous Web services. These two architectures are distinguished by their request-response handling. With synchronous services, clients invoke a request on a service and then suspend their processing while they wait for a response. With asynchronous services, clients initiate a request to a service and then resume their processing without waiting for a response. The service handles the client request and returns a response at some later point, at which time the client retrieves the response and proceeds with its processing.

Let’s see some great articles about Asynchronous Web Service.
1. Web Service Interaction Architectures
2. Asynchronous operations and Web services, Part 1: A primer on asynchronous transactions
3. Asynchronous operations and Web services, Part 2
4. Can I call you back? – Asynchronous Web Services
5. Asynchronous Transactions and Web Services
6. Gerard Davison’s Asynchronous web service post

Since we talk about asynchronous web service, I will drill down the underlying basic,  SOAP over JMS. The W3C has just released W3C Candidate Recommendation. See InfoQ: SOAP Over Java Messaging Service.

1. Soap over JMS – what does it mean and why should I care?
2. Building a JMS Web service using SOAP over JMS and WebSphere Studio
3. Using Request-Response SOAP over JMS Web Services
4. SOAP over JMS with Axis2
5. SOAP Over JMS Interoperability

Recently I worked on a integration project threw read-timeout exception and blocking web service call. The client calls a EJB web service method to persistent an Entity Bean. Unfortunately this web service is implemented wihtin a long-running process and it causes JBoss transaction timeout.
The exception like this

This bug looks like easy, and I can just raise the JBoss’s jboss-service.xml Transaction Timeout value to higher.
But if you look down the details, you may find more interesting thing. Read more on Transaction Timeouts and EJB3/JPA and Using JBossJTA in Application Servers.

Although this might fix my problem of tansaction timeout, I still haven’t solved the performance issue from client calling the long-running process. Then I change the code inside of EJB web service. I crate a Message Driven Bean (MDB) and move the persistent Entity Bean there. Now the exposed web service method sends JMS message to MDB asynchronously and return to client fast. No more blocking web service call.

Reference:

Building a JMS Web service using SOAP over JMS and WebSphere Studio

pic0914

sometimes I need to debug program with multiple threads and complex codes. Or I work on analyzing and interpreting the JVM performance monitoring and diagnostics statistics. I need a sharp tool to do it. So I find most powerful kits to help.

1. An Introduction to Java Stack Traces
2. JDB Example: Generating a Thread Dump
3. How to debug a multithreaded java program
4. Schedule your own Java thread dumps
5. JDK Debugging Tools
6. JDK Tools and Utilities
7. Exercise basic command line debugging tools
8. Debug your Java code with ease using JPDA
9. IBM and SAP Open Source their JVM Diagnostics Tools
10. IBM Thread and Monitor Dump Analyzer for Java
11. Sun VisualVM is a visual tool integrating several commandline JDK tools and lightweight profiling capabilities.
12. Eclipse Test & Performance Tools Platform Project
13. Eclipse Tips: Debugging your multi thread application

The constraint programming technology has matured to the point where it is possible to isolate some essential features and offer them as libraries or embedded cleanly in general purpose host programming languages.
For example ILOG Solver is for planning or scheduling systems. Now I am trying to use Constraint Programming Satisfy (CPS) and domain specific language (DSL) at scheduling system.

Library
1. JaCoP – Java Constraint Programming solver
2. Cream: Class Library for Constraint Programming in Java
3. jOpt is an open source implementation of the Optimization Programming Language
4. JACK: A Java Constraint Kit
5. ILOG CP Optimizer
6. Koalog Constraint Solver
7. Open Source Constraint Programming Solvers Written in Java (collections)

Reference
1. Magazine: Journal of Scheduling
2. Constraint Programming in Java with JSolver (ILOG)
3. Course: CS 886: Topics in Artificial Intelligence: Constraint Programming
4. Course: Programming with Constraints: an Introduction
5. Course: CONSTRAINT PROGRAMMING
6. Course: Constraint Programming: Services
7. Book: Programming with constraints
8. Book: Handbook of constraint programming
9. Solving Sudokus in Java

1. Callable and Future
The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception.

1.1 JDK API
1.2 Using Callable to Return Results From Runnables
1.3 Java 5 Concurrency: Callable and Future
1.4 Concurrency: I Can See Into Your Future

2. Concurrent ReadWriteLock
Think of a scenerio that there are many reader threads that reads a shared data frequently and only one writer thread that updates shared data. It’s not necessary to exclusively lock access to shared data while reading because multiple read operations can be done in parallel unless there is a write operation.

2.1 JDK API
2.2 ReadWriteLock example in Java
2.3 Java 5 Concurrency Reader Writer Locks
2.4 ReentrantLock: test for deadlocks
2.5 Read / Write Locks in Java
2.6 Declarative Synchronization with Java 5’s ReadWriteLock
2.7 Read-write locks

3. Locks

3.1 JDk API
3.2 Locks in Java
3.3 Explicit locks in Java 5
3.4 ReentrantLock Example
3.5 More flexible, scalable locking in JDK 5.0
3.6 Java 5 Concurrency: Locks
3.7 Using ReentrantLock for thread synchronization

4. Lock Conditions

4.1 JDK API
4.2 Java 5 Concurrency: Conditions
4.3 Java 1.5: collection and thread

5. Implementing Read-Write Locks by yourself

5.1 Understanding Java Multithreading and Read-Write Locks, Part 1
5.2 Implementing Read-Write Locks in Java
5.3 Concurrency: Understanding the Value of Read-Write Locks
5.4 Java Thread Programming: Implement Read & Write Locks

6. Thread Interrupt
An interrupt is an indication to a thread that it should stop what it is doing and do something else. It’s up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate.

6.1 JDK API
6.2 Shutting down threads cleanly
6.3 Thread interrupt Method to Terminate Thread
6.4 Java: Handling Interrupts
6.5 Dealing with InterruptedException

Writing correct concurrent code is difficult, but not impossible. Using good object-oriented design techniques can make it easier.
Here I listed most useful tips together and hoepfully you don’t fear threads any more.

Basic Lesson: Concurrency

1. ThreadPoolExecutor (Thread pool)
- Maintains caller-specified work queue
- Manages thread pool size and use depending upon queue type and contents, and keepAlive parameter
- Easy instantiation through Executors factory class
- Work queue is implementation of BlockingQueue interface: LinkedBlockingQueue, PriorityBlockingQueue, ArrayBlockingQueue, SynchronousQueue are provided by lib.

1.1 JDK API
1.2 Thread pools and work queues
1.3 A Thread Pool Pattern in Java
1.4 Java 5 Executors: ThreadPool

1.5 How to make your own Thread Pool
- create your ThreadPoolExecutor that includes specific BlockingQueue and Comparator.
- override the beforeExecute and afterExecute
- create your ThreadFactory that implement uncaughtException to prevent thread dead problem

class MyThreadPoolExecutor extends ThreadPoolExecutor{
	protected MyThreadPoolExecutor(){
		super(corePoolSize, maximumPoolSize, keepAliveTime, unit, new PriorityBlockingQueue(maxCapacity,new MyComparator()),new MyThreadFactory());
	}
	@Override
	protected void beforeExecute(Thread t,Runnable r){
		/*do your stuff*/
		super.beforeExecute(t, r);
	}
	@Override
	protected void afterExecute(Runnable r, Throwable t) {
		/*do your stuff*/
		super.afterExecute(r, t);
	}

}

class MyThreadFactory implements ThreadFactory {
	private final MyThreadGroup tg = new MyThreadGroup();
	    public Thread newThread(Runnable r) {
			     return new Thread(tg,r);
			}
	}
	private class MyThreadGroup extends ThreadGroup{
		private MyThreadGroup(){
			super("MyThreadGroup");
		}
		public void uncaughtException(Thread t, Throwable e){
			log.debug(t);
			/*do something*/
		}
}

2. Thread and UncaughtException

2.1 UncaughtExceptionHandler
2.2 Hey, where’d my thread go?

3. Collections and Thread

3.1 JDK ConcurrentHashMap
3.2 JDK ConcurrentSkipListSet
3.3 Concurrent collections classes
3.4 What is new in Java 6.0 Collections API?
3.5 Correct use of ConcurrentHashMap
3.6 Concurrent structures and collections in Java 5

Reference: Understand Skip List Map/Set
3.7 Java Ordered Collections: Trees and Skip Lists
3.8 Skip lists

4. Thread Scheduling
The JVM schedules using a preemptive , priority based scheduling algorithm.
All Java threads have a priority and the thread with he highest priority is scheduled to run by the JVM.

4.1 JAVA Thread Scheduling
4.2 Course online about OS and Thread
4.3 Achieve strong performance with threads, Part 3
4.4 Thread Scheduling
4.5 Thread Priorities and Thread Scheduling

5. Collections synchronizedList and synchronizedMap

5.1 JDK API
5.2 When A Synchronized Class Isn’t Threadsafe
5.3 Best way to control concurrent access to Java collections

6. ThreadLocal
A thread-local variable effectively provides a separate copy of its value for each thread that uses it.
Each thread can see only the value associated with that thread, and is unaware that other threads may be using or
modifying their own copies.

6.1 Using Thread-Local Variables In Java
6.2 Threading lightly, Part 3: Sometimes it’s best not to share
6.3 Using ThreadLocal and Servlet Filters to cleanly access JPA an EntityManager
6.4 Thread-local variables in Java
6.5 Introduction to reducing thread contention in Java

The simplest Java programs run from the console and executed in a single thread, when an uncaught RuntimeException is thrown, it will print a stack trace to the console and terminate the thread and itself.
But if you work on Java Swing, Java web applicaiton or multiple threads application, then the thread always remains alive, in spite of any RuntimeExceptions thrown by your program.

1. Where Uncaught Exceptions Go
2. How uncaught exceptions are handled
3. Provide an uncaught exception handler
4. Uncaught exception handlers
5. Java theory and practice: Hey, where’d my thread go?
6. Unchecked exceptions in Java