JBoss AS 7 EJB remote clients without configuration file

In order to lookup a remote EJB 3 with JBoss AS 7 you have to fill in some basic information, such as the host name and remoting port. This is normally achieved using a file named jboss-ejb-client.properties which needs to be placed in the client classpath. However the remote client API does provide some utility classes to perform a lookup without any configuration file.

In order to load the remote configuration, we will store the properties in a standard Properties object and use an org.jboss.ejb.client.EJBClientConfiguration object to provide the configurations that will be used for creating EJB receivers and managing the EJB client context.

Once done with it, we will need to create a ConfigBasedEJBClientContextSelector using the EJBClientConfiguration that we have just created.
This constructor creates a org.jboss.ejb.client.EJBClientContext and uses the passed ejbClientConfiguration to create and associate EJB receivers to that context.

Finally, a proxy for the ejb is created by passing an instance of the StatelessEJBLocator (or of the StatefulEJBLocator if you are running against a Stateful EJB) class to the EJBClient class:

So supposing you are going to recall the following EJB


@Stateless

@Remote(CalculatorRemote.class)

public class CalculatorRemoteBean implements CalculatorRemote {

 . . . .

}

Here's the client for it:


Properties pr = new Properties();

pr.put("endpoint.name", "client-endpoint");
pr.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "false");
pr.put("remote.connections", "default");
pr.put("remote.connection.default.port", "4447");
pr.put("remote.connection.default.host", "localhost");
pr.put("remote.connection.default.username", "user");
pr.put("remote.connection.default.password", "Password12345");

EJBClientConfiguration cc = new PropertiesBasedEJBClientConfiguration(pr);
 
final ContextSelector<EJBClientContext> ejbClientContextSelector = new ConfigBasedEJBClientContextSelector(cc);

// Now let's setup the EJBClientContext to use this selector

final ContextSelector<EJBClientContext> previousSelector = EJBClientContext.setSelector(ejbClientContextSelector);              
 
StatelessEJBLocator<CalculatorRemote> locator = new StatelessEJBLocator(CalculatorRemote.class, "", "calculator-demo", "CalculatorRemoteBean", "");               

CalculatorRemote ejb =  org.jboss.ejb.client.EJBClient.createProxy(locator);

Related articles available on mastertheboss.com

JBoss AS 7 introduction

This is an introduction tutorial to the newest JBoss AS 7 which a

Develop Java EE applications with JBoss AS 7

In this tutorial we will learn how to create and deploy a Java EE

Adding users with JBoss AS 7

If you are planning to add new users to your management interface

Using JBoss AS 7 management API programmatically

In this tutorial we will show how to use detyped management API t

JNDI view in JBoss AS 7

JNDI can be checked on the naming subsystem of the application se

How to install a module on WildFly / JBoss AS 7

Installing a module on WildFly / JBoss AS 7 requires creating a p

Follow us on Twitter