Context ctx = new InitialContext();
MessageSender msgSender = null;
MessageSenderHome home = (MessageSenderHome) javax.rmi.PortableRemoteObject
.narrow(ctx.lookup("MessageSender"),
MessageSenderHome.class);
msgSender = home.create();
msgSender.sendMessage();
如果要在服务器外部的普通java类的main方法中调用websphere上的EJB,需要注意几个问题:MessageSender msgSender = null;
MessageSenderHome home = (MessageSenderHome) javax.rmi.PortableRemoteObject
.narrow(ctx.lookup("MessageSender"),
MessageSenderHome.class);
msgSender = home.create();
msgSender.sendMessage();
1.需要将websphere安装目录下的lib中的jar包加入classpath
2.拷贝websphere安装目录下\WebSphere\AppServer\properties\下面的implfactory.properties文件加入classpath
3.需要将websphere部署完成后生成的jar包(包含自动生成的存根类等,不然会报不能载入存根的error)加入classpath,比如WebSphere\AppServer\installedApps\dennis\JMSTest.ear\JMSTest.jar包
4.使用webphere自带的ibm的JDK,也就是安装目录下WebSphere\AppServer\java即是,在eclipse中新建一个安装的jre即可,替代策略是将此jdk下的jre的jar包加进classpath也行
一个java类调用ejb的例子,调用SLSB:
package net.rubyeye.testjms;
import javax.naming.Context;
import javax.naming.InitialContext;
import net.rubyeye.testjms.interfaces.MessageSender;
import net.rubyeye.testjms.interfaces.MessageSenderHome;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
try {
// Hashtable h = new Hashtable();
// h.put(Context.INITIAL_CONTEXT_FACTORY,
// "com.ibm.websphere.naming.WsnInitialContextFactory");
// h.put(Context.PROVIDER_URL, "corbaloc::127.0.0.1:port");
Context ctx = new InitialContext();
MessageSender msgSender = null;
MessageSenderHome home = (MessageSenderHome) javax.rmi.PortableRemoteObject
.narrow(ctx.lookup("MessageSender"),
MessageSenderHome.class);
msgSender = home.create();
msgSender.sendMessage();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
import javax.naming.Context;
import javax.naming.InitialContext;
import net.rubyeye.testjms.interfaces.MessageSender;
import net.rubyeye.testjms.interfaces.MessageSenderHome;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
try {
// Hashtable h = new Hashtable();
// h.put(Context.INITIAL_CONTEXT_FACTORY,
// "com.ibm.websphere.naming.WsnInitialContextFactory");
// h.put(Context.PROVIDER_URL, "corbaloc::127.0.0.1:port");
Context ctx = new InitialContext();
MessageSender msgSender = null;
MessageSenderHome home = (MessageSenderHome) javax.rmi.PortableRemoteObject
.narrow(ctx.lookup("MessageSender"),
MessageSenderHome.class);
msgSender = home.create();
msgSender.sendMessage();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
在同一台机器上使用上面的代码即可调用,远程调用去掉注释,并替换相应的IP和port
文章转自庄周梦蝶 ,原文发布时间5.17