A remote object is obtained in one of the following ways:
An object pushed to a remote node as part of replication. This mechanism is described in the Chapter 7, High availability.
A query pulls remote objects to the local node. This mechanism is described in the section called “Queries”.
An object returned from a method invocation on a remote object.
An external directory service. This approach is not discussed in this document.
When a remote object is obtained on a node, all fields for the object are read to the node.
For non-replicated distributed objects, the field data is refreshed when:
a method is executed on a remote object that modifies the object. All field data is read to the node when the method completes. If a method does not modify the object, the field data is not refreshed.
a state conflict is detected (see the section called “State conflict”).
the object is explicitly flushed (see the section called “Flushing objects”). The field data is refreshed the next time the object is accessed.
Replicated objects are automatically refreshed as they are modified on their active node.
![]() | |
Replicated objects are only refreshed if the local node is a replica in the partition definition. If the local node is not in the partition definition, this object behaves as a non-replicated distributed object. |
The default caching policy for non-replicated distributed objects is cache never, which means that objects are only cached on the local node for the duration of the transaction. The object is flushed when the transaction commits. See the section called “Flushing objects” for more details.
The Example 6.1, “Accessing remote objects” demonstrates using a distributed query to access remote objects. The snippet performs the following steps:
Creates a named cache to contain the objects being created.
Creates an object on each node in a cluster.
Uses a distributed cardinality query to wait for all nodes to create objects.
Executes a distributed extent query to cache all objects in the local node.
Displays the contents of the local extent, showing all of the cached objects.
Waits for an explicit shutdown.
Example 6.1. Accessing remote objects
// $Revision: 1.1.2.6 $ package com.kabira.snippets.distributedcomputing; import com.kabira.platform.CacheManager; import com.kabira.platform.LockMode; import com.kabira.platform.ManagedObject; import com.kabira.platform.QueryScope; import com.kabira.platform.Transaction; import com.kabira.platform.annotation.Managed; import com.kabira.platform.property.Status; /** * This snippet demonstrates how to access distributed objects * <p> * <h2> Target Nodes</h2> * <ul> * <li> <b>domainname</b> = Development * </ul> */ public class RemoteReferences { @Managed private static class Distributed { Distributed() { m_message = "Created on " + _NODE_NAME; } String getMessage() { return m_message; } String getCreatedOnNode() { return _NODE_NAME; } private final String m_message; private final static String _NODE_NAME = System.getProperty(Status.NODE_NAME); } /** * Main entry point * * @param args Not used * @throws InterruptedException Sleep interrupted */ public static void main(String[] args) throws InterruptedException { // // Initialize the snippet // initialize(); // // Wait for all nodes to create objects // waitForInitialization(); // // Cache all distributed objects on local node // cacheDistributedObjects(); // // Display distributed objects // displayDistributedObjects(); // // Wait for shutdown // waitForStop(); } // // Display the distributed objects // private static void displayDistributedObjects() { new Transaction("Display Distributed Objects") { @Override protected void run() { String label; // // Access objects using local extent // This returns only the objects located on the loca node // System.out.println("Found in local extent on " + _NODE_NAME); for (Distributed d : ManagedObject.extent(Distributed.class)) { System.out.println("\t" + d.getMessage()); } } }.execute(); } // // Initialize the snippet // private static void initialize() { new Transaction("Initialization") { @Override protected void run() throws Rollback { // // Set up a cache always distributed object cache on all // nodes. This overrides the default distributed object // behavior which flushes distributed objects after they // are accessed. // CacheManager.Cache cache = CacheManager.getOrCreateCache("Remote References"); cache.setSizePercent(100); cache.addClass(Distributed.class); cache.enable(); new Distributed(); } }.execute(); } // // Wait for objects to be created on all nodes // private static void waitForInitialization() throws InterruptedException { while (m_count != 3) { Thread.sleep(5000); new Transaction("Wait for Creation") { @Override protected void run() { // // Execute cluster-wide cardinality to determine number // of objects created // m_count = ManagedObject.cardinality( Distributed.class, QueryScope.QUERY_CLUSTER); } }.execute(); } } // // Perform a cluster-wide extent query to cache objects on local node // private static void cacheDistributedObjects() throws InterruptedException { new Transaction("Cache Objects") { @Override protected void run() { // // Execute cluster wide extent query to pull all // objects to the local node // for (Distributed d : ManagedObject.extent( Distributed.class, QueryScope.QUERY_CLUSTER, LockMode.READLOCK)) { System.out.println("Cached object from " + d.getCreatedOnNode()); } } }.execute(); } // // Wait for termination // private static void waitForStop() throws InterruptedException { System.out.println("Waiting for stop..."); while (true) { Thread.sleep(1000); } } private final static String _NODE_NAME = System.getProperty(Status.NODE_NAME); private static int m_count = 0; }
When this snippet is run it outputs (annotation added):
# # Cache all objects on node C by doing a distributed extent query # [C] Cached object from C [C] Cached object from A [C] Cached object from B # # Shows all cached objects in local extent on node C # [C] Found in local extent on C [C] Created on C [C] Created on A [C] Created on B [C] Waiting for stop... # # Cache all objects on node B by doing a distributed extent query # [B] Cached object from C [B] Cached object from A [B] Cached object from B # # Shows all cached objects in local extent on node B # [B] Found in local extent on B [B] Created on C [B] Created on A [B] Created on B [B] Waiting for stop... # # Cache all objects on node A by doing a distributed extent query # [A] Cached object from C [A] Cached object from A [A] Cached object from B [A] Found in local extent on A # # Shows all cached objects in local extent on node A # [A] Created on C [A] Created on A [A] Created on B [A] Waiting for stop...