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.
ProjStructure

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

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

import javax.ejb.Remote;

@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")
@Remote(HelloWorldSessionBeanRemote.class)
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
serverconsole

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 “jboss-ejb-client.properties” in the classpath of the application. We can place it in ejbModule folder of our application. The jboss-ejb-client.properties contains the following properties:
jboss-ejb-client.properties

    remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false

    remote.connections=default

    remote.connection.default.host=localhost
    remote.connection.default.port = 4447
    remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false

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) {
			e.printStackTrace();
		}
		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 🙂

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s