Wednesday, May 26, 2010

OSB and EJB 3.0 Client Jar File

Now, we want to expose an EJB 3.0 Stateless Session Bean in a Business Service in OSB11.

Create an EJB 3.0 Project, and ASSOCIATE IT to a EAR, and enable generation of EJB Client Jar.

The EJB will look like this:

package com.lassi.mejb;
import javax.ejb.Stateless;

/**
 * Session Bean implementation class LassiEJB
 */
@Stateless(mappedName="
LassiEJB")
public class LassiEJB implements LassiEJBRemote, LassiEJBLocal {

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

    @Override
    public String hello(String message) {
        System.out.println("message=" + message);
        return "hello " + message;
    }

    @Override
    public String helloLocal(String message) {
        System.out.println("messagelocal=" + message);
        return "hellolocal " + message;
    }

   
}




and the interfaces:


package com.lassi.mejb;
import javax.ejb.Remote;

@Remote
public interface LassiEJBRemote {
    String hello(String message);
}



and


package com.lassi.mejb;
import javax.ejb.Local;

@Local
public interface LassiEJBLocal {
    String helloLocal(String message);
}



To generate the Client EJB Jar, just export the EAR project as an EAR, then unzip it.
You will get a LassiEJBProjectClient.jar in APP-INF/lib.
Otherwise you can right-click on the EJB project, choose Java EE  Tools and Generate EJB Client JAR.

Make sure that your Local and Remote interfaces are created in the EJBClient project, otherwise your EJB Client JAR will contain only the manifest.mf file and you will be very disappointed.

THIS EJB Client JAR is the JAR you need to import into OSB.

To find the JNDI name of your EJB click on the WLServer in the console and look into "View JNDI tree".
It should be something like LassiEARLassiEJBProject_jarLassiEJB_Home
Also, in OSB create a JNDI provider with t3://localhost:7001 and call it myjndiprovider

Create a Business Service, Transport Typed,
the URI will be like ejb:myjndiprovider:LassiEJB#com.lassi.mejb.LassiEJBRemote

In the Business Interface  you can choose the Local or the Remote interface (of course if you choose Local it will be faster! But if you use the JNDI name of the Remote interface it will not work... I presume ???? )....


At the end, if you get a

java.lang.ClassCastException: weblogic.ejb.container.internal.StatelessEJBHomeImpl

or a


java.lang.ClassCastException: $Proxy224
 at com.bea.wli.sb.transports.ejb.Jws.hellopierrelocal(Unknown Source)



most likely it means you are binding to the wrong JNDI name...

1 comment:

Jesús María Guerra said...

Hi PierLuigi,

I get at the same error. Annoying but the solution is set the mappedName parameter at the bean definition

@Stateless (mappedName="Somethingweird")

I have just loose 4 hours looking for the solution.

IHTH