ejb3学习笔记2(ejb3 helloworld之本地远程调用)
一。改写组件,再次导出jar包,拷贝到deploy目录下
package cn.wenkun.ejb.service;
import javax.ejb.Remote;
@Remote
public interface HelloWorldRemoteService extends HelloWorldService {
}
package cn.wenkun.ejb.service;
import javax.ejb.Stateless;
@Stateless
public class HelloWorldServiceBean implements HelloWorldService,HelloWorldRemoteService{
public String sayHello(String str) {
System.out.println("hello");
return "hello,"+str;
}
}
二。本地远程调用
原理:socket进行进程通信,对象传输用到java的序列化与反序列化
remote远程调用
ejb 生成两个代理:stub存根(客户端代理)socket skeleton(服务端代理)serversocket
package cn.service.app;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import cn.wenkun.ejb.service.HelloWorldService;
public class EjbApp {
public static void main(String[] args) {
try {
Properties prop = new Properties();
prop.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
prop.put("java.naming.provider.url", "localhost:1099");\\ejb远程调用端口
InitialContext ctx = new InitialContext(prop);
HelloWorldService hws = (HelloWorldService) ctx
.lookup("HelloWorldServiceBean/remote");
String res = hws.sayHello("tom");
System.out.println(res);
} catch (NamingException e) {
e.printStackTrace();
}
}
}