method
setMaxNumberOf-ConcurrentRequests exposes
a writable property to the
application server’s administration or configuration facilities. Pooling of already created
ManagedConnection instances
requires implementation of
the matchManagedConnections
method.
Usually, a connection is differentiated by the username,
password, or other security credentials. Here, all thread pools are equal,
so we just match the connection using
the ConnectionRequestInfo instance.
Both the ManagedConnection
implementation (Connection) and
the ManagedConnectionFactory
(ConnectionFactory) realize connection
management at the logical level and
are highly reusable. In addition, the
Connection class fires events to notify
the application server runtime about the
current state (see Listing 9a, Listing 9b,
and Listing 9c).
The Connection instance manages a
List of ConnectionEventListeners and fires
events on close, commit, begin, or rollback events. Our connector implementation is not transactional (but could be
implemented as such), so most of the
events will never fire.
As a logical part, the Connection
also manages its physical counterpart. JCAExecutor is the actual “meat”
of the connector. Runnable instances
are passed to the execute method to
be asynchronously executed by the
WorkManager (see Listing 10).
The JCAExecutor implements
the
java.util.concurrent
.Executor interface, which will
be directly used by the application and is created by the
JCAExecutorFactory, as shown in
Listing 11.
JCAExecutor with its
JCAExecutorFactory are the
actual domain-specific
implementation. Most of
the ManagedConnection and
ManagedConnectionFactory
implementations are business logic–
agnostic and could be reused for other
JCA implementations. All classes have
to be packaged into a JAR file that has
the .rar extension and deployed to the
application server.
Java EE applications do not care about
the JCA implementation and are interested only in the asynchronous execution of tasks in a managed environment.
The application-facing API consists of a
single interface that returns the Executor
to the application, as shown in Listing 12.
Now Java EE 6 applications can access
a managed Executor implementation as
easily as in a Java SE environment (see
Listing 13).
Java EE 7 will
come with
even more
interesting
concurrency
features.
public class Connection
implements ManagedConnection {
private ConnectionFactory mcf;
private Print Writer out;
private JCAExecutor fileConnection;
private ConnectionRequestInfo connectionRequestInfo;
private List<ConnectionEventListener> listeners;
Connection(Print Writer out,ConnectionFactory mcf, ConnectionRequestInfo
connectionRequestInfo) {
}
JAVA IN ACTION
public Object getConnection(Subject subject, ConnectionRequestInfo
connectionRequestInfo)
throws ResourceException {
fileConnection = new JCAExecutor(out,this,mcf, connectionRequestInfo);
return fileConnection;
}
public void destroy() {
this.fileConnection.destroy();
ABOUT US
public void cleanup() {
}
public void associateConnection(Object connection) {
this.fileConnection = (JCAExecutor) connection;
}
Java EE 7—Even Better
With five classes and a simple Maven
Project Object Model—and without any
XML deployment descriptors—you get
a flexible thread pool implementation
that is fully managed by the application
server and could be extended to support transactions, security, and progress monitoring.
blog