How to Use Remote EJBs in JBoss Pt. 1

Remote EJBs help us access components when they run on a different machine

How to Use Remote EJBs in JBoss Pt. 1

When we create enterprise software, we constantly add new features, fix bugs, and modify our requirements.

And to make these updates easy, we break our applications into small components.

Each component has one function, and each server runs one component.

This separation makes it easy to change or reuse any part of our system.

But we still need our components to communicate across servers.

And for that, we need to use Remote EJBs

So..

Why Do We Use Remote EJBs?

When we deploy our web (Servlets) and business components (EJBs) to separate servers, we need a way for them to communicate.

Remote EJBs make it easy to access business logic components when they run on a different server.

How Do We Use Remote EJBs?

Our Final Architecture

  • 1 server running a web app that can access our Remote EJB (Web Tier)
  • 1 server running a Remote EJB (Business Tier)

What You'll Need

Part 1: The Business Tier

Deploy a Remote EJB

Step 1: Open JBoss Developer Studio

Step 2: Configure JBoss Maven Repositories (Section 1: Steps 4-7)

Step 3: Create a maven project named ejb-server-side. (Steps here)

Step 4: Update Maven

  • In your pom.xml file, click the pom.xml tab and change the packaging type
<packaging>ejb</packaging>
  • Change EJB plugin to version 3.0. Add this before the tag
<build>     
    <finalName>${project.artifactId}</finalName>
    <plugins>        
        <plugin>            
            <artifactId>maven-ejb-plugin</artifactId>            
            <configuration>                
                <ejbVersion>3.1</ejbVersion>                 
                <generateClient>true</generateClient>             
            </configuration>       
        </plugin>   
    </plugins> 
</build>
  • Add EJB dependencies: jboss-ejb-api_3.1_spec
  • Right click your project -> Maven -> Update Project

Step 5: Create a Remote EJB

  • Create an interface called RemoteEJB
  • Add @Remote annotation and a helloWorld method
@Remote 
public interface RemoteEJB {    
	String helloWorld(String name); 
}
  • Create a stateless bean called MyEJB that implements RemoteEJB
@Stateless 
public class MyEJB implements RemoteEJB {
    public String helloWorld(String name){
        return "Hello " + name;    
    } 
}

Step 6: Build the project

  • Right click your project -> Run as -> Maven Build.
Goals: clean install

Step 7: Deploy our EJB

  • Right click your project -> Run on server
  • You should see deployment messages like..
java:jboss/exported/ejb-server-side/MyEJB!org.codelikethewind.ejbserver.RemoteEJB

Add a New User

Step 1: Stop your server

  • Navigate to the Servers tab in JBoss Dev Studio
  • Click [name-of-your-server] -> Click the Red Stop Button

Step 2: Find your JBoss server directory (aka $JBOSS_HOME)

  • In the Servers tab, double click on your server
  • Click Runtime Environment. Your JBoss Server directory is under Home Directory

Step 3: In a terminal, navigate to $JBOSS_HOME (your server directory)

Step 4: Create a new user

  • In $JBOSS_HOME/bin, run the add-user.sh script (for Windows, add-user.bat)
$ ./add-user.sh
  • Type b to add a Application User
  • Name the user ejbuser and give it a password
  • Press Enter to skip group selection
  • Type yes to add the user to ApplicationRealm
  • Type yes to use this user to connect to other AS processes (servers)

Step 5: Save the secret-value to a text file. We'll use this later

 secret value="[your_secret_value]"

Great! Let's review what we did.

Recap

Maven

  • We added the EJB API and changed the packaging to EJB
  • EJB deployments (before version 3.0) must have an ejb-jar.xml file. We updated our version to 3.0 so we can skip that file. Otherwise you'll see this errorError assembling EJB: META-INF/ejb-jar.xml is required for ejbVersion 2.x
  • We generated a client JAR that we'll use in Part 2

Deploy EJB

  • We created a Remote EJB
  • We declared a remote interface using the @Remote annotation. All methods in this class are exposed to a client
  • We implemented the interface with a @Stateless EJB
  • We built and deployed the EJB to our server

Add a New User

  • We created a new user and added it to the ApplicationRealm
  • The ApplicationRealm stores users that are allowed to connect to deployed apps or EJBs
  • Next time, we'll use these credentials to connect from our Web server

What's a best practice for Remote EJBs?

It's a good practice to use Local EJBs whenever possible.

Local EJBs have better performance than Remote EJBs because they pass references to objects instead of entire values.

Only use Remote EJBs if you need to access an EJB outside your server.

So..

  1. Remote EJBs help us access business logic components when they run on a different server
  2. We use a @Remote annotation and an interface to mark EJBs as remote
  3. Use Local EJBs when possible for better performance
  1. Project Source Code
  2. 3 Tier Architecture
  3. When to use remote EJBs

In part 2, we'll see how to access this remote EJB from a different server.

Happy Coding,

-T.O.