EJB无法使用@EJB在Wildfly 9.0.0中初始化

时间:2018-09-25 17:39:11

标签: jboss ejb ejb-3.0 ejb-3.1 wildfly-9

我正在尝试从EJB2.x迁移到EJB3.x,并且我正在使用Wildfly 9.0.0。 旧的EJB2.x在JBoss 4.2.2中工作,这是这样的:

public interface WUFFacadeRemote extends EJBObject {
    public ClientData getItems(ClientData data);
    public ClientData save(ClientData data);
}

public interface WUFFacadeHome extends EJBHome {
    public WUFFacadeRemote create();
}

public class WUFFacade {
    public ClientData getItems(ClientData data) { 
        //code here
    }

    public ClientData save(ClientData data) {
        //code here
    }
}

public class WUFAction extends HttpServlet implements IAction {
    public void doPost(HttpServletRequest request, HttpServletResponse response) {

        ... 

        Object objRef = ic.lookup("java:comp/env/wUF");

        com.wuf.WUFFacadeHome home = (com.wuf.WUFFacadeHome) PortableRemoteObject.narrow(objRef, com.wuf.WUFFacadeHome.class);

        engine = home.create();

        //engine gets the reference, and I can use it normally.
        ...
    }
}

我也有ejb-jar.xml,它正在工作。现在,我正在考虑的针对EJB3.x和Wildfly 9.0.0的解决方案如下:

@WebServlet(urlPatterns = "windows/wUF.do", loadOnStartup = 1)
public class WUFAction extends HttpServlet implements IAction {

    @EJB
    private WUFFacadeRemote engine; 

    public void doPost(HttpServletRequest request, HttpServletResponse response) {

        //Here I should be able to use my engine.
        //Wildfly starts and I call the page, engine is not null at this moment,
        //but after I call the page again, it becomes null and remains null.
    }
}

@Stateless
@Remote(WUFFacadeRemote.class)
public class WUFFacade extends RootFacade implements WUFFacadeRemote, Serializable {
    public WUFFacade() { }

    @EJB
    FUFHome home;

    public ClientData getItems(ClientData data) {
        //code here
    }

    public ClientData save(ClientData data) {
        //code here
    }

    private Col load(ClientData data,InitialContext ic) {
        //here i'm calling home.
        // but home is always null. It was supposed to have the @EJB reference initialized.
        //But instead I get a null pointer...
        home.findByFilter(loader);
    }
}

@Remote(FUFHome.class)
public interface FUFHome {

    FUF create(FUFValue fUFValue);

    FUF findByPrimaryKey(FUFPK  pk);

    Collection findByFilter(FacadeLoader loader);
}



public interface WUFFacadeRemote{
    public ClientData getItems(ClientData data);
    public ClientData save(ClientData data);
}

我不再有ejb-jar.xml,部署已成功完成,Wildfly开始没有错误。然后,当我第一次调用有问题的页面时,@ EJB似乎正在工作(调试是“ bus-facade / WUFFacade”的远程EJB StatelessEJBLocator的代理,视图是com.wuf.WUFFacadeRemote接口,亲和力为“无”) ,该值不为null,但对于所有后续调用,我的变量为null,并且得到了NullPointerException。

我真的不知道我在做什么错(也许我完全迷路了),但是对我来说,@ EJB应该可以正常工作。我想念什么?谢谢。

当我使用EJB3.x时,我现在仅使用注释(这似乎可以)。

JNDI:

部署中名为FUF的会话bean的JNDI绑定

java:global/fumo/bus-entities-fumo/FUF!apyon.components.fumo.fuf.FUF
java:app/bus-entities-fumo/FUF!apyon.components.fumo.fuf.FUF
java:module/FUF!apyon.components.fumo.fuf.FUF
java:global/fumo/bus-entities-fumo/FUF
java:app/bus-entities-fumo/FUF
java:module/FUF

名为WUFFacade的会话bean的JNDI绑定

java:global/fumo/bus-facade-fumo/WUFFacade!apyon.fumo.wuf.WUFFacadeRemote
java:app/bus-facade-fumo/WUFFacade!apyon.fumo.wuf.WUFFacadeRemote
java:module/WUFFacade!apyon.fumo.wuf.WUFFacadeRemote
java:jboss/exported/fumo/bus-facade-fumo/WUFFacade!apyon.fumo.wuf.WUFFacadeRemote
java:global/fumo/bus-facade-fumo/WUFFacade
java:app/bus-facade-fumo/WUFFacade
java:module/WUFFacade

1 个答案:

答案 0 :(得分:0)

我认为我找到了解决该问题的方法。我仍然会尝试找到另一个,但是到目前为止很好。

更改为.war并将我的其他项目保留在.ears之后,它开始工作。可能是因为我的main.ear中有一个RootController servlet,这是应用程序的起点。上下文从此处开始,然后重定向到fumo.ear(现在为fumo.war)。

由于某种原因,进入页面后,我的EJB总是空值。当我第一次进入JSP并尝试再次调用该页面时,总是感到很沮丧。我对此的解决方案是:

@WebServlet(urlPatterns = "windows/wUF.do", loadOnStartup = 1)
public class WUFAction extends HttpServlet {

    private WUFFacadeRemote engine; 

    public void doGet(HttpServletRequest req, HttpServletResponse resp) {
        doPost(req, resp);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) {

        if(engine == null) {
            InitialContext ic; 
            try {
                ic = new InitialContext();
                engine = (WUFFacadeRemote) ic.lookup("java:global/fumo/WUFFacade!fumo.wuf.WUFFacadeRemote");
            } catch (NamingException e) {
                e.printStackTrace();
            }
        }

        //here I always have the context now.
    }
}

作为.war,我的结构现在看起来像这样:

enter image description here enter image description here

因此@Inject@EJB之类的其他注释现在也可以使用。总是在从调用Servlet或某些操作的JSP重定向时,首先要检查上下文是否不为null,否则我将对其进行查找。我的@Stateless工作正常,而@PersistenceContext@Remote也正常工作。

 @Stateless
 public class WUFFacade implements WUFFacadeRemote {
     @Inject
     private FUFRules rules;

     @EJB
     private FUFHome home;

    private Col load(ClientData data, InitialContext ic) throws InterfaceException {
        try {
            // home here is nor null anymore.
            Collection res = (Collection) home.findByFilter(loader);
            ...
        } catch (InterfaceException e) {
            e.printStackTrace();
        }
        ...
        return data;
    }
}

因此,我要感谢所有在主题中提供帮助的人。这是了解和查看问题或找到解决方法的好方法。正如我说的那样,将来我仍然会尝试使用.ear,但是作为简化包装,它肯定可以使用。