I\'m using JBoss Server for EJB And I need JNDI in console app to get reference of session bean, console app code looks like this
import java.util.Properties
you may need add jboss-client.jar and jboss-ejb3-common-client.jar into your library
You can use following context to connect. I have tried and tested to set up this.
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingException;
public class Program {
public static void main(String[] args) throws NamingException {
Properties jndiProps = new Properties();
jndiProps.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jboss.naming.remote.client.InitialContextFactory");
jndiProps.put(Context.PROVIDER_URL,"remote://localhost:4447");
jndiProps.put(Context.SECURITY_PRINCIPAL, "testuser");
jndiProps.put(Context.SECURITY_CREDENTIALS, "testpassword");
jndiProps.put("jboss.naming.client.ejb.context", true);
Context ctx = new InitialContext(jndiProps);
}
}
Then i got this error
JBREM000200: Remote connection failed: javax.security.sasl.SaslException:
Authentication failed: all available authentication mechanisms failed - Could
not register a EJB receiver for connection to remote://localhost:4447
java.lang.RuntimeException: javax.security.sasl.SaslException: Authentication
failed: all available authentication mechanisms failed.
Then i added the user using add-user.sh.
Successful Handshake message came.
To overcome above error you need to pay attention to two points.
First , You need to have jboss-client.jar in classpath.
Second, according to Jboss version you are using you need to change your provider url.
For JBossAS 5 you should set following properties in environment
env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.NamingContextFactory");
env.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
env.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099");
For JBossAS 7 you should set following properties in environment
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, "remote://localhost:4447"));
env.put(Context.SECURITY_PRINCIPAL, System.getProperty("username", DEFAULT_USERNAME));
env.put(Context.SECURITY_CREDENTIALS, System.getProperty("password", DEFAULT_PASSWORD));
You need dependency:
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jboss-remote-naming</artifactId>
</dependency>
https://mvnrepository.com/artifact/org.jboss/jboss-remote-naming/
For WildFly you can use org.wildfly.naming.client.WildFlyInitialContextFactory form:
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-naming-client</artifactId>
</dependency>
https://mvnrepository.com/artifact/org.wildfly/wildfly-naming-client
You can use bom file for import:
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-ejb-client-bom</artifactId>
<type>pom</type>
</dependency>
https://mvnrepository.com/artifact/org.wildfly/wildfly-ejb-client-bom