EJB2.x is a long way toward EJB3.x. A lot has changed since 2.x. Major overhaul. However, most 2.x basics still hold when developing or supporting EJB3.x applications.
Software applications used:
1. JBoss AS 7.1 – www.jboss.org/jbossas/downloads/
2. WinXP running on a virtual machine (e.g., Virtual Box)
3. JDK 1.6.0_21
4. Eclipse Ganymede
Download, Install, and Run JBoss AS 7
Install JDK in WinXP running on some virtual machine so as not to affect your current host system. Next, download JBoss AS 7.1 (or the latest), extract it to some appropriate directory, and run it using <JBOSS_HOME>/bin/standalone.bat. You may need to add an admin user the first time you access the web console.
The Stateless Session Bean Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package com.karlsangabriel.ejb31; import javax.annotation.PostConstruct; import javax.ejb.Remote; import javax.ejb.Stateless; @Stateless @Remote(Interface1.class) public class SessionBean1 implements Interface1{ @PostConstruct public void postConstruct() { System.out.println("Karl San Gabriel"); } @Override public void doIt() { System.out.println("Karl San Gabriel - Do It!"); } } |
The Stateful Session Bean Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.karlsangabriel.ejb31; import javax.annotation.PostConstruct; import javax.ejb.Remote; import javax.ejb.Stateful; @Stateful @Remote(Interface1.class) public class StatefulSessionBean1 implements Interface1 { @PostConstruct public void postConstruct() { System.out.println("Karl San Gabriel - Stateful"); } @Override public void doIt() { System.out.println("Karl San Gabriel - Stateful - Do It!"); } } |
The Remote Interface – for both Stateless and Stateful Session Beans
1 2 3 4 | package com.karlsangabriel.ejb31; public interface Interface1 { void doIt(); } |
jboss-ejb-client.properties file
1 2 3 4 5 6 7 8 | 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 remote.connection.two.host=localhost remote.connection.two.port = 4447 remote.connection.two.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false |
The Clients codes
Stateless Session bean client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import java.security.Security; import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.jboss.sasl.JBossSaslProvider; import com.karlsangabriel.ejb31.Interface1; import com.karlsangabriel.ejb31.SessionBean1; public class Client1 { static { Security.addProvider(new JBossSaslProvider()); } //STATIC BLOCK - YOU MUST HAVE THIS public static void main(String[] args) throws NamingException { final Hashtable<String, String> jndiProperties = new Hashtable<String, String>(); jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); final Context context = new InitialContext(jndiProperties); final String appName = ""; final String moduleName = "ejbksg"; // THIS IS THE NAME OF THE JAR WITH YOUR EJBs. Write its name here, without the .jar. final String distinctName = ""; // AS7 allows deployments to have an //distinct name. If you don't use this feature, let this field empty. final String beanName = SessionBean1.class.getSimpleName(); //EJB CLASS WITH THE IMPLEMENTATION (simple name) final String viewClassName = Interface1.class.getName(); // FULLY QUALIFIED NAME OF THE REMOTE CLASS (interface). Interface1 bean = (Interface1) context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName); bean.doIt(); //NOTICE THAT DOING LOOKUP FOR STATELESS EJBs IS DIFFERENT FROM STATEFUL EJBs!!! } } |
The Stateful Session Bean Client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import java.security.Security; import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.jboss.sasl.JBossSaslProvider; import com.karlsangabriel.ejb31.Interface1; import com.karlsangabriel.ejb31.StatefulSessionBean1; public class Client2ForStatefulSessionBean { static { Security.addProvider(new JBossSaslProvider()); } //STATIC BLOCK - YOU MUST HAVE THIS public static void main(String[] args) throws NamingException { final Hashtable<String, String> jndiProperties = new Hashtable<String, String>(); jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); final Context context = new InitialContext(jndiProperties); final String appName = ""; final String moduleName = "ejbksg"; // THIS IS THE NAME OF THE JAR WITH YOUR EJBs. Write its //name here, without the .jar. final String distinctName = ""; // AS7 allows deployments to have an distinct //name. If you don't use this feature, let this field empty. final String beanName = StatefulSessionBean1.class.getSimpleName(); //EJB CLASS WITH THE IMPLEMENTATION (simple name) final String viewClassName = Interface1.class.getName(); // FULLY QUALIFIED NAME OF THE REMOTE CLASS (interface). Interface1 bean = (Interface1) context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName + "?stateful"); bean.doIt(); } } |