如何在不同的VM之间保留对象身份 [英] How to preserve object identity across different VMs

查看:66
本文介绍了如何在不同的VM之间保留对象身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体来说,让我用Spring http-remoting示例来说明这个问题.

To be specific let me illustrate the question with Spring http-remoting example.

假设我们已经实现了一个简单的界面:

Suppose we have such implementation of a simple interface:

public SearchServiceImpl implements SearchService {
    public SearchJdo processSearch(SearchJdo search) {
        search.name = "a funky name";
        return search;
    }
}

SearchJdo本身就是一个简单的POJO.

SearchJdo is itself a simple POJO.

现在,当我们通过 http-remoting (Spring调用远程对象的机制,类似于使用序列化的EJB)从客户端调用该方法时,我们将得到:

Now when we call the method from a client through http-remoting (Spring's mechanism of calling remote objects much like EJB that uses serialization) we'll get:

public class HTTPClient {
    public static void main(final String[] arguments) {
        final ApplicationContext context = new ClassPathXmlApplicationContext(
            "spring-http-client-config.xml");
        final SearchService searchService =
            (SearchService) context.getBean("searchService");

        SearchJdo search = new SearchJdo();
        search.name = "myName"; 
        // this method actually returns the same object it gets as an argument
        SearchJdo search2 = searchService.processSearch(search);
        System.out.println(search == search2); // prints "false"
    }
}

问题是搜索对象由于序列化而不同,尽管从逻辑上看它们是相同的.

The problem is that the search objects are different because of serializaton although from logical prospective they are the same.

问题是,是否存在某种允许跨VM支持或模拟对象身份的技术.

The question is whether there are some technique that allows to support or emulate object identity across VMs.

推荐答案

您说的是-对象身份不同于逻辑相等性.

You said it - object identity is different from logical equality.

  • 对象标识与==
  • 进行比较 将
  • 逻辑相等性与.equals(..)
  • 进行比较
  • object identity is compared with ==
  • logical equality is compared with .equals(..)

因此重写equals()方法,一切都会好起来的.切记也要基于相同的字段覆盖hashCode().使用您的IDE为您生成这两种方法.

So override the equals() method and all will be fine. Remember to override hashCode() based on the same field(s) as well. Use your IDE to generate these 2 methods for you.

( Teracotta VM群集允许在VM之间共享对象,但这不适合您的情况.)

(Teracotta VM clustering allows sharing objects between VMs, but that doesn't fit your case.)

这篇关于如何在不同的VM之间保留对象身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆