I saw InjectionPoint mentioned breifly by Adam Bien in his JavaOne 2011 Presentation JavaEE 6 Cool Parts (around about 0:50).

There wasn’t much information about it though I found the Javadoc at http://docs.oracle.com/javaee/6/api/javax/enterprise/inject/spi/InjectionPoint.html and it had all I needed to know.

For example, the following producer method creates injectable Loggers. The log category of a Logger depends upon the class of the object into which it is injected.

Logger createLogger(InjectionPoint injectionPoint) {
return Logger.getLogger( injectionPoint.getMember().getDeclaringClass().getName() );

And it works!

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html">
    <title>Facelet Title
    Hello from Facelets
      <h:commandButton value="Injector seat!" action="#{messenger.hello}"/>
// Messenger.java
package boundary;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Inject;
import javax.inject.Named;

 * @author bsmith
public class Messenger {
    @Inject String data;
    @Inject Logger log;

    public void hello() {
        System.out.println("Hello!  I want: "+data);
        log.log(Level.SEVERE, "("+log.getName()+") I am logging some data: "+data);
// Configuration.java
package control;

import java.util.logging.Logger;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;

 * @author bsmith
public class Configuration {
    public String getData(InjectionPoint ip) {
        System.out.println("ip.toString: "+ip.toString());
        System.out.println("ip.getAnnotated: "+ip.getAnnotated());
        System.out.println("ip.getBean: "+ip.getBean());
        return "data";

    public Logger getLogger(InjectionPoint ip) {
        return Logger.getLogger( ip.getMember().getDeclaringClass().getName() );

INFO: com.tintuna_Injection_war_1.0-SNAPSHOT was successfully deployed in 398 milliseconds.
INFO: ip.toString: [field] @Inject boundary.Messenger.data
INFO: ip.getAnnotated: [field] @Inject boundary.Messenger.data
INFO: ip.getBean: Managed Bean [class boundary.Messenger] with qualifiers [@Any @Default @Named]
INFO: Hello!  I want: data
SEVERE: (boundary.Messenger) I am logging some data: data
Written on October 13th, 2012 , EJB, Java Tags: , ,

EJB Asynchronous moves the task of handling concurrent threads to the container to manage. Java’s concurrency cannot be used in the normal way; by creating Thread runners, with wait() and notify() and so on.  Adam Bien talked about managing threads in JavaEE 6 using an Executor on his blog.

Asynch is easier though it does seem to me to be less powerful.  However the power of an Executor would seem to trump the low-level benefits of using Threads directly. See this Lars Vogel tutorial on Java Concurrency for what they allow you to do.

I digress; back to Async where it seems to be easier then Threads and Executor’s.

I will show an example simple usage, making use of isDone() in the client and how to use cancel().

Example simple usage

From Enterprise Java Beans 3.1 book page 60ish:

// for some Session Bean with an interface
public Future hashAsync(final String input) throws IllegalArgumentException, EncryptionException
    // Get the real hash
    final String hash = this.hash(input);
    // Wrap and return
    return new AsyncResult(hash);

// And used by the client like this
public void someMethod() {
    // Declare the input
    final String input = "Async Hashing Input";
    // Hash
    final Future hashFuture = myEjbProxyReference.hashAsync(input);
    // Now we're free to do whatever work we want here while the EJB invocation runs concurrently
    // ...[work]
    // At this point we need the hash, so get the result,
    // and block for up to 10 seconds to get it
    final String hash = hashFuture.get(10,TimeUnit.SECONDS);
    log.info("Hash of \"" + input + "\": " + hash);

When is the Asynch method done?

You’d use the following code in some way such as set an Observer on it for population in a GUI. That’s an exercise for the reader.

  final String input = "Async Hashing Input";
  // Hash
  final Future hashFuture = myEjbProxyReference.hashAsync(input);
  if (hashFuture.isDone()) {
    final String hash = hashFuture.get();

Cancelling and making the most of Asynch code

I found this quite confusing, as did others.

I did some searching and the gestault switch flicked when I read IBM discussion of Developing client code that calls EJB asynchronous methods in Websphere.

What follows is my interpretation.

In the client we have:

  final String input = "Async Hashing Input";
  // Hash
  final Future hashFuture = myEjbProxyReference.hashAsync(input);
  if (hashFuture.cancel()) {
      // it was cancelled and got a true return value - what does this mean?;
  } else {
      // it was cancelled and got a false return value - what does this mean?

And the Asynchronous method in the Session Bean is:

public Future run() throws Exception {
    while (myContext.wasCancelCalled() == false) {
        // do work can do in stages
    return new AsyncResult(result);

The contents of the while loop is some task we can break down into smaller chunks and each chunk is done in a trip around the while loop. The guard on the loop is what checks if the operation has been cancelled.

But what does the return value from the if (hashFuture.cancel()) mean?

  • true – means that the cancel happened before the Asynch method was dispatched (ie. the run()method never started)
  • false – means that the run() method started. In which case the while loop in the Asynch method was necessary to cancel the operation.
  • mayInterruptIfRunning – if true means that once the run() method has started the container won’t stop it. Thus the while loop will have no affect.

I’m doing study for the BUSINESS COMPONENT DEVELOPMENT WITH EJB TECHNOLOGY, JAVA EE 6 certification and although I didn’t think I needed to know such details for the exam, it bugged me and I just had to find out. And the truth of the matter is that I don’t know what will be on the exam.

I hope this helps anyone else doing this study or just wanting to know.

I now have a question, be it another itch I must scratch – how to use the features of Asynch I’ve just discussed with Executor’s…..

Written on September 11th, 2012 , EJB, Java Tags: , , ,

Brooke Smith is proudly powered by WordPress and the Theme Adventure by Eric Schwarz
Entries (RSS) and Comments (RSS).

Brooke Smith

Portfolio and site of a software engineer