RSS

Stateless Bean based on Remote Interface

04 Mar

HelloEJBRemote.java

package com.xavier.ejb3.sessionbean;

public interface HelloEJBRemote {

        public String hello(String name);

}

HelloEJBRemoteBean.java

package com.xavier.ejb3.sessionbean;

import javax.ejb.Remote;

import javax.ejb.Stateless;

 

@Stateless

@Remote(HelloEJBRemote.class)

public class HelloEJBRemoteBean implements HelloEJBRemote {

    public String hello(String name) {

return new String(“Hello, “ + name);

    }

}

 

Note:

1) If you want to rename the Session Bean’s name – @Stateless(name=”HelloEJB”), if no name parameter, then the Session Bean would be class name.

2) If EJB Bean implements multiple business interfaces: @Remote({HelloEJBRemote.class, UserEJBRemote.class});

 

HelloEJBRemote.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8” pageEncoding=“UTF-8”%>

<%@ page

import=“javax.naming.*, com.xavier.ejb3.sessionbean.*, com.xavier.ejb3.entitybean.*, java.util.*”%>

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” http://www.w3.org/TR/html4/loose.dtd>

<html>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>

<title>Hello EJB Remote Bean</title>

</head>

<body>

<%

try{

final Hashtable jndiProperties = new Hashtable();

jndiProperties.put(Context.PROVIDER_URL, “localhost:1099”);

jndiProperties.put(Context.URL_PKG_PREFIXES, “org.jboss.ejb.client.naming”);

Context ctx = new InitialContext(jndiProperties);

HelloEJBRemote hr = (HelloEJBRemote)ctx.lookup(ejb:ejb3/ejb3//HelloEJBRemoteBean!com.xavier.ejb3.sessionbean.HelloEJBRemote);

out.println(“<br>Invoke EJB via Remote Interface<br>” + hr.hello(“Xavier”));           

}catch(Exception e){

out.println(“<br>Invoke Remote Interface failed.”);

}

%>

</body>

</html>

Note:

1) JBoss AS 7.1 EJB Session Beans’ naming convention rules:

For stateless beans:

ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>

For stateful beans:

ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>?stateful

2) JNDI Properties.

Here we are creating a JNDI InitialContext object by passing it some JNDI properties. The Context.URL_PKG_PREFIXES is set to org.jboss.ejb.client.naming. This is necessary because we should let the JNDI API know what handles the ejb: namespace that we use in our JNDI names for lookup. The “org.jboss.ejb.client.naming” has a URLContextFactory implementation which will be used by the JNDI APIs to parse and return an object for ejb: namespace lookups. You can either pass these properties to the constructor of the InitialContext class or have a jndi.properites file in the classpath of the client application, which (atleast) contains the following property:

java.naming.factory.url.pkgs=org.jboss.ejb.client.naming

Advertisements
 
Leave a comment

Posted by on 03/04/2012 in EJB

 

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 )

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

 
%d bloggers like this: