Andrea Sindico
Andrea Sindico

Reputation: 7440

Cannot get Bean from Client Application

I have deployed an EJB3 app with one module and one session bean called User Data in Wildfly. This is my project structure: enter image description here

This is the UserData Stateful Bean:

@Stateful
@Remote(UserDataRemote.class)
public class UserData implements UserDataRemote, UserDataLocal {

    private String name;

    /**
     * Default constructor. 
     */
    public UserData() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void saveName(String name) {
        // TODO Auto-generated method stub
        this.name =name;
    }

    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return this.name;
    }

}

This is a crop of the console:

enter image description here

This is how I am trying to retrieve it from an application via JNDI:

public static void main (String args[]) {
    Properties jndiProperties = new Properties();
    jndiProperties.put("jboss.naming.client.ejb.context", true);
    jndiProperties.put(Context.PROVIDER_URL, "http-remoting://127.0.0.1:8080");
    jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
    try {
        InitialContext namingContext = new InitialContext(jndiProperties);
        UserDataRemote userRemote = (UserDataRemote)namingContext.lookup("java:global/testEJB/UserData!entities.UserDataRemote");

    } catch (NamingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

** UPDATE** I am also trying to use this url for the lookup:

UserDataRemote userRemote = (UserDataRemote)namingContext.lookup("ejb:/testEJB//UserData!entities.UserDataRemote?stateful");

But it can not find it as well.

This is the error I get:

ott 06, 2017 7:45:51 AM org.xnio.Xnio <clinit>
INFO: XNIO version 3.4.0.Final
ott 06, 2017 7:45:51 AM org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.4.0.Final
ott 06, 2017 7:45:51 AM org.jboss.remoting3.EndpointImpl <clinit>
INFO: JBoss Remoting version 4.0.21.Final
ott 06, 2017 7:45:52 AM org.jboss.ejb.client.remoting.VersionReceiver handleMessage
INFO: EJBCLIENT000017: Received server version 2 and marshalling strategies [river]
ott 06, 2017 7:45:52 AM org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
INFO: EJBCLIENT000013: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@6b09bb57, receiver=Remoting connection EJB receiver [connection=Remoting connection <13359fd6> on endpoint "config-based-naming-client-endpoint" <6536e911>,channel=jboss.ejb,nodename=macbook-pro-di-andrea]} on channel Channel ID 96d8a7d7 (outbound) of Remoting connection 1f36e637 to /127.0.0.1:8080 of endpoint "config-based-naming-client-endpoint" <6536e911>
javax.naming.NameNotFoundException: global/testEJB/UserData!entities.UserDataRemote -- service jboss.naming.context.java.jboss.exported.global.testEJB."UserData!entities.UserDataRemote"
    at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:106)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:207)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:184)
    at org.jboss.naming.remote.protocol.v1.Protocol$1.handleServerMessage(Protocol.java:127)
    at org.jboss.naming.remote.protocol.v1.RemoteNamingServerV1$MessageReciever$1.run(RemoteNamingServerV1.java:73)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

Upvotes: 0

Views: 422

Answers (2)

jaysee
jaysee

Reputation: 466

I suppose, you should use other values for jndiProperties and use another value for the lookup of the EJB. I've adapted your code below.

public static void main (String args[]) {
    Properties jndiProperties = new Properties();
    jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
    jndiProperties.put("jboss.naming.client.ejb.context", true);

    try {
        InitialContext namingContext = new InitialContext(jndiProperties);
        UserDataRemote userRemote = (UserDataRemote)namingContext.lookup("ejb:/testEJB/UserData!entities.UserDataRemote");

    } catch (NamingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

The given lookup value works for stateless EJB which are deployed in a JAR-file (not an EAR-file). If your EJB is stateful, you have to add the following "?stateful" at the end. How to create the lookup value for EAR-files is described in the link down below.

The JNDI-Bindings shown by my Wildfly-server are similiar to yours. They didn't start with ejb:.... But the lookup works nevertheless.

Moreover there should be a file jboss-ejb-client.properties in the classpath at runtime. The classes in jboss-client.jar will look for this file.

Here is the content of mine:

endpoint.name=client-endpoint
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default

remote.connection.default.host=localhost
remote.connection.default.port=8080
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false

You can find more information here link.

Upvotes: -1

Simon Martinelli
Simon Martinelli

Reputation: 36163

If you are directly deploying the EJB JAR then the lookup should be

UserDataRemote userRemote =
  (UserDataRemote) namingContext.lookup("testEJB/UserData!entities.UserDataRemote");

If the EJB JAR is packed in a EAR then you need to add the EAR name (application name)

UserDataRemote userRemote =
  (UserDataRemote) namingContext.lookup("<ear-name>/testEJB/UserData!entities.UserDataRemote");

Upvotes: 2

Related Questions