Latest GlassFish 4 promoted build (74 at this time) has the first cut of Concurrency Utilities for Java EE (JSR 236) implementation integrated. An earlier blog provided an overview of the specification, lets see what can be actually done with this build.
This build introduce new commands to create
ManagedExecutorService
,ManagedScheduledExecutorService
, ManagedThreadFactory
,
and ContextService
.A simple command execution as:
asadmin create-managed-executor-service concurrent/myExecutor
Managed executor service concurrent/myExecutor created successfully.
Command create-managed-executor-service executed successfully.
creates the configuration for a default
ManagedExecutorService
.
Adding --help
shows more details about the command:create-managed-executor-service
[--enabled=true]
[--contextinfo=contextinfo]
[--threadpriority=5]
[--longrunningtasks=false]
[--hungafterseconds=hungafterseconds]
[--corepoolsize=0]
[--maximumpoolsize=2147483647]
[--keepaliveseconds=60]
[--threadlifetimeseconds=0]
[--taskqueuecapacity=2147483647]
[--description=description]
[--property=property]
[--target=target]
jndi_name
The created executor can then be injected into a Java EE component (say Servlet) as:
@Resource(name = "concurrent/myExecutor")or looked up using JNDI as:
ManagedExecutorService executor;
InitialContext ctx = new InitialContext();
ManagedExecutorService executor = (ManagedExecutorService) ctx.lookup("concurrent/myExecutor");
A task can be defined as:
public class MyRunnableTask implements Runnable {Task can be submitted as:
private int id;
public MyRunnableTask(int id) {
this.id = id;
}
@Override
public void run() {
try {
System.out.format("%d (runnable): starting", id);
System.out.format("%d (runnable): sleeping 2 seconds", id);
Thread.sleep(2000);
System.out.format("%d (runnable): complete", id);
} catch (InterruptedException ex) {
Logger.getLogger(TestResourceServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Future<?> f = executor.submit(new MyRunnableTask(1));
OR
executor.execute(new MyRunnableTask(2));
A task may also be defined as:
public class MyCallableTask implements Callable<Product> {where
private int id;
public MyCallableTask(int id) {
this.id = id;
}
@Override
public Product call() {
try {
System.out.format("%d (callable): starting", id);
System.out.format("%d (callable): sleeping 2 seconds", id);
Thread.sleep(2000);
System.out.format("%d (callable): complete", id);
} catch (InterruptedException ex) {
Logger.getLogger(TestResourceServlet.class.getName()).log(Level.SEVERE, null, ex);
}
return new Product(id);
}
}
Product
is a domain-specific class. In this
case, the task is submited for execution as:Future<Product> f2 = executor.submit(new MyCallableTask(3));A
ManagedScheduledExecutorService
can be created as:asadmin create-managed-scheduled-executor-service concurrent/myScheduledExecutor
Managed scheduled executor service concurrent/myScheduledExecutor created successfully.
Command create-managed-scheduled-executor-service executed successfully.
A
ManagedThreadFactory
can be created as:asadmin create-managed-thread-factory concurrent/myThreadFactory
Managed thread factory concurrent/myThreadFactory created successfully.
Command create-managed-thread-factory executed successfully.
A
ContextService
can be created as:asadmin create-context-service concurrent/myContextService
Context service concurrent/myContextService created successfully.
Command create-context-service executed successfully.
Note, this is the first integration and some of the options may not work. But you can definitely start playing around with basic stuff now.
The complete source code used in this Tip Of TheDay (TOTD) is available here and will run on GlassFish 4 b74.