google Analytics

Monday, August 2, 2010

EJB3 Session Bean annotation Example

--jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1300
/**
* Session Bean implementation class CallEjb3Bean
*/
@Stateless(name="CallEjb3Bean", mappedName="CallEjb3")
public class CallEjb3Bean implements CallEjb3BeanLocal {

/**
* Default constructor.
*/
public CallEjb3Bean() {
System.out.println("CallEjb3Bean");
}

public void callme(){
System.out.println("callme-->> Called");
}

}

@Local
public interface CallEjb3BeanLocal {
public void callme();
}


@Stateless(mappedName="testejb")
public class TestEjb3Bean implements TestEjb3BeanRemote, TestEjb3BeanLocal {

/**
* Default constructor.
*/
public TestEjb3Bean() {
System.out.println("TestEjb3Bean->>>>>>> Called");
}
@EJB
private static CallEjb3BeanLocal local;

public void sayHello(String name){
System.out.println("Name-->>"+name);
try {
System.out.println("Start--------------->g>>>");
InitialContext ctx = new InitialContext();
local.callme();
System.out.println("Ends--------------->>>>");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception----------------->>");
}
}

}

@Local
public interface TestEjb3BeanLocal {
public void sayHello(String name);
}

@Remote
public interface TestEjb3BeanRemote {
public void sayHello(String name);
}


public class Client
{
@EJB
public static TestEjb3BeanRemote remote;
public static void main(String[] args) throws Exception
{
InitialContext ctx = new InitialContext();
TestEjb3BeanRemote testEjb3BeanRemote = (TestEjb3BeanRemote) ctx.lookup("testejb");
testEjb3BeanRemote.sayHello("ANISH");

}
}

1 comment: