EJBs are associated with applications and it can do application's work. It's that simple. So these applications have to be deployed on a server. For the server you have various servers to choose.
- Apche
- IBM websphere application server
- JBoss (now called as WildFly)
- OW2
- Oracle GlassFish
- Resin and many more.
But for now we will focus on WildFly server more particularly 9.x.
PS- If you haven't install WildFly you can do this with ecilpse's servers tab. And configure it with correct HOME_PATH for WildFly.
Before we going down further,lets make clear of this thing.
Session bean:
- Session beans contain business logic.
- State-full session beans cant be reused again and again. One instance/session per client. Destroy and re create
- Stateless sessions beans can be reused and shared across multiple clients
- Singleton session beans are 1 per application
Entity bean is simply a representation of table raw. Which means it should be uniquely identifiable from other entity bean instances. For this we can use the existing table row primary key(PK). This can be composed of a single primary key or a composition of different primary keys.
Characteristics of entity bean PK
- It should be a type of integer wrapper class
- Should be unique
- Should have proper annotations
Entity bean:
- Can maintain its state with a unique primary key. So even in a system shutdown these can be persistent coz it has a unique primary key.
- At a time can be associated with multiple clients
- Represents a row in a table. Meaning entity bean and a data source has an association
Minimum code for a Session bean implementation
package Session;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.LocalBean;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
/**
* Session Bean implementation class MySessionEJB
*/
@Stateless
@LocalBean
public class MySessionEJB implements SessionBean {
/**
* Default constructor.
*/
public MySessionEJB() {
}
@Override
public void ejbActivate() throws EJBException, RemoteException {
}
@Override
public void ejbPassivate() throws EJBException, RemoteException {
}
@Override
public void ejbRemove() throws EJBException, RemoteException {
}
@Override
public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {
}
// additional user defined methods
public int multiply(int x, int y)
{
return x*y;
}
}
Minimum code for a Entity bean implementation
package Entity;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.LocalBean;
import javax.ejb.RemoveException;
import javax.ejb.Stateless;
/**
* Session Bean implementation class MyEntityBean
*/
public class MyEntityBean implements EntityBean {
@Id // Indicates this is an ID
@TableGenerator(name="") // Name of the table
@Column() // Unique column name
Integer primaryKey;
public Integer getPrimaryKey()
{
return primaryKey;
}
/**
* Default constructor.
*/
public MyEntityBean() {
}
@Override
public void ejbActivate() throws EJBException, RemoteException {
}
@Override
public void ejbLoad() throws EJBException, RemoteException {
}
@Override
public void ejbPassivate() throws EJBException, RemoteException {
}
@Override
public void ejbRemove() throws RemoveException, EJBException, RemoteException {
}
@Override
public void ejbStore() throws EJBException, RemoteException {
}
@Override
public void setEntityContext(EntityContext arg0) throws EJBException, RemoteException {
}
@Override
public void unsetEntityContext() throws EJBException, RemoteException {
}
}
Minimum code for a Message bean implementation
package Message;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;
/**
* Message-Driven Bean implementation class for: MyMessageBean
*/
@MessageDriven(
activationConfig = { @ActivationConfigProperty(
propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class MyMessageBean implements MessageListener {
/**
* Default constructor.
*/
public MyMessageBean() {
}
/**
* @see MessageListener#onMessage(Message)
*/
public void onMessage(Message message) {
}
}