Monday, February 15, 2010

EJB3 Session Beans

**************************************************************************************
Server Side Code
**************************************************************************************
package test;

import javax.ejb.Remote;

@Remote
public interface TestEJBBean {
public void executeBusinessLogic();
}

**************************************************************************************
package test;

import javax.ejb.Remote;
import javax.ejb.Stateless;

import org.jboss.annotation.ejb.LocalBinding;
import org.jboss.annotation.ejb.RemoteBinding;
@Stateless (name="TestEJBBeanBean")
@Remote(TestEJBBean.class)
@RemoteBinding(jndiBinding = "/ejb3/TestEJBBeanBean")

public class TestEJBBeanBean implements TestEJBBean{
public void executeBusinessLogic() {
System.out.println("Executing business logic.");
}
}
**************************************************************************************
Client Side Code (Create this as another project)
**************************************************************************************
package test;
public interface TestEJBBean {
public void executeBusinessLogic();
}
**************************************************************************************
package client;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import test.TestEJBBean;



public class EJBTestClient {

static Properties props = new Properties();
/**
* @param args
*/
public static void main(String[] args) {
try {
props.load(new FileInputStream("jndi.properties"));
InitialContext ic = new InitialContext(props);
TestEJBBean testEJBBean = (TestEJBBean)ic.lookup("/ejb3/TestEJBBeanBean");
testEJBBean.executeBusinessLogic();
} catch(FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch(IOException ioe) {
ioe.printStackTrace();
} catch(NamingException ne) {
ne.printStackTrace();
}
}
}
**************************************************************************************
EJBJNDI.properties file (for JBoss)
**************************************************************************************
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
TestEJBBeanBean=/ejb3/TestEJBBeanBean
**************************************************************************************

No comments:

Post a Comment