`
neo
  • 浏览: 260408 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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

阅读更多
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();

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics