Hot questions for Using Enterprise JavaBeans in servlets

Question:

I am learning EJB 3 and trying some simple stuff on it.

I am using eclipse -> Luna Service Release 1 (4.4.1) with JBOSS AS 7.1.1 (final)

I created a simple EJB project in eclipse with name -> SalutationEJBProj. Under this project created a simple session, stateless Bean.

code:
package com.vipin.bean.session.stateless;

import javax.ejb.Stateless;

@Stateless(mappedName="SalutationBean")
public class SalutationBean implements SalutationBeanRemote {

public SalutationBean() {

}

@Override
public String getFormalSalutation(String name) {

    return "Dear" + name;
}

@Override
public String getInfomalSalutation(String name) {

    return "Hi" + name;
}

}
The Remote interface is:
package com.vipin.bean.session.stateless;

import javax.ejb.Remote;

@Remote
public interface SalutationBeanRemote {

public String getFormalSalutation(String name);

public String getInfomalSalutation(String name);
}
I am able to deploy it in Jboss AS 7.1.1 and here is the JNDI bindings output when i start the jboss:
    java:global/SalutationEJBProj/SalutationBean!com.vipin.bean.session.stateless.SalutationBeanRemote
java:app/SalutationEJBProj/SalutationBean!com.vipin.bean.session.stateless.SalutationBeanRemote
java:module/SalutationBean!com.vipin.bean.session.stateless.SalutationBeanRemot
java:jboss/exported/SalutationEJBProj/SalutationBean!com.vipin.bean.session.stateless.SalutationBeanRemote
java:global/SalutationEJBProj/SalutationBean
java:app/SalutationEJBProj/SalutationBean
java:module/SalutationBean

Now, i want this ejb to be accessible by an external Servlet, for this I created a dynamic web project with name --> SalutationServletProj

Here is the code of the simple Servlet:
package com.vipin.servlet;

@WebServlet(urlPatterns = { "/SalutationServlet" })
public class SalutationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

@EJB
private SalutationBean salutationBean;  // <-- How will this be accessible, this is in a different project, how do we import this?

public SalutationServlet() {
    super();

}

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

}

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

}
}

My intention is to use @EJB annotation (or any other method) to get the instance of the EJB in the Servlet.

As these two are in different projects, how can i achieve this? I can't use like this in servlet:

import com.vipin.bean.session.stateless.SalutationBean

1) What are the files from EJB project that i have to copy into servlet project.

2) Will it work as @EJB annotation or do i need to do JNDI lookup? (Because the servlet is remote).

3) Where can i find the classes/libraries generated in jboss?

Any help highly appreciated.


Answer:

it should be sufficient to only add the SalutationBeanRemote into WEB-INF/classes of your web project.

Then it should work out if you add the following member to your Servlet:

@EJB(lookup="java:global/SalutationEJBProj/SalutationBean")
private SalutationBeanRemote salutationBean;

Only the java:global... names will work because your servlet is running in another module and application as the EJB. I am unsure if the java:jboss/exported... lookup name works or if it is only for remote clients.

Best regards, Robert

Question:

We have migrated our application from weblogic 10.0 to jboss EAP 6.1.0.

While accessing the properties which is loaded in the init() method of the servlet from our EJB, the value returned is null.

Below method is in Ejbbean class:

public void execute() throws EJBException
    {
System.out.println("Constants.x in execute  ----"+Constants.x);
}

the value printed here is null. Although if we use the same code inside our application anywhere other than EJB, the correct value is returned.

We are loading the properies inside the init method of servlet.

public void init() throws javax.servlet.ServletException 
    {
ServletContext sContext = this.getServletContext();
        try 
        {
    String propFile = sContext.getInitParameter("abc");
            Properties prop = new Properties();
            FileInputStream inStream = new FileInputStream(propFile);
            prop.load(inStream);
Constants.loadValues(prop);
 System.out.println("Constants.x in execute  ----"+Constants.x);
}

the correct value for static variable x is printed here.

        public final class Constants
        {

            public static String x = null;

    public final static void loadValues(Properties prop) throws Exception
        {
    // the properties are loaded here...
    }
}

Whats the reason behind this unexpected behaviour of static variable inside EJB when the same code is working fine elsewhere? This problem was not there in the application when it was deployed on weblogic server.

How can this be fixed?


Answer:

I got the solution to this problem. jboss-deployment-structure.xml needs to be written for the application with the dependency of the EJB on the WAR.

Reference link: http://www.mastertheboss.com/jboss-server/jboss-deploy/configuring-jboss-as-7-deployment-order