The different space components allow you to configure a space within a Spring application context (or a Processing Unit). A Space component allows you to create an IJSpace (or JavaSpace) with the most common practice of creating one based on a Space URL.
Here is an example of creating a space within a Spring XML-based configuration:
UrlSpaceConfigurer spaceConfigurer = new UrlSpaceConfigurer("/./space");
IJSpace space = spaceConfigurer.space();
// ...
// shutting down / closing the Space
spaceConfigurer.destroy();
This example creates an embedded space (IJSpace) within the Spring application context (using the /./ prefix) with the name space and a Spring bean id of the space.
URL Properties
The UrlSpaceFactoryBean allows you to set different URL properties (as defined in the Space URL) either explictly using explicit properties or using a custom Properties object. All of the current URL properties are exposed using explicit properties in order to simplify the configuration, except for clustering properties (see the Cluster Configuration section).
Here is an example a space with specific schema working in FIFO mode, using specific lookup groups.
UrlSpaceConfigurer spaceConfigurer = new UrlSpaceConfigurer("/./space").fifo(true)
.lookupGroups("test");
IJSpace space = spaceConfigurer.space();
// ...
// shutting down / closing the Space
spaceConfigurer.destroy();
General Properties
The space allows you to override specific schema element values using the Properties object, that uses an XPath-like navigation as the name value. The UrlSpaceFactoryBean allows you to set the Properties object, specifying it within the Spring configuration.
UrlSpaceConfigurer spaceConfigurer =
new UrlSpaceConfigurer("/./space").addProperty("space-config.serialization-type", "1");
IJSpace space = spaceConfigurer.space();
// ...
// shutting down / closing the Space
spaceConfigurer.destroy();
Embedded vs. Remote Space
When looking up or creating a space, OpenSpaces qualifies the state of the Space as either embedded or remote. An embedded space uses /./ as the URL "protocol", and causes the space to be created and be part of the application context (or processing unit). A remote space is one that was looked up using one of the remote protocols (Jini or RMI). The previous example showed how to look up an embedded space, while the example below shows you how to look up a remote space using the Jini protocol (looks up a space called space on all machines under the same lookup group).
UrlSpaceConfigurer spaceConfigurer = new UrlSpaceConfigurer("jini://*/*/space");
IJSpace space = spaceConfigurer.space();
// ...
// shutting down / closing the Space
spaceConfigurer.destroy();
Clustering
OpenSpaces views clustering as a deployment or runtime decision. The idea is not to configure a space with clustering information (total_members, id, etc.) within the Spring configuration, but allows the application context to be injected with this information when it is deployed (or run within the IDE).
Built on the same concept of Spring ApplicationContextAware callback interface, OpenSpaces defines ClusterInfo – an object holding the specific Processing Unit instance cluster parameters (id, total_members, backup_id, etc.), and ClusterInfoAware – allows any Spring bean to implement this interface and be injected with the cluster parameters.
The UrlSpaceFactoryBean implements this ClusterInfoAware and uses the ClusterInfo in order to automatically amend the Space URL with cluster parameters. The ClusterInfo is provided by external containers that can run a Processing Unit.
For more details on ClusterInfo, refer to the Cluster Info section.
Primary/Backup
When working in clustered mode (schema) that includes a primary/backup schema, several components within the Processing Unit need to be aware of the current space mode and any changes made to it (such as event containers). Using Spring support for application events, two events are defined within OpenSpaces: BeforeSpaceModeChangeEvent and AfterSpaceModeChangeEvent. Both are raised when a space changes its mode from primary to backup or versa, and holds the current space mode.
Custom beans that need to be aware of the space mode (for example, working directly against a cluster member, i.e. not using a clustered proxy of the space, and performing operations against the space only when it is in primary mode) can implement the Spring ApplicationListener and check for the mentioned events.
OpenSpaces also provides the space mode context loader, which can load Spring application context when it has become primary, and unload it when it moves to backup.
In embedded mode, the space factory bean registers with the space for space mode changes. The registration is performed on the actual space instance (and not a clustered proxy of it), and any events raised are translated to the equivalent OpenSpaces space mode change events. In remote mode, a single primary event is raised.
Space mode registration can be overridden and explicitly set within the space factory configuration. Here is an example of how it can be set (cannot register for notifications even though it is an embedded space):
UrlSpaceConfigurer spaceConfigurer =
new UrlSpaceConfigurer("/./space").registerForSpaceModeNotifications(false);
IJSpace space = spaceConfigurer.space();
// ...
// shutting down / closing the Space
spaceConfigurer.destroy();
Security Context
A secured space should be configured with a security context so that it can be accessed (when connecting to it remotely). Here is an example of how this can be configured:
Fore more details, refer to the [Space Security] section.
Space Filters
The UrlSpaceFactoryBean allows you to configure Space Filters. It uses the space support for a FilterProvider which is a wrapper for an ISpaceFilter implementation and its characteristics (such as priority, activeWhenBackup), allowing to provide space filters without changing the space schema.
Space Filters can only be used with embedded spaces.
ISpaceFilter
An actual implementation of the ISpaceFilter interface can be provided using the SpaceFilterProviderFactory class. Here is a very simple example of an ISpaceFilter implementation:
public class SimpleFilter implements ISpaceFilter {
public void init(IJSpace space, String filterId, String url, int priority)
throws RuntimeException {
// perform operations on init
}
public void process(SpaceContext context, ISpaceFilterEntry entry, int operationCode)
throws RuntimeException {
// process single entry filter operations
}
public void process(SpaceContext context, ISpaceFilterEntry[] entries, int operationCode)
throws RuntimeException {
// process multiple entries filter operation (such as update)
}
public void close() throws RuntimeException {
// perform operation when filter closes
}
}
The following Spring configuration registers this filter for before write (0), before read (2), and before take (3) operations:
OpenSpaces comes with delegate implementations of ISpaceFilter, allowing you to use either annotations or explicit method listings in order to use POJOs as space filters.
Here is an example of a simple POJO filter using annotations:
public class SimpleFilter {
@OnFilterInit
void init() {
}
@OnFilterClose
void close() {
}
@BeforeWrite
public void beforeWrite(Message entry) {
// ...
}
@AfterWrite
public void afterWrite(Echo echo) {
// ...
}
@BeforeRead
public void beforeRead(ISpaceFilterEntry entry) {
// ...
}
@BeforeTake
public void beforeTake(Message entry, int operationCode) {
// ...
}
}
This example (which also applies for explicit method listings, just without the annotations) demonstrates different options to mark methods as filters operation callbacks or filter lifecycle callbacks.
First, note the beforeRead(ISpaceFilterEntry entry) method (the method name can be any name of your choice). The method accepts the same ISpaceFilterEntry that the ISpaceFilter process method uses (which is usually used for extracting the actual template or Entry). With the beforeWrite(Message entry) method, the delegate automatically detects that the first parameter is not an ISpaceFilterEntry and uses it to extract the actual Entry, which is used to invoke the method with (in our case Message). When using Entry-type classes in the filter callback, other types that are not assignable to the Entry parameter type do not cause the filter method callback to be invoked (in our case, beforeWrite is not invoked for the echo object).
When either annotations or explicit method listings are used, only a single method per operation can be defined.
The delegate filter shown above can be configured in Spring using the following XML:
The following Spring configuration XML shows how the filter can be configured using explicit method listings (in this case, annotations are not required):
Accessing a space within a space filter can cause a cycle construction exception, since the space can not be injected to the filter (because the space was not constructed yet). There are options to solve this with pure Spring, but OpenSpaces provides a simpler option by using the GigaSpacesLateContext annotation.
Space Replication Filters
The UrlSpaceFactoryBean allows you to configure Space Replication Filters. It uses the space support for a ReplicationFilterProvider which is a wrapper for an IReplicationFilter implementation and its characteristics (such as activeWhenBackup), allowing you to provide space replication filters without changing the space schema.
Space replication filters can only be used with embedded spaces.
If we use a simple implementation of IReplicationFilter:
public class SimpleReplicationFilter implements IReplicationFilter {
public void init(IJSpace space, String paramUrl, ReplicationPolicy replicationPolicy) {
// init logic here
}
public void process(int direction, IReplicationFilterEntry replicationFilterEntry, String remoteSpaceMemberName) {
// process logic here
}
public void close() {
// close logic here
}
}
it can be injected using the following configuration:
When constructing a space, it is possible to provide an External Data Source using Spring-based configuration (instead of using the space schema). Here is an example of how it can be defined:
The above example uses Spring built-in support for configuring both a custom JDBC DataSource and a Hibernate SessionFactory to define and use GigaSpaces built-in HibernateDataSource. The GigaSpaces data source is then injected into the space construction (note the specific schema change), and causes the space to use it.
This configuration can also be used with the GigaSpaces Mirror Service deployed as a Processing Unit.
GigaSpaces 6.0 Documentation Contents (Current Page in Bold)