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)

tier

What You’ll Need

Part 1: The Business Tier

Deploy a Remote EJB

  1. Open JBoss Developer Studio
  2. Configure JBoss Maven Repositories (Section 1: Steps 4-7)
  3. Create a maven project named ejb-server-side. (Steps here)
  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
      Screen Shot 2017-03-16 at 10.43.59 AM
    • Right click your project -> Maven -> Update Project
      Screen Shot 2017-03-08 at 11.31.33 AM
  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;
          }
      }
  6. Build the project
    • Right click your project -> Run as -> Maven Build.
      Goals: clean install
  7. Deploy our EJB
    • Right click your project -> Run on serverScreen Shot 2017-03-09 at 2.31.20 PM
    • You should see deployment messages like..
      java:jboss/exported/ejb-server-side/MyEJB!org.codelikethewind.ejbserver.RemoteEJB

Add a New User

    1. Stop your server
      • Navigate to the Servers tab in JBoss Dev Studio
        Screen Shot 2017-03-10 at 12.20.32 PM
      • Click [name-of-your-server] -> Click the Red Stop Button
    2. Find your JBoss server directory (aka $JBOSS_HOME)
      • In the Servers tab, double click on your server
        Screen Shot 2017-03-09 at 3.58.38 PM
      • Click Runtime Environment. Your JBoss Server directory is under Home DirectoryScreen Shot 2017-03-09 at 4.05.56 PM
    3. In a terminal, navigate to $JBOSS_HOME (your server directory)
    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)
    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.

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 error
    Error 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

Source Code & Useful Links

  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.

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑