开发者

How to preserve object identity across different JVMs

开发者 https://www.devze.com 2023-01-02 10:38 出处:网络
To be specific let me illustrate the question with Spring http-remoting example. Suppose we have such implementation of a simple interface:

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 is itself a simple POJO.

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.processSearc开发者_C百科h(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.

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.

  • object identity is compared with ==
  • logical equality is compared with .equals(..)

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 clustering allows sharing objects between VMs, but that doesn't fit your case.)


IMHO attempting to preserve object identity equality across VMs is a losing proposition. To the best of my knowledge the language specification does not require a VM to support that, so you would be limited in where you can pull off if you truly want to be portable.

May I ask why you don't just use some unique ID that you supply yourself? Java GUIDs, while expensive, are serializable.


I did this once, but I'm not quite sure if this is a right approach:

Every user had a username, session id, roles, and a login date attached to a user object. Every time I logged into a VM the system would load a User object into memory; I would also return the user object to the application.

If I needed to execute an action within the application server, then I would send the user object as an argument. If the VM had the User loaded with the same session ID then it would use the object stored in the VM to know the assigned roles. Otherwise, the application would then be capable of changing the roles in the user and it wouldn't be secure.

If the application had to change the application server, then it sends the user object to the new server and the new server wouldn't be able to find the user within its records.

HERE IS THE SECRET: The session ID is created hashing the username, the login date and a secret password shared among all of the servers.

Once the new server finds that the session ID is coherent, then it would load the roles from the database as a reliable source of information.

Sorry if I couldn't write this before, but hope it helps for someone.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号