EJB 3 getting started

November 23, 2012 | Miscellaneous

EJB3 Getting Started with JBoss 5:
 
 
Environment:
JBoss – Atleast 5.1.0.GA. If it is less than 5.1, use JDK 1.5 only
Java version  – Atleast 1.5
EJB – Atleast 3
 
 
Use the following reference JAR if at all you are using any IDE: ejb-api-3.0.jar
 
 
Steps to follow:
Create remote interface class "Converter":
package org.bss.ejb;
 
import java.math.BigDecimal;
 
import javax.ejb.Remote;
 
@Remote
public interface Converter {
 
 public BigDecimal dollarToYen(BigDecimal dollars);
 
 public BigDecimal yenToEuro(BigDecimal yen);

}

2. Create the session bean "ConverterBean" by implementing "Converter":
package org.bss.ejb;
 
import java.math.BigDecimal;
 
import javax.ejb.Stateless;
 
@Stateless
public class ConverterBean implements Converter {
 
 private static final BigDecimal yenRate = new BigDecimal("83.0602");
    private static final BigDecimal euroRate = new BigDecimal("0.0093016");
 
    public BigDecimal dollarToYen(BigDecimal dollars) {
        BigDecimal result = dollars.multiply(yenRate);
        return result.setScale(2, BigDecimal.ROUND_UP);
    }
 
    public BigDecimal yenToEuro(BigDecimal yen) {
        BigDecimal result = yen.multiply(euroRate);
        return result.setScale(2, BigDecimal.ROUND_UP);
    }
 
}

3. If you are using any IDE skip this step and go to step 4. Or else run the following command in order to compile the files:
 
E:\workspace\Converter\bin>set JBOSS_HOME=E:\jboss-5.1.0.GA
E:\workspace\Converter\bin>javac -d . -classpath %JBOSS_HOME%\client\jbossall-client.jar;. ..\src\org\bss\ejb\*.java
 
NOTE: Paths may change depending on system
 
4. Build the project. After compiling/building the files, the project tree should look like:

E:\workspace\Converter>tree /a /f
E:.
|
+—bin
|   \—org
|       \—bss
|           \—ejb
|                   Converter.class
|                   ConverterBean.class
|
\—src
    \—org
        \—bss
            \—ejb
                    Converter.java
                    ConverterBean.java
 
6. Make the JAR of these two files by going into class path.
Run the following command in order to build the JAR from class path. See the output laso:
 
E:\workspace\Converter\bin>jar -cvf converter.jar *
added manifest
adding: org/(in = 0) (out= 0)(stored 0%)
adding: org/bss/(in = 0) (out= 0)(stored 0%)
adding: org/bss/ejb/(in = 0) (out= 0)(stored 0%)
adding: org/bss/ejb/Converter.class(in = 263) (out= 204)(deflated 22%)
adding: org/bss/ejb/ConverterBean.class(in = 1002) (out= 566)(deflated 43%)
 
E:\workspace\Converter\bin>
 
7. Copy the JAR file "converter.jar " to "E:\jboss-5.1.0.GA\server\default\deploy" folder and start the Jboss by running:
 
E:\jboss-5.1.0.GA\bin>run.bat
 
8. Client Code:
To access the session bean here we're using JNDI lookup process.
Write ConverterClient .java:
 
package org.bss.request;
 
import java.math.BigDecimal;
 
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
 
import org.bss.ejb.Converter;
 
public class ConverterClient {
 
 public static void main(String[] args) throws NamingException {
 
  // Jboss JNDI name
  final String jndiName = "ConverterBean/remote";
  // Get naming context
  Context context = new InitialContext();
  // Set the JNDI environment properties
  context.addToEnvironment("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
  context.addToEnvironment("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
  context.addToEnvironment("java.naming.provider.url", "localhost:1099");
 
  System.out.println("\nLooking up …..");
 
  Converter converter = (Converter) context.lookup(jndiName);
 
  String amount = "12";  
  BigDecimal yen = new BigDecimal(amount);
  System.out.println("\nCalling remotedly …..");
  System.out.print("\n" + amount + " Yen = ");
  System.out.print(converter.yenToEuro(yen));
  System.out.print(" Euro\n");
 }
 
}
 
8. Build the project or compile the file using:
E:\workspace\Converter\bin>javac -d . -classpath %JBOSS_HOME%\client\jbossall-client.jar;. ..\src\org\bss\ejb\*.java
 
9. After doing above all the project structure should look like:
E:\workspace\Converter>tree /a /f
E:.
|   
+—bin
|   |   converter.jar
|   |
|   \—org
|       \—bss
|           +—ejb
|           |       Converter.class
|           |       ConverterBean.class
|           |
|           \—request
|                   ConverterClient.class
|
\—src
    \—org
        \—bss
            +—ejb
            |       Converter.java
            |       ConverterBean.java
            |
            \—request
                    ConverterClient.java
9. Now its time for seeing the output. Run the Client class file using:
 
E:\workspace\Converter\bin>java -cp %JBOSS_HOME%\client\*;. org.bss.request.ConverterClient
 
The output should look like:
 
Looking up …..
 
Calling remotedly …..
 
12 Yen = 0.12 Euro
 

Tags:

Leave a Reply

If you have a Tip, Comment, Article or anything you want to share, please send it to: i@poisure.com