kids encyclopedia robot

Jakarta Enterprise Beans facts for kids

Kids Encyclopedia Facts

Jakarta Enterprise Beans (EJB; formerly Enterprise JavaBeans) are like special building blocks for creating big computer programs used by companies. Imagine you're building a huge LEGO castle. EJB helps you make strong, ready-to-use sections (like a tower or a wall) that handle important jobs. These jobs often include keeping your data safe, managing how different parts of the program work together, and making sure everything runs smoothly.

EJB is a part of a bigger set of tools called Java EE, which helps developers build powerful web applications.

What are Jakarta Enterprise Beans?

Jakarta Enterprise Beans are special software parts that handle the "brain" or "business rules" of a big application. Think of it like the chef in a restaurant. The chef (EJB) knows how to prepare the food (business logic), like taking an order, cooking it, and sending it out. The restaurant kitchen (EJB container) provides everything the chef needs, like security, managing orders, and making sure payments are handled correctly.

This way, programmers don't have to worry about common tasks like:

  • Keeping data safe (security).
  • Making sure all steps in a process happen correctly, like a money transfer (transaction processing).
  • Saving information permanently (persistence).

EJB handles these common concerns in a standard way. This lets programmers focus on the unique parts of their company's software.

How were EJBs developed?

The idea for EJB started in 1997 with IBM. Later, Sun Microsystems took over and improved it. Over the years, EJB has changed a lot to become simpler and easier to use.

For example, early versions of EJB were quite complicated. They were designed for programs spread across many computers. But most programs didn't need that complexity. So, later versions, especially EJB 3.0, became much lighter and easier to code. They started using simple Java objects and special notes called annotations to make things less wordy.

What do EJBs do?

An EJB system helps with many important tasks for big applications:

  • Handling Transactions: Making sure a series of steps either all succeed or all fail, like when you transfer money from one bank account to another.
  • Saving Data: Working with tools that help store information in databases.
  • Controlling Access: Managing how different parts of the program can use shared resources.
  • Responding to Events: Listening for messages or events and reacting to them, like sending an email when an order is placed.
  • Doing Tasks Later: Allowing parts of the program to run in the background without making the user wait.
  • Scheduling Jobs: Setting up tasks to run at specific times, like a daily report.
  • Finding Services: Helping different parts of the program find each other.
  • Talking to Other Programs: Allowing EJBs to communicate with other software, even on different computers.
  • Security: Making sure only authorized users or programs can access certain features.
  • Setting Up Software: Helping to install and run the software parts correctly on a server.

Simple EJB Example

Let's look at a very basic example of what an EJB looks like in code. Imagine you want to add a new customer to a system:

@Stateless
public class CustomerService {

  private EntityManager entityManager;

  public void addCustomer(Customer customer) {
    entityManager.persist(customer);
  }
}

This code defines a service that adds a `Customer` object to a database. The `@Stateless` part tells the EJB system that this service doesn't remember anything about previous calls. The EJB system automatically handles saving the customer and making sure the process is safe (transactional).

Now, imagine a part of the website that lets a user click a button to add a customer. That part might use the `CustomerService` like this:

@Named
@RequestScoped
public class CustomerBacking {
   @EJB
   private CustomerService customerService;

   public String addCustomer(Customer customer) {
      customerService.addCustomer(customer);
      // Show a message to the user
      return "customer_overview";
   }
}

Here, the `@EJB` part tells the system to automatically connect our `CustomerBacking` (which handles what the user sees) to the `CustomerService` (which handles the actual saving). This keeps the "user interface" part separate from the "business logic" part.

Kinds of Enterprise Beans

There are two main types of Enterprise Beans:

  • Session Beans: These are like temporary workers that handle specific tasks for a client. They can be "Stateful," "Stateless," or "Singleton."
  • Message Driven Beans (MDBs): These beans are triggered by messages, not direct calls. They are like mailboxes that process incoming letters.

Session Beans

Session beans are the most common type. They help manage the "business logic" of your application.

Stateful Session Beans

Imagine a shopping cart on an online store. A Stateful Session Bean is like a personal shopping assistant who remembers everything you put in your cart. It keeps track of your specific actions during your shopping session. Because it remembers your "state," only one customer can use a specific assistant at a time.

  • Example: Managing the steps of a checkout process. The bean remembers which items you're buying and where you are in the payment steps.

Stateless Session Beans

A Stateless Session Bean is like a general-purpose calculator. It performs a task, gives you the answer, and then forgets everything. It doesn't remember anything about previous calculations or who asked for them. This means many people can use the same calculator, one after another, without it getting confused.

  • Example: Sending a single email to customer support. It's a one-time action, and the bean doesn't need to remember anything afterward.
  • Example: Getting a list of products. The bean just fetches the data and doesn't need to remember anything about the user or their past requests.

Singleton Session Beans

A Singleton Session Bean is like a single, shared notice board for everyone using the application. There's only one of it, and it holds information that everyone needs to see.

  • Example: Loading a daily price list that is the same for all users. The bean loads it once, and then everyone can quickly access it without the system having to look it up again and again.

Message Driven Beans

Message Driven Beans (MDBs) are special beans that don't wait for a direct call. Instead, they listen for messages. Imagine a post office box. When a new letter (message) arrives, the MDB automatically processes it. This is great for tasks that don't need an immediate response or for sending information to many places at once.

  • Example: Sending a software update to many computers. You send one message, and the MDBs on each computer listen for it and apply the update.
  • Example: Processing a job submitted to a group of computers. You send a message to a "job queue," and an MDB picks it up and makes sure the job gets done.

How EJBs Run

EJBs run inside something called an EJB container, which is usually part of a larger application server. Think of the container as a special house for your EJB building blocks. The container provides all the services and rules for the EJBs to work correctly.

When your program wants to use an EJB, it doesn't directly create it. Instead, it asks the EJB container for a reference. The container gives back a "proxy" – like a stand-in or a representative. When your program talks to this proxy, the proxy first asks the container to do some important background work (like starting a transaction or checking security), and *then* it calls the actual EJB. This makes sure all the EJB's special powers work automatically.

Transactions in EJBs

Transactions are super important for keeping data correct. An EJB container can manage transactions for you automatically. This is called Container-Managed Transactions (CMT).

Imagine you're buying something online. A transaction makes sure that either your money is taken *and* you get the item, or if something goes wrong, neither happens. You don't end up with no item and no money!

Here are some ways transactions can be handled:

How Transactions are Managed
Type What it means
MANDATORY If your program hasn't started a transaction, it will stop. Otherwise, it uses your program's transaction.
REQUIRED If your program has a transaction, it uses it. If not, a new one is started. (This is the usual way).
REQUIRES_NEW A brand new transaction is always started, even if your program already has one (the old one is paused).
SUPPORTS If your program has a transaction, it uses it. If not, no transaction is used.
NOT_SUPPORTED No transaction is used. If your program has one, it's paused.
NEVER If your program has started a transaction, it will stop. No transaction is used.

Sometimes, programmers want to control transactions themselves. This is called Bean Managed Transactions (BMT).

Events and Messages

EJBs can send and receive messages. This is often done using something called JMS. It's like sending a text message instead of making a direct phone call.

  • EJBs can send messages to other parts of the system or to clients.
  • Message Driven Beans (MDBs) are designed to listen for and process incoming messages automatically.

Finding EJBs

Programs need a way to find and use EJBs. One common way is through something called JNDI. Think of JNDI as a phone book for software components. You look up the EJB by its name, and JNDI gives you its "phone number" (a reference to the EJB).

Talking to Remote EJBs

Sometimes, an EJB needs to be used by a program running on a different computer. EJBs can be set up to allow this "remote communication." This is like having a special phone line for your EJB.

EJBs can also talk to programs not written in Java, often using "web services." These are like universal translators that let different types of software communicate over the internet.

EJB Versions Over Time

EJB has changed a lot since it first appeared in 1998. Each new version tried to make it simpler and add new features.

  • EJB 1.0 (1998): The very first version. It defined the basic roles and how EJBs would work.
  • EJB 1.1 (1999): Improved how applications were put together and deployed.
  • EJB 2.0 (2001): Focused on building "distributed" applications (programs spread across many computers). It also added support for web services.
  • EJB 2.1 (2003): Added a timer service to run tasks at specific times and allowed Message Driven Beans to handle more types of messages.
  • EJB 3.0 (2006): This was a huge change! It made writing EJBs much, much easier by using simple "annotations" instead of complex setup files. It was almost like a completely new tool.
  • EJB 3.1 (2009): Simplified things even more, like allowing EJBs to be packaged more easily and adding "Singleton" beans. It also introduced "EJB Lite," a simpler version for smaller needs.
  • EJB 3.2 (2013): A smaller update that mostly clarified rules and lifted some old restrictions.
  • EJB 3.2.6 (2019): Officially renamed "Enterprise JavaBeans" to "Jakarta Enterprise Beans" to avoid trademark issues.
  • EJB 4.0 (2020): Mainly updated the names of the software packages to "jakarta.ejb" from "javax.ejb." It also removed some old, unused features.
kids search engine
Jakarta Enterprise Beans Facts for Kids. Kiddle Encyclopedia.