Creating EJB with JBoss 7.1

Hi friends, many of my friends were asking me about EJB and deployment in JBoss. Here in this post i decided to write about the development and deployment of EJB in JBoss Application Server 7.1.

Please find the project structure as shown below.

Now lets start code our EJB Session Beans ans its Interfaces.
Remote Interface:

package com.spark.ejb.session.beans.def;

import javax.ejb.Remote;

public interface HelloWorldSessionBeanRemote {

	public String sayHello();

Session Bean Implementation :

package com.spark.ejb.session.beans.def;

import javax.ejb.Remote;
import javax.ejb.Stateless;

 * Session Bean implementation class HelloWorldSessionBean
@Stateless(mappedName = "helloWorldBean")
public class HelloWorldSessionBean implements HelloWorldSessionBeanRemote, HelloWorldSessionBeanLocal {

     * Default constructor. 
    public HelloWorldSessionBean() {
        // TODO Auto-generated constructor stub
    public String sayHello() {
        return "Hello World !!!";


Please observe @Remote(RemoteInterface.class) in the above implementation, this will help EJB client to search the Remote Bean implementation using JBoss Remoting. Once deployed in the server if the EJB deployed without any error then we should see the below JNDI global entries in the server console

Now its time to write our client code.
An EJB client context is a context which contains contextual information for carrying out remote invocations on EJBs. This is a JBoss AS specific API. The EJB client context can be associated with multiple EJB receivers. Each EJB receiver is capable of handling invocations on different EJBs.
For example, an EJB receiver “ClientA” might be able to handle invocation on a bean identified by app-A/module-A/distinctinctName-A/BeanA!com.ibc.RemoteBeanA, app-B/module-B/distinctName-B/BeanB!RemoteBeanB, etc. Each such EJB receiver knows about what set of EJBs it can handle and each of the EJB receiver knows which server target to use for handling the invocations on the bean. The server IP address and its remoting port should be specified in the properties file placed in the client classpath. This properties file (EJB client context) will then be used internally by the JNDI implementation to handle invocations on the bean proxy.

Create a file “” in the classpath of the application. We can place it in ejbModule folder of our application. The contains the following properties:

    remote.connection.default.port = 4447

Now let us write an utility class which will return us InitialContext object using which we will search the EJB proxy object.

package com.spark.ejb.session.client;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

 * @author Sony
public class ClientUtility {

	private static Context initialContext;
	private static final String PKG_INTERFACE = "org.jboss.ejb.client.naming";
	public static Context getInitialContext() throws NamingException{
		if(initialContext == null){
			Properties properties = new Properties();
			properties.put(Context.URL_PKG_PREFIXES, PKG_INTERFACE);
			initialContext = new InitialContext(properties);
		return initialContext;

Main class

package com.spark.ejb.session.client;

import javax.naming.Context;
import javax.naming.NamingException;

import com.spark.ejb.session.beans.def.HelloWorldSessionBean;
import com.spark.ejb.session.beans.def.HelloWorldSessionBeanRemote;

 * @author Sony
public class EJBClient {

	 * @param args
	public static void main(String[] args) {
		HelloWorldSessionBeanRemote bean = doLookUp();
		System.out.println(bean.sayHello()); // Call business logic

	private static HelloWorldSessionBeanRemote doLookUp() {
		Context context = null;
		HelloWorldSessionBeanRemote bean = null;
		try {
			// 1. Obtaining Context
			context = ClientUtility.getInitialContext();
			// 2. Generate JNDI Lookup name
			String lookupName = getLookUpName();
			// 3. Lookup and cast
			bean = (HelloWorldSessionBeanRemote) context.lookup(lookupName);

		} catch (NamingException e) {
		return bean;

	private static String getLookUpName() {

		// AppName is the .ear file name without extension, if no .ear is deployed then this is empty
		String appName = "";
		// module name is the EJB jar file deployed without .jar extension
		String moduleName = "HelloWorldEJB";
		String distinctName = "";

		// EJB bean impl class name
		String beanName = HelloWorldSessionBean.class.getSimpleName();

		// Fully qualified remoteInterface Name
		String interfaceName = HelloWorldSessionBeanRemote.class.getName();

		// Create a look up string name
		String name = "ejb:" + appName + "/" + moduleName + "/" + distinctName
				+ "/" + beanName + "!" + interfaceName;

		return name;


Jar files that are needed to run our client.

Jar File Name Location
jboss-transaction-api_1.1_spec-1.0.0.Final.jar AS7_HOME/modules/javax/transaction/api/main/
jboss-ejb-api_3.1_spec-1.0.1.Final.jar AS7_HOME/modules/javax/ejb/api/main/
jboss-ejb-client-1.0.0.Beta10.jar AS7_HOME/modules/org/jboss/ejb-client/main/
jboss-marshalling-1.3.0.GA.jar AS7_HOME/modules/org/jboss/marshalling/main/
xnio-api-3.0.0.CR5.jar AS7_HOME/modules/org/jboss/xnio/main/
jboss-remoting-3.2.0.CR6.jar AS7_HOME/modules/org/jboss/remoting3/main/
jboss-logging-3.1.0.Beta3.jar AS7_HOME/modules/org/jboss/logging/main/
xnio-nio-3.0.0.CR5.jar AS7_HOME/modules/org/jboss/xnio/nio/main/
jboss-sasl-1.0.0.Beta9.jar AS7_HOME/modules/org/jboss/sasl/main/
jboss-marshalling-river-1.3.0.GA.jar AS7_HOME/modules/org/jboss/marshalling/river/main/

Once done please run the client. remember prior to running your EJB client your EJB should be up and running in server.

Happy EJB3 with JBoss 🙂


3 thoughts on “Creating EJB with JBoss 7.1

  1. Thanks for any other informative site. The place else may just
    I get that kind of info written in such a perfect
    means? I’ve a project that I’m just now operating on, and
    I’ve been on the look out for such information.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s