Here are the steps to call a method of an EJB (v3.0) deployed in WebSphere 7 from a plain old Java client, either using Spring to shield your code from unnecessary implementation details, or bare-bones straightforward coding style.
Note: Tested against WAS 7 only.
Common Steps
1. Firstly, the Remote interface class is needed and will be referenced in your client code to call the EJB method. Copy the class file or Java source to your Java client workspace. In my example, the remote interface class is org.gizmo.minyakangin.CapKapakRemote2. If you have a local development WebSphere 7, run the following command to create the stub class
[Class_Folder_Path]:>[WAS_HOME]\AppServer\bin\createEJBStubs.bat org.gizmo.minyakangin.CapKapakRemote -cp .
You should see an output similar to the one below:
Processing the org.gizmo.minyakangin.CapKapakRemote input file.
Command Successful
A file with the name _CapKapakRemote_Stub.class will be created under folder [Class_Folder_Path]\org\gizmo\minyakangin.
3. A missing stub file may produce the following errors when getting a reference to the EJB:
java.lang.ClassCastException: Unable to load class: org.gizmo.minyakangin._CapKapakRemote_Stub
java.lang.ClassCastException: org.omg.stub.java.rmi._Remote_Stub incompatible with org.gizmo.minyakangin.CapKapakRemote
4. At the WebSphere Admin Console, locate the EJB application and ensure a proper JNDI reference is assigned e.g. ejb/CapKapak. It is best not to rely on the default JNDI reference. But if you insist, the default is the fully qualified remote class name i.e. org.gizmo.minyakangin.CapKapakRemote.
5. Also, check the bootstrap port of the application server instance hosting the EJB (my environment is 9812):
6. At your Java client workspace, ensure that com.ibm.ws.webservices.thinclient_7.0.0.jar (under [WAS_HOME]\AppServer\runtimes) and com.ibm.ws.runtime.jar (under [WAS_HOME]\AppServer\plugins) are included in the classpath. Not sure why including only com.ibm.ws.ejb.thinclient_7.0.0.jar doesn't work in my case.
Caveat: The steps only work using IBM JDK (v7). You may need to do some 'hacking' to get it working under a different JVM.
Using Spring 3.x
You can interface with a plain Java interface (no EJB classes or annotations needed) using Spring. Here's the interface:
package org.gizmo.minyakangin;
public interface CapKapak {
public String getBrand();
}
An EJB3 'bean' can be easily constructed using a JNDI object factory bean, and it's even easier to just use the 'jee' namespace, as below:
<beans xmlns:jee="http://www.springframework.org/schema/jee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<jee:jndi-lookup id="capKapak" jndi-name="ejb/CapKapak">
<jee:environment>
java.naming.factory.initial=com.ibm.websphere.naming.WsnInitialContextFactory
java.naming.provider.url=corbaloc:iiop:localhost:9812
</jee:environment>
</jee:jndi-lookup>
</beans>
Note: The 'java.naming.provider.url' value should refer to the WebSphere server's bootstrap address as shown above.
Then in the code (using Spring's JUnit class):
package org.gizmo.minyakangin;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:org/minyakangin/spring-context.xml"})
public class EjbTests extends AbstractJUnit4SpringContextTests {
CapKapakRemote capKapak;
@Before
public void before() {
capKapak = (CapKapakRemote)applicationContext.getBean("capKapak");
}
@Test
public void testLoginManager() throws Exception {
String brand = capKapak.getBrand();
System.out.println(brand);
}
}
Using plain Java
package org.gizmo.minyakangin;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import com.cv.ibs.user.session.LoginManagerRemote;
import com.cv.ibs.utils.SessionUser;
public class StandaloneEjbClient {
public static void main (String args[]) throws Exception {
Properties props = new Properties();
props.put("java.naming.factory.initial", "com.ibm.websphere.naming.WsnInitialContextFactory");
props.put("java.naming.provider.url", "corbaloc:iiop:localhost:9812");
InitialContext ctx = new InitialContext(props);
Object ejbBusIntf = ctx.lookup("ejb/CapKapak");
CapKapakRemote loginManager = (CapKapakRemote)PortableRemoteObject.narrow(ejbBusIntf, CapKapakRemote.class);
String brand = capKapak.getBrand();
System.out.println(brand);
}
}
No comments:
Post a Comment