Rudziankoŭ
Rudziankoŭ

Reputation: 11251

Understanding of remote EJB

I am playing around with JBoss and EJB, I use jboss-eap-quickstarts

I have server-side class:

package org.jboss.as.quickstarts.ejb.remote.stateless;

import javax.ejb.Remote;
import javax.ejb.Stateless;

@Stateless
@Remote(RemoteCalculator.class)
public class CalculatorBean implements RemoteCalculator {

    @Override
    public int add(int a, int b) {
        return a + b;
    }

    @Override
    public int subtract(int a, int b) {
        return a - b;
    }
}

And I have client-side method:

private static RemoteCalculator lookupRemoteStatelessCalculator() throws NamingException {
    final Hashtable jndiProperties = new Hashtable();
    jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
    final Context context = new InitialContext(jndiProperties);
    return (RemoteCalculator) context.lookup(
     "ejb:/jboss-as-ejb-remote-server-side/CalculatorBean!" + RemoteCalculator.class.getName()
  );
}

The questions are:

  1. What have been deployed to server? Class / Object / both? How did client find classes/objects?
  2. How did ejb:/jboss-as-ejb-remote-server-side/CalculatorBean!" + RemoteCalculator.class.getName() help hence it doesn't know host:port?
  3. Where was executed CalculatorBean logic on the server or client side? Again if it was executed at the Server, how did exchange of values between two jvms worked?

Upvotes: 5

Views: 12825

Answers (2)

Sampada
Sampada

Reputation: 2991

EJBs are based on the concept of RMI (remote method invocation) over IIOP that helps bring into the java world.

On the server side, there are three parts:

  1. EJB implementation - concrete class containing implementation methods.
  2. Home Interface - interface extending javax.ejb.EJBHome that must contain a create() method. Used by client code to create a bean instance.
  3. Remote Interface - interface extending javax.ejb.EJBObject that declares all methods that you'd wish your client code should be able to access.

On the client side, you must first lookup the ejb. This is where JNDI comes in picture. There is no need of host:port because you have to register the bean in the Java Naming Directory on the server side. This registry is made with a name (represented by ejb:/jboss-as-ejb-remote-server-side/CalculatorBean!" + RemoteCalculator.class.getName() ) in your case.

This is published by the server and the client uses it to access the Home interface. This in turn gives you access to all the implemented methods in the bean, that are declared in the remote interface.

Hope this addresses your three questions.

Upvotes: 3

  1. The client finds the classes/objects because the client have dependencies to the interfaces of the server-side (pom.xml file).

  2. It finds the host:port thanks to the file jboss-eap-quickstarts/ejb-remote/client/src/main/resources/jboss-ejb-client.properties Note that you can configure your server to expose your services on a different port.

  3. CalculatorBean logic will be executed on the server side.

For all questions that i've not answered, (What's deployed where, exchange of values between jvm and how the JNDI address (ejb:/ ...) allow the client to find the correct service), you should read about how JNDI and EJBs work.

Here is a good start :

JNDI: http://www.javaworld.com/article/2076888/core-java/jndi-overview--part-1--an-introduction-to-naming-services.html

EJBs: http://www.javaworld.com/article/2071724/java-web-development/ejb-fundamentals-and-session-beans.html

Upvotes: 5

Related Questions