Using a EJB
Using the business logic contained within the deployed EJB from within a web application
involves making the following steps:
- Get an InitialContext
- Lookup the Home interface
- Get a instance from the Home interface
- Call a method on this instance
It is useful to view this chain of events from within the scope of java code annotated with comments.
// Create a property object with parameters
Properties props = new Properties();
// The class to use to create the InitialContext
props.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
// The port number, remember the port number is specific
props.put(Context.PROVIDER_URL,"jnp://localhost:1099");
props.put(Context.URL_PKG_PREFIXES,
"org.jboss.naming:org.jnp.interface");
// Get the Initial Context (1)
InitialContext jndiContext = new InitialContext(props);
out.println("Got context");
// Lookup and get a home instance (2)
Object ref = jndiContext.lookup("testbean");
InterestHome home = (InterestHome)
PortableRemoteObject.narrow(ref, TestBeanHome.class);
// Get a instance from the Home interface (3)
Interest testbean = home.create();
// Call a method on the business object (4)
out.println(testbean.callBusinessLogic());
The code above omits key elements such as importing the classes required by the
client, error handling and importing the required libraries used the code.
However, you should find that the business object returns the correct response.
An important aspect is the port used to connect to the JNDI server, this will be
assigned to you so you will need to make appropriate adjustments to your code.
Stopping Jboss
With the ability of jboss to hot deploy stopping Jboss will be a rarity except in
occasions where the underlying JBoss configuration is modified. The shutdown.sh script located
within the same folder as run.sh (/usr/jboss/bin) will shutdown jboss, later versions
of JBoss require an additional parameter. The syntax of the shutdown.sh command is:
bin/shutdown -s localhost:[jndi port]
For exhaustive documentation in using and configuring Jboss please consult the Jboss documentation
found here: Jboss