Data Grid Beans (in five minutes)
Say you want to share some information across a collection of Spring Applications, for example, providing a simple shared counter to generate unique numbers for the said applications. While there are many approaches to achieve this, most requiring use of remoting, messaging or distributed transactions and all requiring complex/time-consuming deployment strategies to ensure availability, clustering or high-performance, using a Data Grid Bean with Coherence Data Grid for Spring is a simpler and more natural solution.
 | What are Data Grid Beans?
(simple definition) Data Grid Bean: A standard Java Bean that has its' life-time scoped and managed by a Data Grid. Access and usage of a Data Grid Bean in frameworks such as Spring is no different from using normal Java Beans, including the ability to register update listeners (observers) for change notification. |
Here's how.
Step One - Create your Bean class
Nothing complicated here. We simply create a Java class that encapsulates the concept of a Counter.
public class Counter {
private long nextInSequence;
public Counter() {
nextInSequence = 0;
}
public Counter(long nextInSequence) {
this.nextInSequence = nextInSequence;
}
public long next() {
return nextInSequence++;
}
}
Step Two - Make your Bean Serializable
Again, nothing complicated here. We simply implement the java.io.Serializable interface and provide a serialVersionUID. Nothing else is really required.
import java.io.Serializable;
public class Counter implements Serializable {
private static final long serialVersionUID = -3273058899102762451L;
private long nextInSequence;
public Counter() {
nextInSequence = 0;
}
public Counter(long nextInSequence) {
this.nextInSequence = nextInSequence;
}
public long next() {
return nextInSequence++;
}
}
 | Why Make It Serializable?
As Data Grid Beans may exist outside the JVM process in which they are created (ie: in another node in a Data Grid) or may be copied to other locations (for recovery or performance optimizations), they must be java.io.Serializable to support being moved across process boundaries or networks. |
Step Three - Data Grid enable your Spring Application
Again, a relatively straight forward process. We simply modify our Spring Application xml configuration to include the datagrid namespace and declare that the Spring Application is a <datagrid:member/>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http:
xmlns:xsi="http:
xmlns:aop="http:
xmlns:datagrid="http:
xsi:schemaLocation="http: http: http:
<datagrid:member />
</beans>
To introduce the datagrid namespace you must:
- Add the xmlns:datagrid="http://schemas.tangosol.com/schema/datagrid-for-spring" declaration to the beans element.
 | Why <datagrid:member/> ?
Declaring an application to be a <datagrid:member/> in your configuration file allows your application to access Data Grid Beans and other Data Grid features. This declaration makes no changes to your application context. It simply registers an (internal) Bean to handle Application and Data Grid lifecycle events (and provide access to datagrid scoped beans). |
For more information on Data Grid Membership, you might like to read about Data Grid Members.
Step Four - Define your Bean with the datagrid Scope
Once again, very simple. In our Spring Application xml configuration we define our Counter bean (as we would normally in Spring), but with the datagrid scope instead of singleton.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http:
xmlns:xsi="http:
xmlns:aop="http:
xmlns:datagrid="http:
xsi:schemaLocation="http: http: http:
<datagrid:member />
<bean id="ourCounter" class="Counter" scope="datagrid" />
</beans>
Step Five - Use your Bean in Spring
Using Data Grid scoped beans in Spring is exactly the same as using standard beans. Hence to use our counter we would do something like the following.
Counter counter = (Counter)applicationContext.getBean("ourCounter");
long number = counter.next();
Step Six - Deploying your Spring Applications
Deploying and executing a Spring Application that uses Data Grid Beans is as easy as including the necessary .jars in our deployment. Simply include the standard Coherence jars coherence.jar and tangosol.jar together with the coherence-datagrid-for-spring.jar in the classpath and we're ready to cluster and execute our Spring Applications using Data Grid Beans on a Data Grid.
Should we deploy several or hundreds / thousands of Spring applications using the above configuration, each will have access to the shared instance of ourCounter.
For customized configuration, configuring Coherence infrastructure is all that is typically required.
For more details on the semantics of Data Grid Beans in a Data Grid (or Cluster of Applications), you might like to read about Data Grid Beans.
Next?
Getting Started with Coherence Data Grid for Spring