Question

I'm developing a JAX-WS WebService in JDeveloper 11.1.1.4 that should use EJBs from a JAR previously deployed to a WebLogic server. Both the WebService project and the EJB project are my own code, but I'd like to deploy them separately. For now I'm experimenting with the setup.

In the ExampleEJB project I have a bean ExampleBean that implements a remote interface Example.

@Remote
public interface Example {
    public String doRemoteStuff();
}

@Stateless(name = "Example", mappedName = "ExampleApplication-ExampleEJB-Example")
public class ExampleBean implements Example {
    public String doRemoteStuff() {
        return "did remote stuff";
    }
}

In that project, I have two deploy descriptors (ejb-jar.xml and weblogic-ejb-jar.xml):

ejb-jar.xml

<?xml version = '1.0' encoding = 'UTF-8'?>
<ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
         version="3.0" xmlns="http://java.sun.com/xml/ns/javaee">
  <enterprise-beans>
    <session>
      <ejb-name>Example</ejb-name>
    </session>
  </enterprise-beans>
</ejb-jar>

weblogic-ejb-jar.xml

<?xml version = '1.0' encoding = 'UTF-8'?>
<weblogic-ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-ejb-jar http://www.bea.com/ns/weblogic/weblogic-ejb-jar/1.0/weblogic-ejb-jar.xsd"
                  xmlns="http://www.bea.com/ns/weblogic/weblogic-ejb-jar">
  <weblogic-enterprise-bean>
    <ejb-name>Example</ejb-name>
    <stateless-session-descriptor/>
  </weblogic-enterprise-bean>                  
</weblogic-ejb-jar>

Additionaly, I've created an EJB JAR deployment profile named example-ejb.jar and managed to deploy it to the server.

In the ExampleWS project I have an ExampleWebService:

@WebService(serviceName = "ExampleWebService")
public class ExampleWebService {

    @EJB
    Example example;

    public String doStuff() {
        return example.doRemoteStuff();
    }
}

I added the ExampleEJB project dependency to this project (so it would compile). The only XML I have in this project is the web.xml used to describe the servlet. Also, I have the WebServices WAR file created automatically by jDeveloper when creating a WebService. Lastly, I created an EAR deployment profile named example-ws that only includes the WebServices WAR file in it's application assembly.

What do I need to do for this to work? Also, what would the procedure be if the ExampleEJB project was referenced from another project (say, AdditionalExampleEJB) that has additional beans that use ExampleBean? How would I reference the ExampleBean from there?

Thank you VERY MUCH for any help you can give me!

EDIT:

I've managed to reference the EJB from the WebService!

In the ExampleEJB project I modified the weblogic-ejb-jar.xml and now it looks like this:

weblogic-ejb-jar.xml

<?xml version = '1.0' encoding = 'UTF-8'?>
<weblogic-ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-ejb-jar http://www.bea.com/ns/weblogic/weblogic-ejb-jar/1.0/weblogic-ejb-jar.xsd"
                  xmlns="http://www.bea.com/ns/weblogic/weblogic-ejb-jar">
  <weblogic-enterprise-bean>
    <ejb-name>Example</ejb-name>
    <stateless-session-descriptor>
      <pool>
        <max-beans-in-free-pool>10</max-beans-in-free-pool>
        <initial-beans-in-free-pool>3</initial-beans-in-free-pool>
      </pool>
      <business-interface-jndi-name-map>
        <business-remote>hr.example.Example</business-remote>
        <jndi-name>ejb/example-ejb/Example</jndi-name>
      </business-interface-jndi-name-map>
    </stateless-session-descriptor>
  </weblogic-enterprise-bean>                  
</weblogic-ejb-jar>

In the ExampleWS project I added a deployment descriptor weblogic.xml that looks like this:

weblogic.xml

<?xml version = '1.0' encoding = 'UTF-8'?>
<weblogic-web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-web-app http://www.bea.com/ns/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd"
                  xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app">
  <ejb-reference-description>
    <ejb-ref-name>ExampleReference</ejb-ref-name>
    <jndi-name>ejb/example-ejb/Example</jndi-name>
  </ejb-reference-description>
</weblogic-web-app>

Note that the ExampleReference value and ejb/example-ejb/Example value are something I decided to enter - I think they is more or less a developer's choice.

Also, I referenced the EJB in my WebService using the ExampleReference value, so my ExampleWebService looks like this:

ExampleWebService

@WebService(serviceName = "ExampleWebService")
public class ExampleWebService {
    @EJB(
      name="ExampleReference"
    )
    Example example;

    public String doStuff() {
        return example.doRemoteStuff();
    }
}

Lastly, in the deployment profile of ExampleWS (the WebServices.war) I added the dependency contributor and checked the interface Example.class element (NOT the ExampleBean.java that has the implementation).

Now, how would this work if the Example bean was referenced from another EJB project (not a WebService)?

Was it helpful?

Solution

So, for all those that encounter the same problem, I have solved it. There is no way to look up a remote EJB in EJB 3.0 other than using InitialContext.lookup("jndi/name"). Also, narrowing the object seems to help in some ClassCastException situations, so I tend to do it as a precaution. This is how I look up my EJBs:

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

import javax.rmi.PortableRemoteObject;

public Object lookup (String jndiName, Class type) throws NamingException {
    return PortableRemoteObject.narrow(InitialContext.doLookup(jndiName), type);
}

If using EJB 3.1, there is a way using @EJB(lookup = "jndi/name"), but since I'm not using this version, I cannot guarantee that this works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top