pablosz
pablosz

Reputation: 55

JNDI name for EJB 3.1 running on JBoss 7

I'm trying to run this EJB example on JBoss 7 but it always throws NameNotFoundException.

The EJB that I try to test is: Calculator. When I deploy it on JBoss 7 show this next lines in the server console:

java:global/CalculadoraProject/Calculator!demo.CalculatorRemote
java:app/CalculadoraProject/Calculator!demo.CalculatorRemote
java:module/Calculator!demo.CalculatorRemote
java:jboss/exported/CalculadoraProject/Calculator!demo.CalculatorRemote
java:global/CalculadoraProject/Calculator
java:app/CalculadoraProject/Calculator
java:module/Calculator

The Calculator bean code is this:

package demo;
import javax.ejb.Stateless;

@Stateless
public class Calculator implements CalculatorRemote
{
    @Override
    public double sum(double a, double b)
    {
        return a+b;
    }
}

And the remote interface code is:

package demo;
import javax.ejb.Remote;

@Remote
public interface CalculatorRemote
{
    public double sum(double a,double b);
}

The main code which must connect the EJB through JNDI is:

package demo;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;

public class Test
{
    public static void main(String[] args) throws Exception
    {
        Context ctx=getInitialContext();

        String jndiName="?????????????";
        CalculatorRemote c = (CalculatorRemote)ctx.lookup(jndiName);

        System.out.println( c.sum(2,2) );
    }

    private static Context getInitialContext() throws Exception
    {
        Properties p=new Properties();
        p.put("java.naming.factory.initial","org.jboss.naming.remote.client.InitialContextFactory");
        p.put("java.naming.provider.url","remote://localhost:4447");

        return new InitialContext(p);
    }
}

First error is: Authentication failed. So, I create an "Management User" user for "ApplicationRealm" into the server: usr: test, pwd: test123 and then modify getInitialContext() to look like this:

Properties p=new Properties();        
p.put("java.naming.factory.initial","org.jboss.naming.remote.client.InitialContextFactory");
p.put("java.naming.provider.url","remote://localhost:4447");
p.put(Context.SECURITY_PRINCIPAL,"test");
p.put(Context.SECURITY_CREDENTIALS,"test123");

The next error is: javax.naming.NameNotFoundException: ?????????????

So, I want ask: What name must I use to lookup my Calculator bean?

Thanks!

Upvotes: 1

Views: 3363

Answers (1)

ptrdom
ptrdom

Reputation: 108

1) by using global JNDI name.

java:global/CalculadoraProject/Calculator!demo.CalculatorRemote

2) by using ejb JNDI name. Play with slashes a little if you still get the error. Should be correct if you have the required libs and don't have syntax errors in other places of code.

ejb:/CalculadoraProject/Calculator!demo.CalculatorRemote

JNDI structure
For stateless beans:

ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>

For stateful beans:

ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>?stateful

Links worth reading:
https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI
https://docs.jboss.org/author/display/AS71/Remote+EJB+invocations+via+JNDI+-+EJB+client+API+or+remote-naming+project

Hope this helped :)

Upvotes: 1

Related Questions