Here i am attaching a simple EJB3.0 application on JBoss Server.
Just follow the following steps
1. File->New->Other->EJB->EJB Project or File->New->EJB->EJB Project then give a name to your project and choose the default settings.
(Choose target runtime click on New button and configure JBoss Server)
2. It will create src folder and ejbModule by default choose it later you can change.
3. Write a sample interface named as Calculator.java
4. Write SesssionBean named as CalculatorBean
5. Write your client class named as CalculatorClient.java
here is my full running code
//***************************************
Calculator.java
/**
*
*/
package com.hcl.ejb.iface;
/**
* @author Administrator
*
*/
public interface Calculator {
public int add(int a,int b);
public float mul(float a,float b);
}
//***************************************
CalculatorLocal.java
package com.hcl.ejb.sessionface;
import com.hcl.ejb.iface.*;
import javax.ejb.Local;
@Local
public interface CalculatorLocal extends Calculator{
}
//***************************************
CalculatorRemote.java
package com.hcl.ejb.sessionface;
import com.hcl.ejb.iface.*;
import javax.ejb.Remote;
@Remote
public interface CalculatorRemote extends Calculator{
}
//***************************************
CalculatorBean.java
package com.hcl.ejb.session.bean;
import com.hcl.ejb.sessionface.CalculatorLocal;
import com.hcl.ejb.sessionface.CalculatorRemote;
import javax.ejb.Stateless;
public @Stateless class CalculatorBean implements CalculatorLocal, CalculatorRemote{
public int add(int a,int b){
return a+b;
}
public float mul(float a,float b){
return a+b;
}
}
//***************************************
CalculatorClient.java
/**
*
*/
package com.hcl.ejb.client;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.hcl.ejb.iface.*;
import com.hcl.ejb.sessionface.*;
import com.hcl.ejb.session.bean.*;
/**
* @author Administrator
*
*/
public class CalculatorClient {
/**
*
*/
public CalculatorClient() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
Properties prop=new Properties();
prop.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
prop.setProperty("java.naming.provider.url","
jnp://localhost:1099";);
prop.setProperty("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
InitialContext ctx=new InitialContext(prop);
System.out.println("111");
Calculator cal=(Calculator)ctx.lookup("CalculatorBean/remote");
System.out.println("222");
System.out.println("add: = "+cal.add(10,20));
System.out.println("mul: = "+cal.mul(10.5f,20.5f));
}catch(NamingException ne){
ne.printStackTrace();
}
}
}
deploy the project on JBoss and run it will run.