megloff
megloff

Reputation: 1566

Java - Framework to detect if Object has changed / dirty detection mechanism

Dear all I am currently implementing a java client which is using quite heavily third party webservices. In order to gain performance I just like to call webservices only in case the objects on my client side has been modified (become dirty).

Instead of writing an own kind of framework which is able to detect if an object is dirty, exists there any open / generic framework which can be reused and is not bind to its core product (e.g. hibernate)?

Upvotes: 0

Views: 1934

Answers (2)

megloff
megloff

Reputation: 1566

To give others a starting point how this can look like.

    TestBean bean1 = new TestBean("AAA");
    TestBean bean2 = new TestBean("BBB");

    log.info("serialize...");
    ByteArrayOutputStream str1 = new ByteArrayOutputStream();
    ObjectOutputStream oos1 = new ObjectOutputStream(str1);
    oos1.writeObject(bean1);
    byte[] serialized1 = str1.toByteArray();
    oos1.close();

    ByteArrayOutputStream str2 = new ByteArrayOutputStream();
    ObjectOutputStream oos2 = new ObjectOutputStream(str2);
    oos2.writeObject(bean2);
    byte[] serialized2 = str2.toByteArray();
    oos2.close();

    log.info("compare");
    boolean same = Arrays.equals(serialized1, serialized2);

The advantage of this approach

  • you serialize the whole object structure so it supports complex hierarchies out of the box and you do not have to care about cycles (parent / childs)
  • you can use "transient" key word on members which you like to exclude from the serialization / comparison
  • you have not to add some boiler plate code to your bean classes (apart that the class needs to implement the Serializable interface)

disadvantages

  • it is not fine grained, so you don't get out of the box which fields are dirty. There are sophisticated approaches by using an own serialization format
  • I did not yet had a thought about performance but I could imagine that other approaches comes also not for free, so here you need to test and tune the approach your self.

Upvotes: 0

cruftex
cruftex

Reputation: 5723

I presume with object you don't mean a single scalar value but a bean.

Technically you can do all sorts of fancy stuff to detect the bean mutation, for example changing the byte code and add some code whenever the object field is updated.

Another option is to keep a copy of the old bean instance and compare it. So actually the problem reduces to comparing two beans, which was asked here: how to generically compare entire java beans? Probably you find more, there are a lot of frameworks dealing with beans in general.

However, since you call webservices, you must have a mechanism to serialize your objects. You could use the serialized form of the old and new object to compare for identity before sending the update request.

Change notification: I don't recommend attaching change listeners to every bean. This might change your general performance and introduce memory leaks. There is also a transaction problem: If more then one bean property is updated, when is the update of a bean completed? So you need an explicit call after the mutation anyways.

Note to myself and other caching guys: Actually this is the use case to provide a method like Cache.putIfNotEquals(key, value) on a cache, which is not much efford. The cache stores the previous value already and it does only call the cache writer (in a write through setup) if the value changed.

Upvotes: 2

Related Questions