Summary: Filters are interceptors inside the GigaSpaces space engine that enable integration with external systems and implementation of user-defined logic based on space events.
OverviewFilters are interceptors inside the GigaSpaces space engine that enable integration with external systems and/or implementation of user-defined logic based on space events. The filter business logic impacts the space responsiveness to client requests - please make sure your filter implementation will not involve heavy logic. With concurrent access into the space all clients share the same filter instance object where the space engine access the same filter object on behalf of each client. Please make sure that every non thread safe attributes you use as part of the filter implementation will be thread-safe protected. A filter is an instance of a class that implements the ISpaceFilter interface (com.j_spaces.core.filters.ISpaceFilter; seeJavadoc ).
com.j_spaces.core.filters.ISpaceFilter Interface
com.j_spaces.core.filters.FilterOperationCodesThe FilterOperationCodes class includes the following operations:
The BEFORE_NOTIFY_TRIGGER, AFTER_NOTIFY_TRIGGER, BEFORE_ALL_NOTIFY_TRIGGER, and AFTER_ALL_NOTIFY_TRIGGER FilterOperationCodes events provide:
Space Filter invocation with backup/replica Space– The space filter context is null for a filter called as a result of a replication event. You can use this as indication to know if this is originated by a client call or replication event. Just make sure your space is secured and you pass the security context credentials for the space proxy via IJSpace.setSecurityContext(). com.j_spaces.core.filters.entry.ISpaceFilterEntry InterfaceThe ISpaceFilterEntry represents an Entry instance passed to the ISpaceFilter process method implementation. The ISpaceFilterEntry class extends the IFilterEntry that extends the IGSEntry which includes methods that allow you to access the Entry values, class name and other Entry object properties. The ISpaceFilterEntry class does not include the Entry Class methods. To convert the ISpaceFilterEntry class to your Entry object, you should call the ISpaceFilterEntry.getEntry(IJSpace space) method or the ISpaceFilterEntry.getExternalEntry(IJSpace space) method. Modifying Written Entry or TemplateYou may modify the written Entry field values before the Entry is stored in the space, using the BEFORE_WRITE event, or you can modify the template field values used when performing a search for a matching Entry, using the BEFORE_READ event. This allows you to truncate an unnecessary Entry field value, or replace it with some enriched data. When modifying the template field values, you can construct a different search than the original one constructed by the client. For example, you can aggregate data from multiple Entries as part of a BEFORE_READ event, write the aggregated data into the space, and modify the template to return the aggregated data. This will return the aggregated Entry data to the client, and not the Entry that matches the original template constructed by the client. To modify the written Entry or the template, you should use the IFilterEntry.setFieldValue method. Introducing Filters to SpaceEvery space may have a set of filters. The filters are defined using the space schema XML configuration file. The space invokes the filters and space events based on the operation codes defined as part of the filter <operation-code> value: <space-config> <filters> <filter-names>myFilter</filter-names> <myFilter> <enabled>true</enabled> <security>false</security> <class>com.pak.myFilter</class> <!-- List of available filter operation codes: --> <!--The operation codes defines the operations in which this filter will be called.--> <!-- 0 - Before write;--> <!-- 1 - After write;--> <!-- 2 - Before read;--> <!-- 3 - Before take;--> <!-- 4 - Before notify;--> <!-- 5 - Before getadmin;--> <!-- 6 - Set security context;--> <!-- 7 - Before get collection;--> <!-- 8 - Before Clean space;--> <!-- 9 - Before update;--> <!-- 10 - After update;--> <!-- 11 - Before read multiple;--> <!-- 12 - After read multiple;--> <!-- 13 - Before take multiple;--> <!-- 14 - After take multiple;--> <!-- 15 - Before notify trigger operation;--> <!-- 16 - After notify trigger operation;--> <!-- 17 - Before all notify trigger operation;--> <!-- 18 - Before all notify trigger operation;--> <!-- the following operation codes can be used ONLY in non-security filters --> <!-- 51 - On init;--> <!-- 52 - Before remove;--> <!-- 53 - After remove;--> <operation-code>0, 2, 3, 4, 6, 8, 9, 11, 13</operation-code> <url>somefreetext</url> <priority>1</priority> <active-when-backup>false</active-when-backup> <shutdown-space-on-init-failure>true</shutdown-space-on-init-failure> </myFilter> </filters> </space-config>
External Space FilterYou can define an ISpaceFilter implementation including its different characteristics (priority, enabled, etc.) externally when constructing an embedded space. The FilterProvider is set as part of the Properties class (used when calling the SpaceFinder), and wraps the ISpaceFilter implementation and all of its characteristics. For example: ISpaceFilter myFilter = new MyFilter(param1, param2); FilterProvider filterProvider = new FilterProvider("myFilter", myFilter); Properties props = new Properties(); props.put(Constants.Filter.FILTER_PROVIDERS, new FilterProvider[] {filterProvider}); SpaceFinder.find("/./space", props); Space Filter ExampleThe DebugFilter class is an AFTER_WRITE filter that counts the number of Entries written per class: </space-config> <filters> <filter-names>DebugFilter</filter-names> <myFilter> <enabled>true</enabled> <security>false</security> <class>com.j_spaces.examples.filters.DebugFilter</class> <operation-code>1</operation-code> <url>somefreetext</url> <priority>1</priority> <active-when-backup>false</active-when-backup> <shutdown-space-on-init-failure>true</shutdown-space-on-init-failure> </myFilter> </filters> </space-config> package com.j_spaces.examples.filters; import com.j_spaces.core.filters.*; import com.j_spaces.core.*; import com.j_spaces.core.filters.entry.*; import java.util.Hashtable; /** * This example demonstrates the use of filters * in the GigaSpaces(TM) Platform. */ public class DebugFilter implements ISpaceFilter { private String m_FilterName; private Hashtable m_Hashtable; public void close() {} public void init(IJSpace space, String filterId, String url, int priority) { m_FilterName = filterId; } public void process(SpaceContext context, ISpaceFilterEntry[] entries, int operationCode) {} public void process(SpaceContext context, ISpaceFilterEntry entry, int operationCode) { System.out.println(m_FilterName + ": process() called."); synchronized(m_Hashtable) { Integer count = (Integer) m_Hashtable.get(entry.getClassName()); if (count == null) { count = new Integer(1); } else { count = new Integer(count.intValue() + 1); } m_Hashtable.put(entry.getClassName(), count); System.out.println("Number of instances of class: " + entry.getClassName() + " is: " + count.intValue()); } } } As with any filter, in order to run the example, you must ensure that the filter class is accessible to the GigaSpaces instance. Besides being public and with a public default (no-arg) constructor, the class must be available via the server classpath. This can be accomplished by adding it to the GigaSpaces classpath in gsInstance - GigaSpaces CLI. To test the filter, run the ping utility. The number of written entries will be written to the console: debug-filter: process() called Number of instances of class: com.j_spaces.cli.PingCommand$Message is: 1 debug-filter: process() called Number of instances of class: com.j_spaces.cli.PingCommand$Message is: 2 debug-filter: process() called Number of instances of class: com.j_spaces.cli.PingCommand$Message is: 3 debug-filter: process() called Number of instances of class: com.j_spaces.cli.PingCommand$Message is: 4 debug-filter: process() called Number of instances of class: com.j_spaces.cli.PingCommand$Message is: 5 ***Link required |
![]() |
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence |