Summary: Space Filters are interceptors inside the GigaSpaces space engine.
OverviewSpace Filters are interceptors inside the GigaSpaces space engine that enable integration with external systems and/or implementation of user-defined logic based once space operations are executed. The Space Filter implementation may use the following interceptors. The relevant method should be annotated with the annotation listed below. The signature of these methods may include the following parameters:
Annotation List:
The space filter business logic impacts the space responsiveness to client requests - please make sure your filter implementation will not involve heavy business 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. Every non-thread safe attributes you use as part of the filter implementation will be thread-safe protected. ExampleThe following example illustrates a space Filter POJO and a Bean that performs some space operations:
The Space Filter POJO
The Space Filter POJO
package com.test; import org.openspaces.core.GigaSpace; import org.openspaces.core.context.GigaSpaceLateContext; import org.openspaces.core.space.filter.AfterAllNotifyTrigger; import org.openspaces.core.space.filter.AfterNotifyTrigger; import org.openspaces.core.space.filter.AfterReadMultiple; import org.openspaces.core.space.filter.AfterRemoveByLease; import org.openspaces.core.space.filter.AfterTakeMultiple; import org.openspaces.core.space.filter.AfterWrite; import org.openspaces.core.space.filter.BeforeAllNotifyTrigger; import org.openspaces.core.space.filter.BeforeExecute; import org.openspaces.core.space.filter.BeforeNotify; import org.openspaces.core.space.filter.BeforeNotifyTrigger; import org.openspaces.core.space.filter.BeforeRead; import org.openspaces.core.space.filter.BeforeReadMultiple; import org.openspaces.core.space.filter.BeforeTake; import org.openspaces.core.space.filter.BeforeTakeMultiple; import org.openspaces.core.space.filter.BeforeWrite; import org.openspaces.core.space.filter.OnFilterClose; import org.openspaces.core.space.filter.OnFilterInit; import org.springframework.beans.factory.InitializingBean; public class MySpaceFilter implements InitializingBean{ //late context is needed since there's a circular dependency between the filter bean and the space itself @GigaSpaceLateContext GigaSpace space; @OnFilterInit void init() { System.out.println("MySpaceFilter Space Filter initialized"); } @OnFilterClose void close() { System.out.println("Closing Space Filter"); } @BeforeWrite public void beforeWrite(Object spaceObject) { System.out.println("beforeWrite called" + spaceObject); } @AfterWrite public void afterWrite(Object echo) { System.out.println("afterWrite called" + echo); } @BeforeRead public void beforeRead(Object spaceTemplate) { System.out.println("beforeRead called" + spaceTemplate); } @AfterRemoveByLease public void afterRemoveByLease(Object spaceObject) { System.out.println("afterRemoveByLease called" + spaceObject); } @BeforeTake public void beforeTake(Object spaceTemplate) { System.out.println("beforeTake called " + spaceTemplate); } @BeforeReadMultiple public void beforeReadMultiple(Object spaceTemplate) { System.out.println("beforeReadMultiple called - Template:" + spaceTemplate); } @AfterReadMultiple public void afterReadMultiple(Object spaceObject) { System.out.println("afterReadMultiple called - Space Object:" + spaceObject); } @BeforeTakeMultiple public void beforeTakeMultiple(Object spaceTemplate) { System.out.println("beforeTakeMultiple called - Template:" + spaceTemplate); } @AfterTakeMultiple public void afterTakeMultiple(Object spaceObject) { System.out.println("afterTakeMultiple called - Space Object:" + spaceObject); } @BeforeExecute public void beforeExecute(Object task) { System.out.println("beforeExecute called " + task); } @BeforeNotify public void beforeNotify(Object spaceObject) { System.out.println("beforeNotify called " + spaceObject); } @AfterAllNotifyTrigger public void afterAllNotifyTrigger(Object spaceObject) { System.out.println("afterAllNotifyTrigger called " + spaceObject); } @BeforeAllNotifyTrigger public void beforeAllNotifyTrigger(Object spaceObject) { System.out.println("beforeAllNotifyTrigger called " + spaceObject); } @AfterNotifyTrigger public void AfterNotifyTrigger(Object spaceTemplate) { System.out.println("afterNotifyTrigger called " + spaceTemplate); } @BeforeNotifyTrigger public void beforeNotifyTrigger(Object spaceTemplate) { System.out.println("beforeNotifyTrigger called " + spaceTemplate); } @Override public void afterPropertiesSet() throws Exception { System.out.println("space "+space); } } The Bean
The Bean
package com.test; import org.openspaces.core.GigaSpace; import org.openspaces.core.context.GigaSpaceContext; import org.openspaces.events.adapter.SpaceDataEvent; import org.openspaces.events.notify.SimpleNotifyContainerConfigurer; import org.openspaces.events.notify.SimpleNotifyEventListenerContainer; import org.springframework.beans.factory.InitializingBean; import com.j_spaces.core.LeaseContext; public class MyBean implements InitializingBean{ @GigaSpaceContext GigaSpace space; @Override public void afterPropertiesSet() throws Exception { Message m = new Message(); m.setId("1"); m.setData("AAAA"); System.out.println("Calling write"); LeaseContext<Message> lc = space.write(m); line(); System.out.println("Calling read"); Message m1 = space.read(new Message()); line(); System.out.println("Calling lease cancel"); lc.cancel(); line(); System.out.println("Calling write"); space.write(m); line(); System.out.println("Calling readMultiple"); Message m1Array[] =space.readMultiple(new Message(), Integer.MAX_VALUE); line(); System.out.println("Calling take"); Message m2 = space.take(new Message()); line(); System.out.println("Calling write"); space.write(m); line(); System.out.println("Calling takeMultiple"); Message m2Array[] =space.takeMultiple(new Message(), Integer.MAX_VALUE); line(); System.out.println("Calling execute"); space.execute(new MyTask()); Thread.sleep(1000); line(); System.out.println("Calling Notify Registration"); SimpleNotifyEventListenerContainer notifyEventListenerContainer = new SimpleNotifyContainerConfigurer(space) .template(new Message()) .notifyWrite(true) .eventListenerAnnotation(new Object() { @SpaceDataEvent public void eventHappened() { System.out.println("SpaceDataEvent event called"); } }).notifyContainer(); notifyEventListenerContainer.start(); Thread.sleep(1000); line(); System.out.println("Calling write"); space.write(m); Thread.sleep(2000); } static void line() { System.out.println("-------------------------"); } } The Beans Definitions <bean id="mySpaceFilter" class="com.test.MySpaceFilter" /> <bean id="myBean" class="com.test.MyBean" /> <os-core:space id="space" url="/./space" > <os-core:annotation-adapter-filter priority="2"> <os-core:filter ref="mySpaceFilter" /> </os-core:annotation-adapter-filter> </os-core:space> <os-core:giga-space id="gigaSpace" space="space"/> <os-core:giga-space-context/> <os-core:giga-space-late-context /> The Space Class
The Space Class
package com.test; import com.gigaspaces.annotation.pojo.SpaceClass; import com.gigaspaces.annotation.pojo.SpaceId; @SpaceClass public class Message { String id; String data; public Message (){} @SpaceId public String getId() { return id; } public void setId(String id) { this.id = id; } public String getData() { return data; } public void setData(String data) { this.data = data; } public String toString() { return " Class Message - Object ID:" + id + " data:" + data; } } The Space Task
The Space Task
package com.test; import java.io.Serializable; import org.openspaces.core.executor.Task; public class MyTask implements Task<Serializable>{ @Override public Serializable execute() throws Exception { return null; } } The expected outputHere is how the expected output should look like: MySpaceFilter Space Filter initialized INFO [com.gigaspaces.core.common] - Space [space_container:space] with url [/./space?schema=default&groups=gigaspaces-7.1.0-XAPPremium-rc&state=started] started successfully space space_container:space Calling write beforeWrite called Class Message - Object ID:1 data:AAAA afterWrite called Class Message - Object ID:1 data:AAAA ------------------------- Calling read beforeRead called Class Message - Object ID:null data:null ------------------------- Calling lease cancel afterRemoveByLease called Class Message - Object ID:1 data:AAAA ------------------------- Calling write beforeWrite called Class Message - Object ID:1 data:AAAA afterWrite called Class Message - Object ID:1 data:AAAA ------------------------- Calling readMultiple beforeReadMultiple called - Template: Class Message - Object ID:null data:null afterReadMultiple called - Space Object: Class Message - Object ID:1 data:AAAA ------------------------- Calling take beforeTake called Class Message - Object ID:null data:null afterRemoveByLease called Class Message - Object ID:1 data:AAAA ------------------------- Calling write beforeWrite called Class Message - Object ID:1 data:AAAA afterWrite called Class Message - Object ID:1 data:AAAA ------------------------- Calling takeMultiple beforeTakeMultiple called Template: Class Message - Object ID:null data:null afterRemoveByLease called Class Message - Object ID:1 data:AAAA afterTakeMultiple called Space Object:Class Message - Object ID:1 data:AAAA ------------------------- Calling execute beforeExecute called com.test.MyTask@9e4585 ------------------------- Calling Notify Registration beforeNotify called Class Message - Object ID:null data:null ------------------------- Calling write beforeWrite called Class Message - Object ID:1 data:AAAA afterWrite called Class Message - Object ID:1 data:AAAA beforeAllNotifyTrigger called Class Message - Object ID:1 data:AAAA beforeNotifyTrigger called Class Message - Object ID:1 data:AAAA SpaceDataEvent event called afterNotifyTrigger called Class Message - Object ID:1 data:AAAA afterAllNotifyTrigger called Class Message - Object ID:1 data:AAAA Advanced OptionsA filter is an instance of a class that implements the ISpaceFilter interface (com.j_spaces.core.filters.ISpaceFilter; see Javadoc ).
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. For secured space, you can use this as indication to know if this operation originated by a client call or replication event. 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 Space Object 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. |
![]() |
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence |