Distributed subject observer pattern with ano-plass and DistributeMe


Leon - April 16, 2018 - 0 comments

Asynchronous notification aka publisher/subscriber model is a powerful pattern. Both ano-plass and DistributeMe offered a way to do it, one with subject/observer pattern, the other with the classical EventChannels. The ano-plass subject/observer pattern was limited to the current vm. Now it can become global.

We consider you are already using ano-plass and subject observer pattern and DistributeMe in your tech stack. You could use it for example to mark caches as dirty and similar things. For example, if an account object is cached, but some changes the account object all cached copies should be invalidated. Usually you would do it by firing a change event:

observationAPI.fireSubjectUpdateForUser("ACCOUNT", "ME", accountId);

Every piece of code that caches accounts would know that this account have been changed and act accordingly. But with ano-plass this only worked in one jvm. Until now.

To enable multi-jvm support via DistributeMe you have to make a small mandatory and another optional change.

The mandatory change is to enable remoting in the ano-plass configuration file called: apiconfig.json. You should add following property to it:

"supportRemotingForObservation": true

If you already have a DistributeMe Service running in your VM, this is all. In case you are not, for example if you run a web-server and only use clients, that use another services, you have to start DistributeMe eventing manually:

public class StartDistributeMeEventing {
	public static final void startDistributeMeEventing(int port) throws Exception {
		RMIRegistryUtil.findOrCreateRegistry(port);
		org.distributeme.support.eventservice.generated.EventServiceRMIBridgeServer.init();
		org.distributeme.support.eventservice.generated.EventServiceRMIBridgeServer.createServiceAndRegisterLocally();

	}
}

Finally you have to call StartDistributeMeEventing.startDistributeMeEventing somewhere. Best place is the context.xml and you can pass the port number as parameter to your web-application as well:

    public void contextInitialized(ServletContextEvent event) {
        LOGGER.info("--- " + APPLICATION_NAME + " --- APPLICATION INITIALIZATION: STARTED --- ");
		//start distributeme eventing for the api.
		String distributemePortAsString = event.getServletContext().getInitParameter("distributeMePort");
		try{
			int port = Integer.parseInt(distributemePortAsString);
			StartDistributeMeEventing.startDistributeMeEventing(port);
		}catch(Exception any){
			LOGGER.error("Can not start distributeme eventing on port "+distributemePortAsString, any);
		} //end start distributeme
//...
}

This is all. Happy subject-observation!

 

 

 

Post a Comment

Your email address will not be published.