在Play!中提供RMI(通过Spring)

本文探讨了在Play框架中使用RMI技术的具体方法,包括解决Play框架不支持Servlet模型的问题、创建可序列化的VO对象以进行远程传递、利用Spring封装RMI服务以及在RMI实现类中调用Model对象的变通方案。
1、SOAP/RPC风格的Webservice(通过XFire),Hessian, PHPRPC都需要Servlet模型的支持,但是Play!不支持Servlet(或许有变通的方法?),所以,还是使用RMI这种技术

2、Play!的Model类,不是纯的POJO,继承了JPASupport,作为远程对象传递时,可能会产生
10:38:16,956 ERROR ~ failed to lazily initialize a collection of role: models.User.posts, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: models.User.posts, no session or session was closed

这样的错误,因此,要定义 POJO的VO/DTO对象,做为远程传递对象

3、VO对象,因为要通过RMI传递,必须要实现Serializable接口,并且最好在类中指定serialVersionUID,象这样
private static final long serialVersionUID = 1L;
可以避免Server和Client之间同种对象版本不一样的错误

4、通过Spring封装并提供RMI的服务。需要自定义一个job,随Play!启动而启动,初始化RMI服务,象这样
//注册RMI
play.modules.spring.Spring.getBean("postServiceRmi");


5、RMI实现类,无法象Play!的Controller类一样,直接调用Model对象,Guillaume Bort的解释是
“I think that the RMI server call your code in a non managed thread.
You need to wrap your code in a play.Invoker.Invocation object. “

需要使用以下的变通方法
So it is almost the same code than Jobs. Btw you could directly use jobs:

public class RetrievePostsByUserJob extends Job<List<PostVO>> {

private Long id;

public RetrievePostsByUserJob(Long id) {
this.id = id;
}

public List<PostVO> doJobWithResult() {
... your code here
return list;
}

}

and then use the job in your RMI server:

public List<PostVO> getPostsByUserId(Long id) {
return new RetrievePostsByUserJob(id).now().get();

}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值