|
|
Executing tasks with java.util.concurrent API
Eugene Kuleshov's Blog |
May 2, 2006 11:09 PM
|
Comments (4)
Version 0.2 of the xcommonj-work project include implementation of ExecutorService and allows to use java.util.concurrent API introduced in Java 5 for asynchronous and parallel task execution. For compatibility with JRE 1.4 (e.g. in order to run on Weblogic 8.1) you can use
this implementation is using backport-util-concurrent package.
The good thing about java.util.concurrent is that it part of JRE and and can be used in standalone applications. This API is using new Callable interface, which is essentially serve the same function as Work interface used by WorkManager. Result is stored in instance of the Future, which is practically there same as WorkItem in WorkManager API. Here is an example from the JavaDoc:
ExecutorService executor = ...
ArchiveSearcher searcher = ...
Future future = executor.submit(new Callable() {
public String call() {
return searcher.search(target);
}
});
displayOtherThings(); // do other things while searching
try {
displayText(future.get()); // use future
} catch(ExecutionException ex) {
cleanup();
}
Unfortunately, in order to use these interfaces in J2EE environment one would have to deal with the thread management, which is prohibited by specification.
Luckily, we already had all required infrastructure as part of xcommonj-work project, which is using container-managed MDB's as a thread pool. It was really easy to implement ExecutorService on top of this infrastructure.
Assuming xcommonj-work EJB is deployed and JMS is configured as described on the project page, we can use Spring framework to retrieve required dependencies from JNDI:
<bean id="executorService"
class="org.javatx.cj.work.XWorkExecutorService">
<property name="qcf" ref="workQcf"/>
<property name="queue" ref="workQueue"/>
<property name="workHome" ref="workHome"/>
</bean>
<bean id="workQcf" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="javatx.workermdb.qcf"/>
</bean>
<bean id="workQueue" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="javatx.workermdb.queue"/>
</bean>
<bean id="workHome" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<bean id="org.javatx.cj.work.XWorkHome.JNDI_NAME"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/>
</property>
</bean>
Source code for XWorkExecutorService bean is avaialable in Subversion repository of the xcommonj-work project and can be built with Maven 2 tool.
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
Does this code work in a cluster environement?
Posted by: peti505 on June 5, 2006 at 11:23 AM
-
It does work on the cluster, but it won't execute tasks on remote cluster notes since xcommonj-work's WorkManager is not clustered.
However the good news is that in a new release there will be an adapter that allow to use ExecutorService to execute work in any WorkManager, so it will be clustered as long as work manager instance is clustered.
Posted by: euxx on June 5, 2006 at 1:08 PM
-
I have run the junit test of the backport on a Sun JVM 1.4.2_08 and this works fine. However, if I execute the same junit test using Jrockit 1.4.2_08, it hangs for ever, even when I lower the "tck.shortDelay" param.
Have you guys experienced the same limitation ?
Posted by: rpiccand on August 29, 2006 at 2:57 AM
-
I haven't tried this with JRockit, but if it does not work for you, open a bug report on JRockit.
Posted by: euxx on August 29, 2006 at 6:42 AM
|
|