|
Summary: Examples of the JavaSpaces Spring Template and JDBC Spring Template, two ways to access the Data Grid via Spring.
OverviewThere are two ways to access the GigaSpaces Data Grid through the Spring framework: using the JavaSpaces Spring template or the JDBC Spring template. This section explains each option and provides code samples for both templates. JavaSpaces TemplateThe JavaSpaces specification allows you to build distributed applications via simple API. Sharing and exchanging data, data collaboration, data caching and messaging can be implemented through the write, read, take and notify methods.
JavaSpaces supports distributed transactions, leasing, notifications, matching (Query), and batch operations. GigaSpaces enhancements include: SQL Query support, Regular Expression Query support, Local Transaction manager support, FIFO notifications and take support, advanced notifications, continuous query, clustering, persistency, atomic update, partial update and more. The Spring JavaSpace abstraction allows you to use JavaSpaces while abstracting the generic facilities through Spring standards. This includes POJO, Transaction, Remoting and more. Space-Based Operations - Write, Read, Take and Notify Using POJOs
The following code sample demonstrates JavaSpaces write, read, take and notify operations using Spring: Getting the Spring Application Context configuration file: ApplicationContext context = new ClassPathXmlApplicationContext("/config/gigaspaces.xml"); GigaSpacesTemplate template = (GigaSpacesTemplate) context.getBean("gigaspacesTemplate"); The following clears all Entries from the space: template.clear(null);
Creating the Employee object: Employee employee = new Employee("last name", new Integer(32)); employee.setFirstName("first name"); Writing the object to the space: template.write(employee, Lease.FOREVER); Creating the template object to be used to query the space: Employee templatePojo = new Employee(); Employee employeeReadResult = (Employee) template.read(templatePojo, Long.MAX_VALUE); Calling the take operation to get a copy of the object from the space, and removing it in one atomic operation: Employee employeeTakeResult = (Employee) template.take(templatePojo, Long.MAX_VALUE);
The following will allow the application to receive notifications based on the passed templatePojo for all space operations conducted with the Employee or Person objects: template.addNotifyDelegatorListener(new Listener(),templatePojo ,null ,false /* FIFO mode */,Lease.FOREVER /* registration timeout */, NotifyModifiers.NOTIFY_ALL /* Register for all event types*/); The Listener class should implement the RemoteEventListener interface that includes only the notify method: public void notify(RemoteEvent theEvent) throws UnknownEventException, RemoteException { // we can obtain the POJO and the operation type that triggered the event from the RemoteEvent try { EntryArrivedRemoteEvent arrivedRemoteEvent = (EntryArrivedRemoteEvent) theEvent; ExternalEntry msg = (ExternalEntry) arrivedRemoteEvent.getEntry(true); int notifyType = arrivedRemoteEvent.getNotifyType(); } catch (UnusableEntryException e) { e.printStackTrace(); } } JDBC TemplateThe following application code uses the standard Spring JdbcTemplate to create a table, insert, delete and query data from the GigaSpaces Data Grid. public class HelloJdbc { public static void main(String[] args) { { System.out.println("nWelcome to GigaSpaces Spring JDBC HelloWorld example."); System.out.println("This example uses Spring JDBCTemplate to write, read and "+ "delete entries to space...n" ); ApplicationContext context = new ClassPathXmlApplicationContext("jdbc_gigaspaces.xml"); JdbcTemplate template = (JdbcTemplate) context.getBean("jdbcTemplate"); /* SQL CREATE TABLE statement * */ String createSQL = "CREATE TABLE Person(FirstName varchar2 INDEX, " + "LastName varchar2)"; System.out.println("Create table..."); try { template.execute( createSQL ); System.out.println("Create table... Done!"); } catch (Exception e) { System.out.println("nTable may exist already... "); System.out.println("Restart or clean (space-browser) space !"); } /* SQL INSERT statement * */ int maxRows = 10; String insertSQL = "INSERT INTO Person VALUES(?,?)"; System.out.println("Insert into table..."); for (int i = 1; i < maxRows; i++) { Object[] params = new Object[] {"FirstName" + i,"LastName" + i}; template.update(insertSQL, params); System.out.println("Insert into table... Done!"); } /* SQL DELETE statement * */ String deleteSQL="DELETE FROM Person WHERE FirstName='FirstName3'"; System.out.println("Delete from table..."); template.execute( deleteSQL ); System.out.print("Delete from table...Done!"); /* SQL SELECT statement * */ String selectSQL="SELECT * FROM Person ORDER BY Person.FirstName"; System.out.println("Select from table..."); template.query( selectSQL, new RowCallbackHandler() { public void processRow(ResultSet rs) throws SQLException { System.out.println("FirstName : " + rs.getString("FirstName")); System.out.println("LastName : "+rs.getString("LastName")); } }); System.out.println("Select from table... Done!"); } } } Application Context XML - jdbc_gigaspaces.xmlThe following file includes the properties to inject into org.springframework.jdbc.datasource.SingleConnectionDataSource and org.springframework.jdbc.core.JdbcTemplate: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- Declaration of GigaSpace jdbc driver--> <bean id="gigaspaceDataSource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource" destroy-method="destroy" singleton="false"> <property name="driverClassName" value="com.j_spaces.jdbc.driver.GDriver" /> <property name="url" value="jdbc:gigaspaces:url:rmi://localhost:10098/./helloJDBCTemplate" /> <property name="username" value="" /> <property name="password" value="" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref bean="gigaspaceDataSource" /> </property> </bean> </beans> |
Wiki Content Tree
Your Feedback Needed!
We need your help to improve this wiki site. If you have any suggestions or corrections, write to us at techw@gigaspaces.com. Please provide a link to the wiki page you are referring to.
Add Comment