How do I access EJB Remote from a different EAR on WebLogic 11g?

0

I'm doing an application in WebLogic 11g (WebLogic 10.3.6), this server uses JDK6, I want to access a Remote EJB from an EAR different from the one that contains the EJB, I'm working in Eclipse Oxygen with the OEPE tools, my code is this:

Regards.java ( is contained in TestingRemotionEAR / TestingRemotionEJB

)

package com.beans;

import javax.ejb.Stateless;

/**
 * Session Bean implementation class Saludos
 */
@Stateless
public class Saludos implements SaludosRemote {

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

    @Override
    public String Saludo(String nombre) {
        // TODO Auto-generated method stub
        return "Hola " + nombre;
    }

}

GreetingsRemote.java ( is contained in TestingRemotionEAR / TestingRemotionEJBClient

)

package com.beans;

import javax.ejb.Remote;

@Remote
public interface SaludosRemote {

    public String Saludo(String nombre);

}

The following is an Servlet of the EAR that I want to access the previous EJB, I understand that basically here is where the problem is, in the way to access the JNDI of the WebLogic:

Hola.java ( is contained in TryingRemoteWEBEAR / TryingRemote )

import javax.naming.Context;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.beans.Saludos;

/**
 * Servlet implementation class Hola
 */
public class Hola extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Hola() {
        super();
        // TODO Auto-generated constructor stub
    }

    Context contexto;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        PrintWriter out = response.getWriter();

        out.println("<html><body>");

        Saludos saludos = null;
        try {
            saludos = (Saludos) contexto.lookup("SaludosRemote");
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        out.println("<h1>" + saludos.Saludo("Gabriel") + "</h1>");      
        out.println("</body></html>");


    }
}

What I am doing so that Hola.java recognizes the EJBs is generating a jar of Saludos.java and placing that jar as a Hello library. java , but in the end it always gives me an error 500 on the server, it is not the first code I have of this, I have tried several but they all give me the same error 500:

Error 500--Internal Server Error

java.lang.NullPointerException
    
asked by Ricardo Gabriel 10.11.2017 в 21:50
source

1 answer

0

The error java.lang.NullPointerException is because you are accessing the method of an object that you have not instantiated. in this case it is the variable contexto . Try the following:

import javax.naming.Context;
import javax.naming.InitialContext
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.beans.SaludosRemote;
import java.util.Hashtable;


public class Hola extends HttpServlet {

    private static final long serialVersionUID = 1L;

    private Context contexto;

    public Hola() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //Este trozo de código es necesario para conectarse remotamente al ejb en weblogic
        Hashtable<String, Object> env = new Hashtable<String, Object>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
        //aqui debes colocar el host y la ip de tu weblogic
        env.put(Context.PROVIDER_URL, "t3://localhost:7001");
        //aqui debes colocar el nombre del usuario con el que te conectas a tu weblogic
        env.put(Context.SECURITY_PRINCIPAL, "weblogic");
        //aqui debes colocar la clave del usuario con el que te conectas a tu weblogic
        env.put(Context.SECURITY_CREDENTIALS, "weblogic1");

        PrintWriter out = response.getWriter();

        out.println("<html><body>");

        SaludosRemote saludos = null;

        try {

            contexto = new InitialContext(env);
            // la variable que colocas en el método lookup corresponde al jndi de tu ejb remoto en weblogic
            saludos = (SaludosRemote) contexto.lookup("SaludosRemote");

        } catch (NamingException e) {
            e.printStackTrace();
        }

        out.println("<h1>" + saludos.Saludo("Gabriel") + "</h1>");      
        out.println("</body></html>");

    }
}

If you are consuming your remote ejb within the same domain, you do not need to place the user and the password, you can delete or comment on the code:

env.put(Context.SECURITY_PRINCIPAL, "weblogic");
env.put(Context.SECURITY_CREDENTIALS, "weblogic1");

You must also have the JAR client of your ejb in the project that wants to access the remote ejb, as a dependency if you occupy maven or as a library in WEB-INF / lib.

To validate the name of your remote ejb, you can do it by entering the jndi tree of your weblogic server, accessing the web administration console, inside the console, click on Server, AdminServer, View JNDI Tree, a window will appear where the available JNDIs are located, you must navigate and look for the one of your ejb.

I hope you serve, greetings.

    
answered by 15.11.2017 в 21:21