Archive

Archive for October, 2009

EJB 3.1 – Asynchronous Session Beans

October 23rd, 2009 Comments off

The Session Bean Component Contract of the Enterprise JavaBeans 3.1 Specification (JSR 318 Proposed Final Draft) introduces the possibility of exposing methods of session beans with asynchronous semantics.

What does that mean?
For asynchronous invocations, control returns to the client before the container dispatches the invocation to a bean instance.
When a client invokes an asynchronous method, the container returns control to the client immediately and continues processing the invocation on a separate thread.

How to expose session bean’s methods asynchronously
Annotation: @Asynchronous

This annotation can be applied to

  • a business method of a bean class
  • a method of a Local/Remote business interface
  • the class level of a bean class (or super class)
  • the class level of a particular Local/Remote business interface (or super interface)

Application to a class level causes that all methods declared on that class or interface are asynchronous.

Alternatively, asynchronous methods can be designated via the deployment descriptor.

What does an asynchronous method look like?
The return type of an asynchronous method is either void or java.util.concurrent.Future<V>, where V is the result value type.

Return Type Behaviour
void must not declare any application exceptions
Future<V>
  • is permitted to declare application exceptions
  • used to make the return value of an asynchronous method invocation available to the client
  • javax.ejb.AsyncResult<V> is a concrete Future implementation that is provided by the container. Its constructor takes the result value as a parameter.

The Client View
When invoking an asynchronous method the client should expect to receive a system exception (javax.ejb.EJBException).
The system exception will be thrown by the container if it has problems to allocate the internal resources required to support the asynchronous method. The client may retry the asynchronous method invocation at a later time.

Any application exception resulting from the method execution will be available via the Future<V> object.

Interpretation of the Future<V> object

Method Behaviour
Future.cancel(...)
  • The container will attempt to cancel the associated asynchronous invocation
  • The cancellation of the asynchronous method can only be attempted by the container if that invocation has not already been dispatched (Attention! There is no guarantee that the invocation can be cancelled!)
  • Must return true, if the invocation is successfully cancelled
  • Must return false, if invocation cannot be cancelled
Future.get
  • Used by the client to retrieve the result value or resulting exception from the associated asynchronous invocation
  • Important: If a call to get successfully returns a resul value or throws an ExecutionException, all subsequent calls on the same Future object must result in the same behaviour.

Timeouts
An EJB Container Provider is permitted to define a timeout value that governs the maximum amount of time the container maintains result values for completed asynchronous invocations. However, the configuration of this timeout is vendor-specific.

Transactions
From the session bean’s view there is never a transaction context flowing in from the client to asynchronously invoked methods.
Example: The semantics of the REQUIRED transaction attribute on an asynchronous method are exactly the same as REQUIRES_NEW.

Useful little helpers making java developers live easier!

October 10th, 2009 Comments off

You may have some recurring development tasks while your daily work with java in different projects. For example you again and again write some small code that capitalizes a given String. Or you write an application with a command line interface and write code that defines and parses your command line options. Or you need some little helpers for your database handling. Or …
These are only some examples and if you don’t have already written and built your own little libraries for those tasks, you may want to have a look at Apache Commons, formerly known as Jakarta Commons. There are libraries for everyday use in different packages.
I will provide you a short overview over Apache Commons here and write some more articles about some of their libraries in the next weeks.
So, Apache Commons assign their libraries to three repositories depending on their development stage and usage (list taken from their site):

  • The Commons Proper – A repository of reusable Java components.
  • The Commons Sandbox – A workspace for Java component development.
  • The Commons Dormant – A repository of Sandbox components that are currently inactive.

I will focus on Commons Proper in this and my following articles since the contained components are ready to use and stable.

Currently, Commons Proper contains 39 components. You can categorize them into web, xml, utilities, converters and Java API extension.
The following table is taken and translated from german Wikipedia:

Category Components
Web FileUpload, Net, EL, Email, Jexl
XML Betwixt, Digester, Jelly, JXPath
Utilities BeanUtils, Pool, Validator, Daemon, Discovery, Exec, Launcher, JCI, Jelly, Modeler, SCXML, Chain
Converters Codec, Compress
Java API Extensions Lang, Collections, IO, Logging, Configuration, DBCP, DbUtils, Math, Primitives, Proxy, Validator, VFS, Attributes, CLI, Discovery, Transaction

 

Categories: Development, Java Tags: , , , ,

Preventing Eclipse from opening MS Office documents in OLE editor component

October 10th, 2009 1 comment

I’m working with Excel and Word documents in my latest java project. On Windows plattforms Eclipse opens those documents with its own OLE editor by default. But I feel much more comfortable in using the Office apps to edit them. This can be done by right clicking on the file icon and choose “open with -> system editor” for example. But you will have to do this for each new or moved file in your workspace the first time you open it.
Thus Eclipse opens these files with Excel or Word by default you need to navigate to “Window -> Preferences -> General -> Editors -> File Associations”. You will notice, that there are no file associations for Excel nor Word. You have to explicitly add the file types (*.xls and *.doc for example) and add the desired applications as associated (external) editors.

Categories: General Tags: , , , ,