EJB remote client migration from JBoss AS 7.1 to Wildfly 8.1

◇◆丶佛笑我妖孽 提交于 2019-12-02 05:50:05

You need to do two changes

instead of using "remote://localhost:4447" use "http-remoting://localhost:8080"

jndiProps.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");

When you configure jndi properties in code, lookup name should not contain ejb:

MyBeanRemote myBean = (MyBeanRemote) ctx.lookup("/training//MyBean!mypackage.MyBeanRemote");

this solution is tested and working

The provider URL should be http-remoting://localhost:8080 instead of remote://localhost:4447

I'm using:

  • wildfly-10.0.0.Final(JBoss Application Server).

That works to me:

Properties jndiProperties = new Properties();
           jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
           jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
           jndiProperties.put("jboss.naming.client.ejb.context", true);
           jndiProperties.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");   //System.getProperty(Context.PROVIDER_URL, "http-remoting://localhost:8080"));

        /* jndiProperties.put(Context.SECURITY_PRINCIPAL, "user");
           jndiProperties.put(Context.SECURITY_CREDENTIALS, "xxx");*/

        InitialContext context = new InitialContext(jndiProperties);

        MyFirstEJBRemote ejb = (MyFirstEJBRemote) context.lookup("/EJBProjectName/MyFirstEJB!src.MyFirstEJBRemote");

Hope it helps!

Here is my Project called "MyFirstEJBProject". Inside of the code are some notes to help.

package src.ejb;

import javax.ejb.Stateless;

/**
 * Session Bean implementation class MyFirstEJB
 */
@Stateless
public class MyFirstEJB implements MyFirstEJBRemote {

    public MyFirstEJB() {
    }

    @Override
    public String helloWorld() {
        return "Hello World EJB";
    }

}


package src.ejb;

import javax.ejb.Remote;

@Remote
public interface MyFirstEJBRemote {
    public String helloWorld();
}


package src.clientTest;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import src.ejb.MyFirstEJB;
import src.ejb.MyFirstEJBRemote;

public class EJBClient {

    public static void main(String[] args) throws NamingException {

        /**JNDI or Java Naming and Directory Interface.
         * When JNDI constructs an initial context, the context's environment
         * is initialized with properties defined in the environment parameter
         * passed to the constructor, the system properties, the applet parameters,
         * and the application resource files.
         * 
         * JNDI applications need a way to communicate various preferences
         * and properties that define the environment in which naming and
         * directory services are accessed.
         * */

        Properties jndiProperties = new Properties();
        jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
        jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
        jndiProperties.put("jboss.naming.client.ejb.context", true);
        jndiProperties.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");

        /* jndiProperties.put(Context.SECURITY_PRINCIPAL, "user");
           jndiProperties.put(Context.SECURITY_CREDENTIALS, "xxx");*/

        /**Context For JNDI**/
        InitialContext context = new InitialContext(jndiProperties);

        /**The nameOfEJB appears on the console when the server is starting up.**/
        String nameOfEJB = "/MyFirstEJBProject/MyFirstEJB!src.ejb.MyFirstEJBRemote";

                                                  /**The Method .lookup("") search for EJB by name**/
        MyFirstEJBRemote ejb = (MyFirstEJBRemote) context.lookup(nameOfEJB);
        System.out.println(ejb.helloWorld());


        System.out.println("/** =============== 2º TEST ===============**/"); 

        /**getting a EJB name by a private Method.**/
        String nameOfEJB_2 = getEJBName("", "MyFirstEJBProject", "", 
                MyFirstEJB.class.getSimpleName(), MyFirstEJBRemote.class.getName());

        MyFirstEJBRemote ejb_2 = (MyFirstEJBRemote) context.lookup(nameOfEJB_2);
        System.out.println(ejb_2.helloWorld());
    }

    private static String getEJBName(String nameofAppEAR, String nameOfProjectModulo, 
            String distinctNameOfProject, String classBeanSimpleName, String classInterfaceName){
        /**Return a object name for search on JNDI */

        String finalNameOfEJB = nameofAppEAR + "/" + nameOfProjectModulo + "/" + distinctNameOfProject + 
                "/" + classBeanSimpleName + "!" + classInterfaceName;  

        return finalNameOfEJB; 

        /**Ex:                 
           String nameofAppEAR= "";                            // EAR  (if there is)
           String nameOfProjectModulo= "MyFirstEJBProject";
           String distinctNameOfProject= "";                   // Alias (if there is)
           String classBeanSimpleName= MyFirstEJB.class.getSimpleName(); 
           String classInterfaceName=  MyFirstEJBRemote.class.getName();

           String finalNameOfEJB = "" + "/" + "MyFirstEJBProject" + "/" + "" + 
                "/" + MyFirstEJB.class.getSimpleName() + "!" + MyFirstEJBRemote.class.getName();

           The nameOfEJB appears on the console when the server is starting up:
           String nameOfEJB = "/MyFirstEJBProject/MyFirstEJB!src.ejb.MyFirstEJBRemote";   
         */
    }
}

Notes: I am using Wildfly(JBoss), so it is needed to import the jboss-client.jar at wildfly-10.0.0.Final\bin\client\ folder.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!