Setting a timeout for synchronous EJB invocations in EAP6
Environment
- JBoss Enterprise Application Platform (EAP) 6.x
Issue
- We lookup remote EJB's in JBoss using JNDI and it looks like these remote/local EJB calls does not have a timeout.
- Is it possible to interrupt a EJB remote call after an adjustable time?
- I have tried to add properties like 'jnp.timeout', but I still don't get a timeout
- I set remote.connection.???.connect.timeout with no effect
Resolution
Starting with EAP 6 there is a possibility to set a global limit for an EJB execution via the timeout invocation.timeout. If invocation.timeout is not set, EJB client waits indefinitely by default.
Use jboss-ejb-client.properties
The timeout can be set global for all connections within the properties file.
invocation.timeout=1000
Use the JBoss specific client API
If the client API is used the property can be set within the PropertiesBasedEJBClientConfiguration.
Properties p = new Properties();
p.put("invocation.timeout", "1000");
p.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "false");
p.put("remote.connections", "one");
p.put("remote.connection.one.port", "4447");
p.put("remote.connection.one.host", "localhost");
EJBClientConfiguration cc = new PropertiesBasedEJBClientConfiguration(p);
ContextSelector<EJBClientContext> selector = new ConfigBasedEJBClientContextSelector(cc);
EJBClientContext.setSelector(selector);
Properties props = new Properties();
props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
InitialContext context = new InitialContext(props);
final String rcal = "ejb:app/ejb//EJBBean!" + EJBBean.class.getName();
final MainApp remote = (EJBBean) context.lookup(rcal);
Handling of a timeout
After the given timeout (milliseconds) the invocation will be interrupted and a java.util.concurrent.TimeoutException
will be thrown. The server side process will not be interrupted and continue until its normal return.
Notes
The invocation.timeout is the total time to aquire access to an EJB instance and for the EJB to process the request.
Root Cause
- From the EJB specification the EJB calls are synchronous and does not have such timeout.
To control such time a standalone client might create a thread to execute the EJB and return the original after a timeout. - Since EJB3.1 it is possible to use asynchronous methods which provide Future objects, these is possible for standalone clients and also inside the server (where no thread control is allowed). Via the Future object might be also possible to cancel the action.
See KCS solution 142093 - Asynchronous EJB calls. - jnp.timeout is outdated (use for invocation <=EAP5)
- ....connect.timeout is the timeout during establishing the connection
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.