Skip to content
stemey edited this page May 29, 2012 · 7 revisions

Entity observation makes it possible to observe attribute changes. These changes are based on a comparison. The entity observed can be one single instance or it can be the instance returned by an EntityHandle. The observation is not based on AOP. Rather one has to programmtically trigger the check of the entity's state by calling EntityObeserver.check().

	// define a comparison
	EntityType<EntityA> entityType = entityTypeRepository.getEntityType(EntityA.class);
	ComparisonBuilder comparisonBuilder = comparisonBuilderFactory.create(entityType);
	comparisonBuilder.include("firstName");
	Comparison comparison = comparisonBuilder.create();


	EntityA entityA = new EntityA();
	// create an entityObserver based on the comparison
	EntityObserver entityObserver = entityObserverFactory.create(comparison);
	// observer object entityA
	entityObserver.setEntity(entityA);
	// creta listener
	final AttributeListener listener = new AttributeListener() {
		void onEvent(Difference difference) {
			//
		}
	});
	// register the listener
	WatchHandle watch = entityObserver.watch("firstName", listener);

	entityA.setFirstName("Hans");
	// creta first snapshot
	entityObserver.check();
	entityA.setFirstName("Willi");
	// creta second snapshot and fire AttributeChangeEvent for attribute "firstName".
	entityObserver.check();

###Implementation

The implementation uses the Comparison and Snapshotting utilities. Every call to check creates a new snapshot. The snapshot is comparedto the previous snapshot. The deifferences are then sentto interested listeners.

Why is it not implemented via AOP? AOP based on proxies is difficult to setup because the object creation process needs to be intercepted to actually wrap the instances with proxies. Built-time AOP via AspectJ would be great but is not available everywhere.

###Configuration

  • classpath:/meta/utility/observer.xml : classpath scanning
  • classpath:/meta/utility/observer-example.xml : defines component "meta-utility-entityObserverFactory"/>

taken from /meta/utility/observer-example.xml:

<bean id="meta-utility-entityObserverFactory"
	class="org.atemsource.atem.utility.observer.EntityObserverFactory">
	<property name="comparisonBuilderFactory" ref="meta-utility-comparisonBuilderFactory"/>
	<property name="snapshotBuilderFactory" ref="meta-utility-snapshotBuilderFactory"/>
</bean>

Clone this wiki locally