Apa EJB tuh?

Definisi EJB;

“Enterprise JavaBeans (EJB) is an architecture for setting up program components, written in the Java programming language, that run in the server parts of a computer network that uses the client/server model

Sederhananya, EJB itu ada 3 macam; Session Bean , Message Driven Bean, dan Entity Bean.

Message Driven Bean, itu fungsinya kayak listener buat nerima pesan yang dikirim lewat jalur JMS secara asyncronous.
Entity Bean tuh componen yan berfungsi buat mem-persist data. Artinya, kalo aplikasi kamu itu database centric, maka data-data kamu itu di query pake entity bean. Entity bean ini umumnya mewakili tabel-table kita di database.Nah kalo session bean, umunya buat kamu naro2 bussines process. Session bean bisa aja kamu melakukan query make si Entity Bean, entah nge-insert , nge-query, ato nge-update database.

Setiap Bean (ketiga bean) bisa saling berinteraksi satu sama lain. Saling menggunakan satu sama lain….

contoh Message Driven Bean ;


package id.mujoko.mit.ejb.mdb;

import id.mujoko.mit.ejb.beans.MessageContent;
import id.mujoko.mit.ejb.stateless.MessageEngine;

import java.util.Date;

import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

@MessageDriven(activationConfig={
@ActivationConfigProperty(propertyName=”destinationType”,
propertyValue=”javax.jms.Queue”),
@ActivationConfigProperty(propertyName=”destination”,
propertyValue=”queue/mitMessageQueue”)})

public class MessageRouter implements MessageListener {
@EJB
private MessageEngine messageEngine;

@Resource(mappedName=”QueueConnectionFactory”)
private ConnectionFactory connectionFactory;

public void onMessage(Message message) {

try {

ObjectMessage objectMessage = (ObjectMessage) message;
MessageContent messageContent = (MessageContent) objectMessage.getObject();
messageContent.setCreatedOn(new Date());
messageEngine.createMessageContent(messageContent);

} catch (JMSException e) {
e.printStackTrace();
}

}

}

contoh Session Bean


package id.mujoko.mit.ejb.stateless;

import id.mujoko.mit.ejb.beans.Inventory;
import id.mujoko.mit.ejb.beans.MessageContent;

import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class MessageEngineBean implements MessageEngine {

@PersistenceContext(unitName=”mit”)
private EntityManager manager;

public Inventory createInventory(Inventory inventory){
if (inventory!=null){
manager.persist(inventory);
manager.flush();
}
return inventory;

}

public Inventory deleteInventory(Integer id) {
Inventory inventory=manager.find(Inventory.class, id);
if (id!=null){

manager.remove(inventory);
}

return inventory;

}

public Inventory updateInventory(Inventory inventory) {
if(inventory!=null){
manager.merge(inventory);
manager.flush();
}
return inventory;
}

public List<Inventory> findAllInventory(){
List<Inventory> list=manager.createNamedQuery(Inventory.FIND_ALL_INVENTORY).getResultList();
return list;
}

public List<Inventory> findInventoryByYear(Date date){

List<Inventory> list =manager.createNamedQuery(Inventory.FIND_ALL_INVENTORY_BY_YEAR).setParameter(“year”,               date).getResultList();
return list;
}

public void createMessageContent(MessageContent messageContent) {
if(messageContent!=null){
manager.persist(messageContent);
manager.flush();
}
}

public void deleteMessageContent(Integer id) {
if (id!=null){
MessageContent messageContent=manager.find(MessageContent.class, id);
manager.remove(messageContent);
}

}

public void updateMessageContent(MessageContent messageContent) {
if(messageContent!=null){
manager.merge(messageContent);
}
}
}


		

2 thoughts on “Apa EJB tuh?

  1. sitoluama August 20, 2007 at 1:57 pm Reply

    Bukannya Entity Bean udah diganti ama JPA di JEE?

  2. mujoko August 23, 2007 at 3:47 am Reply

    Yap anda 100% benul. JPA itu mulai ada di EJB 3, dan ini konteksnya EJB secara umum. JPA adalah implementasinya, while Entity Bean di EJB 3 yaa JPA itu. Sementara di pre EJB 3 Entity Bean itu BMP dan CMP. den Bagus🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: