Wednesday, 27 July 2016

Pivotal Gemfire with Example

Standard
Image result for pivotal gemfire

Pivotal GemFire is a memory-optimized, distributed data store that is designed for applications that have demanding scalability and availability requirements. With GemFire XD you can manage data entirely using in-memory tables, or you can persist very large tables to local disk store files or to a Hadoop Distributed File System (HDFS) for big data deployments. In this initial release, GemFire XD provides a low-latency SQL interface to in-memory table data, while seamlessly integrating data that is persisted in HDFS. A single GemFire XD distributed system can be easily scaled out using commodity hardware to support thousands of concurrent clients, and you can also replicate data between distinct GemFire XD clusters over a WAN interface. GemFire XD also provides easy access to persisted HDFS data.

GemFire XD offers extremely high throughput, predictable latency, dynamic and linear scalability, and continuous availability of data. GemFire XD is implemented entirely in Java, and it can be embedded directly within a Java application.You can also deploy GemFire XD members as standalone servers that participate in a cluster(wiki).

Case study :


White Paper :




Download



Useful commands & links :

start locator --name=locator1 --port=10334 --initial-heap=256m --max-heap=256m

start server --name=server1 --server-port=40411
start server --name=server2 --server-port=40412
create region --name=regionA --type=REPLICATE_PERSISTENT

connect --locator=locator1
connect --locator=localhost[10334]

--Start locator 1 using  gfsh on node1
--to get mcast ip addres command for windows (netsh interface ip show joins)take scope 0 and for unix (netstat -g) take bond0

start locator --name=locator1 --bind-address=node1 --port=62001 --locators=node1[62001],node2[62001] --log-level=info --mcast-address=239.192.81.1 --mcast-port=10334 --dir=/export/vol01/enterprise-docs/gemfireWorkspace/locator1 --enable-cluster-configuration=true --load-cluster-configuration-from-dir=true --cluster-config-dir=/export/vol01/enterprise-docs/gemfireWorkspace/clusterWorkspace

--Start locator 2 using  gfsh on node2

start locator --name=locator2 --bind-address=node2 --port=62001 --locators=node1[62001],node2[62001] --log-level=info --mcast-address=239.192.81.1 --mcast-port=10334 --dir=/export/vol01/enterprise-docs/gemfireWorkspace/locator2 --enable-cluster-configuration=true --load-cluster-configuration-from-dir=true --cluster-config-dir=/export/vol01/enterprise-docs/gemfireWorkspace/clusterWorkspace


list members
list regions

query --query='select * from /regionA'

deploy --jar=C:\devprogs\Pivotal_GemFire_800_b48398_Windows\bin\locator1\cluster_config\cluster\employee_dto.jar

list deployed

undeploy --jar=employee_dto.jar

create region --name=dtoTestRegion --type=REPLICATE --key-constraint=com.gem.fire.example.Employee --value-constraint=com.gem.fire.example.Address

describe region --name=regionA

remove --region=exampleRegion --all

stop server --name=server1

stop locator --name=locator1

disconnect

shutdown

start pulse
admin/admin

--command to modify cluster.xml - it works only on unix
gfsh>export cluster-configuration --zip-file-name=/export/vol01/enterprise-docs/gemfireWorkspace/clusterWorkspace/cluster_config/cluster/cluster.xml.zip
for import need to follow below steps as first shutdown servers, only locators need to up after import start the servers, 
zip file contains cluster folder with cluster.xml with cluster.properties that modified to import

gfsh> shutdown
gfsh>import cluster-configuration --zip-file-name=/tmp/cluster.xml_1.zip
gfsh> start server --name=server1 --server-port=40411
gfsh> start server --name=server2 --server-port=40412
---------


http://gemfire.docs.pivotal.io/latest/getting_started/15_minute_quickstart_gfsh.html
https://pubs.vmware.com/vfabric5/index.jsp?topic=/com.vmware.vfabric.gemfire.6.6/developing/region_options/region_types.html
http://pubs.vmware.com/vfabric53/index.jsp?topic=/com.vmware.vfabric.gemfire.7.0/getting_started/gemfire_tutorial/running_tutorial.html

https://github.com/Pivotal-Field-Engineering/BigDataRoadshow-labs/tree/master/labs/gemfire
https://pubs.vmware.com/vfabric5/index.jsp?topic=/com.vmware.vfabric.gemfire.6.6/deploying/gfsh/commands/put.html
https://pubs.vmware.com/vfabric5/index.jsp?topic=/com.vmware.vfabric.gemfire.6.6/getting_started/querying_quick_reference.html
http://gemfire.docs.pivotal.io/latest/tools_modules/gfsh/command-pages/put.html

http://gemfire702.docs.pivotal.io/7.0.2/userguide/reference/topics/region_shortcuts_reference.html
https://pubs.vmware.com/vfabric5/index.jsp?topic=/com.vmware.vfabric.gemfire.6.6/developing/querying_basics/query_basics.html
http://gemfire.docs.pivotal.io/latest/geode_rest/chapter_overview.html

http://stackoverflow.com/questions/33692461/pivotal-gemfire-8-1-put-data-not-saving-data-into-gemfire/33753579#33753579

http://theblasfrompas.blogspot.in/2012/11/spring-data-repository-for-gemfire-7.html
http://theblasfrompas.blogspot.in/2014/09/rest-with-pivotal-gemfire-80.html

http://docs.spring.io/spring-data-gemfire/docs/current/reference/html/

https://support.pivotal.io/hc/communities/public/questions/204438137-How-to-create-subregion-from-gfsh-in-the-cluster-configuration

https://github.com/olivergierke/spring-gemfire/blob/master/src/asciidoc/reference/region.adoc

https://maven-repository.com/artifact/com.gemstone.gemfire/gemfire/8.2.0

http://community.gemstone.com/display/gemfire/Querying

-----spring url

http://howtodoinjava.com/2015/01/23/how-to-use-spring-component-repository-service-and-controller-annotations/
http://stackoverflow.com/questions/7414794/difference-between-contextannotation-config-vs-contextcomponent-scan

POC :

With Spring Data :




pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>WebserviceSpringDataGemFire</groupId>
  <artifactId>WebserviceSpringDataGemFire</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>WebserviceSpringDataGemFire</name>
 <properties>
        <spring.version>4.1.7.RELEASE</spring.version>
        <jackson.version>1.9.10</jackson.version>
        <jdk.version>1.7</jdk.version>
    </properties>

    <dependencies>

        <!-- Spring 3 dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>   

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>   

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>   

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
        </dependency>       
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
            <version>1.5.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons-core</artifactId>
            <version>1.0.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-gemfire</artifactId>
            <version>1.2.0.RELEASE</version>
        </dependency>

        <dependency>
          <groupId>com.gemstone.gemfire</groupId>
          <artifactId>gemfire</artifactId>
          <version>8.0.0</version>
       </dependency>    

        <dependency>
            <!-- Required for Log4J XML configuration files -->
            <groupId>log4j</groupId>
            <artifactId>apache-log4j-extras</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>commons-chain</groupId>
            <artifactId>commons-chain</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>2.2</version>
        </dependency>

        <!-- Jackson JSON Mapper -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>${jackson.version}</version>
        </dependency> 
    </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>${jdk.version}</source>
          <target>${jdk.version}</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>WebserviceSpringDataGemFire</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>
mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <mvc:annotation-driven />
    <context:annotation-config />
    <context:component-scan base-package="spring.gemfire.repository.deptemp"/>

</beans>
gemfirebean1.xml
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:context="http://www.springframework.org/schema/context"
    xmlns:gfe="http://www.springframework.org/schema/gemfire"  
    xmlns:gfe-data="http://www.springframework.org/schema/data/gemfire"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd 
        http://www.springframework.org/schema/gemfire 
        http://www.springframework.org/schema/gemfire/spring-gemfire-1.2.xsd  
        http://www.springframework.org/schema/data/gemfire 
        http://www.springframework.org/schema/data/gemfire/spring-data-gemfire-1.2.xsd">

    <context:component-scan base-package="spring.gemfire.repository.deptemp"/>

    <gfe:client-cache id="client-cache" cache-xml-location="classpath:gemfire-client.xml" pool-name="gfPool"/>

    <gfe:pool id="gfPool" max-connections="10">  
        <gfe:locator host="localhost" port="10334"/>        
    </gfe:pool>   



    <gfe:lookup-region id="departments" name="departments" cache-ref="client-cache"/>  
    <gfe:lookup-region id="employees" name="employees" cache-ref="client-cache"/>  
    <gfe-data:repositories base-package="spring.gemfire.repository.deptemp.repos"  />  

</beans> 
gemfire-client.xml
<!DOCTYPE client-cache PUBLIC
    "-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
    "http://www.gemstone.com/dtd/cache6_5.dtd">
<client-cache>      
    <!-- No cache storage in the client region because of the PROXY client region shortcut setting. -->  
<!--    <pool name="cacheServerPool">
        <locator host="GCOTDVMR770036.nam.nsroot.net" port="10334" />
    </pool> -->
    <region name="departments" refid="PROXY">  
    <!--   <region-attributes refid="PROXY" pool-name="gfPool" data-policy="replicate"/> -->  
      <region-attributes refid="PROXY" data-policy="replicate"/>
    </region>     

    <region name="employees" refid="PROXY">
      <!--   <region-attributes refid="PROXY" pool-name="gfPool" data-policy="replicate"/>   -->
      <region-attributes refid="PROXY" data-policy="replicate"/>
    </region>  
</client-cache>  
Department.java
package spring.gemfire.repository.deptemp.beans;

import java.io.Serializable;
import java.util.Properties;
import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.mapping.Region;
import com.gemstone.gemfire.cache.Declarable;

@Region("departments")
public class Department implements Declarable, Serializable {
    private static final long   serialVersionUID    = -9097335119586059309L;
    @Id
    private int                 deptno;
    private String              name;

    @Override
    public boolean equals(final Object obj) {
        if (this == obj) { return true; }
        if (obj == null) { return false; }
        if (getClass() != obj.getClass()) { return false; }
        final Department other = (Department) obj;
        if (deptno != other.deptno) { return false; }
        if (name == null) {
            if (other.name != null) { return false; }
        } else if (!name.equals(other.name)) { return false; }
        return true;
    }

    public int getDeptno() {
        return deptno;
    }

    public String getName() {
        return name;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + deptno;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    public void init(final Properties props) {
        // TODO Auto-generated method stub
        deptno = Integer.parseInt(props.getProperty("deptno"));
        name = props.getProperty("name");
    }

    public void setDeptno(final int deptno) {
        this.deptno = deptno;
    }

    public void setName(final String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Department [deptno=" + deptno + ", name=" + name + "]";
    }
}
Employee.java
package spring.gemfire.repository.deptemp.beans;

import java.io.Serializable;
import java.util.Properties;
import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.mapping.Region;
import com.gemstone.gemfire.cache.Declarable;

@Region("employees")
public class Employee implements Declarable, Serializable {
    private static final long   serialVersionUID    = -8229531542107983344L;
    @Id
    private int                 empno;
    private String              name;
    private String              job;
    private int                 deptno;

    @Override
    public boolean equals(final Object obj) {
        if (this == obj) { return true; }
        if (obj == null) { return false; }
        if (getClass() != obj.getClass()) { return false; }
        final Employee other = (Employee) obj;
        if (deptno != other.deptno) { return false; }
        if (empno != other.empno) { return false; }
        if (job == null) {
            if (other.job != null) { return false; }
        } else if (!job.equals(other.job)) { return false; }
        if (name == null) {
            if (other.name != null) { return false; }
        } else if (!name.equals(other.name)) { return false; }
        return true;
    }

    public int getDeptno() {
        return deptno;
    }

    public int getEmpno() {
        return empno;
    }

    public String getJob() {
        return job;
    }

    public String getName() {
        return name;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + deptno;
        result = prime * result + empno;
        result = prime * result + ((job == null) ? 0 : job.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    public void init(final Properties props) {
        // TODO Auto-generated method stub
        empno = Integer.parseInt(props.getProperty("empno"));
        name = props.getProperty("name");
        job = props.getProperty("job");
        deptno = Integer.parseInt(props.getProperty("deptno"));
    }

    public void setDeptno(final int deptno) {
        this.deptno = deptno;
    }

    public void setEmpno(final int empno) {
        this.empno = empno;
    }

    public void setJob(final String job) {
        this.job = job;
    }

    public void setName(final String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Employee [empno=" + empno + ", name=" + name + ", job=" + job + ", deptno=" + deptno + "]";
    }
}
DeptRepository.java
package spring.gemfire.repository.deptemp.repos;

import java.util.Collection;

import org.springframework.data.gemfire.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import spring.gemfire.repository.deptemp.beans.Department;

//@Repository(DeptRepository.BEAN_ID)
public interface DeptRepository extends CrudRepository<Department, Integer> {
      String BEAN_ID="deptRepository";

    Department findByName(String name);

    @Query("SELECT * FROM /departments")
    Collection<Department> myFindAll();
}
EmpRepository.java
package spring.gemfire.repository.deptemp.repos;

import java.util.Collection;

import org.springframework.data.gemfire.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import spring.gemfire.repository.deptemp.beans.Employee;

//@Repository(DeptRepository.BEAN_ID)
public interface EmpRepository extends CrudRepository<Employee, Integer> {
    String BEAN_ID="empRepository";

    @Query("SELECT * FROM /employees where deptno = $1")
    Collection<Employee> empsInDeptno(int deptno);

    @Query("SELECT * FROM /employees")
    Collection<Employee> myFindAll();
}
SpringFactoryConfig.java
package spring.gemfire.repository.deptemp.util;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public final class SpringFactoryConfig {
    private static String               DEFAULT_CONFIG_FILE = "gemfirebean1.xml";
    private static SpringFactoryConfig  INSTANCE;

    public static final BeanFactory getDefaultSpringBeanFactory() {
        if (INSTANCE == null) {
            INSTANCE = new SpringFactoryConfig(DEFAULT_CONFIG_FILE);
        }
        return INSTANCE.getBeanFactory();
    }

    public static SpringFactoryConfig getSpringFactoryConfig(final String configFile) {
        return new SpringFactoryConfig(configFile);
    }

    private final ApplicationContext    springAppContext;

    private SpringFactoryConfig(final String configFile) {
        springAppContext = new ClassPathXmlApplicationContext("classpath:" + configFile);
        if (springAppContext == null) { throw new IllegalStateException("Cannot get an instance of the Spring bean factory !"); }
    }

    public final <T> T getBean(final String name, final Class<T> beanType) {
        if (springAppContext.isTypeMatch(name, beanType)) {
            return beanType.cast(springAppContext.getBean(name));
        } else {
            throw new IllegalArgumentException("Bean " + name + " is not of appropriate type " + beanType);
        }
    }

    public final BeanFactory getBeanFactory() {
        return springAppContext;
    }
}
Test.java
package spring.gemfire.repository.test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import spring.gemfire.repository.deptemp.beans.Department;
import spring.gemfire.repository.deptemp.beans.Employee;
import spring.gemfire.repository.deptemp.repos.DeptRepository;
import spring.gemfire.repository.deptemp.repos.EmpRepository;

public class Test {
    public static void main(final String[] args) {
        // TODO Auto-generated method stub
        final Test t = new Test();
        // t.deleteData();
        // t.saveData();
        t.getData();
        System.out.println("all done..");
    }

    private ConfigurableApplicationContext  ctx = null;

    public Test() {
        ctx = new ClassPathXmlApplicationContext("mvc-dispatcher-servlet.xml");
    }

    public void deleteData() {
        final DeptRepository deptRepos = ctx.getBean(DeptRepository.class);
        // get quick size
        System.out.println("** Size of dept repository **");
        System.out.println("Size = " + deptRepos.count());
        // deptRepos.deleteAll();
        final Collection<Department> deps = deptRepos.myFindAll();
        deptRepos.delete(deps);
        for (final Department d : deps) {
            System.out.println(d.toString());
            // deptRepos.delete(d);
        }
        System.out.println("** dept repository deleted All**");
        final EmpRepository empRepos = ctx.getBean(EmpRepository.class);
        // get quick size
        System.out.println("** Size of emp repository **");
        System.out.println("Size = " + empRepos.count());
        // empRepos.deleteAll();
        final Collection<Employee> emps = empRepos.myFindAll();
        empRepos.delete(emps);
        for (final Employee e : emps) {
            System.out.println(e.toString());
            // empRepos.delete(e);
        }
        System.out.println("** empRepos repository deleted All**");
    }

    public void getData() {
        final DeptRepository deptRepos = ctx.getBean(DeptRepository.class);
        // get quick size
        System.out.println("** Size of dept repository **");
        System.out.println("Size = " + deptRepos.count());
        // call findOne crud method by key
        System.out.println("** calling  deptRepos.findOne(\"2\") **");
        final Department dept = deptRepos.findOne(5);
        System.out.println(dept);
        // call findOne crud method for an attribute
        System.out.println("** calling  deptRepos.findByName(\"B\") **");
        final Department dept2 = deptRepos.findByName("B");
        System.out.println(dept2);
        // call my own findAll
        final Collection<Department> deps = deptRepos.myFindAll();
        System.out.println("\n** All Departments using -> deptRepos.myFindAll()");
        System.out.println("Defined as : @Query(\"SELECT * FROM /departments\") ");
        System.out.println("Collection<Department> myFindAll(); ** ");
        for (final Department d : deps) {
            System.out.println(d.toString());
        }
        final EmpRepository empRepos = ctx.getBean(EmpRepository.class);
        // get quick size
        System.out.println("** Size of emp repository **");
        System.out.println("Size = " + empRepos.count());
        final Employee empt = empRepos.findOne(2);
        System.out.println(empt);
        final Collection<Employee> emps = empRepos.empsInDeptno(2);
        System.out.println("\n ** All Employees in dept 2 using -> Collection<Employee> empsInDeptno(int deptno) **");
        for (final Employee e : emps) {
            System.out.println(e.toString());
        }
    }

    public void saveData() {
        final DeptRepository deptRepos = ctx.getBean(DeptRepository.class);
        // get quick size
        System.out.println("** Size of dept repository **");
        System.out.println("Size = " + deptRepos.count());
        final Department d1 = new Department();
        d1.setDeptno(1);
        d1.setName("A");
        final Department d2 = new Department();
        d2.setDeptno(2);
        d2.setName("B");
        final Department d3 = new Department();
        d3.setDeptno(5);
        d3.setName("C");
        final List<Department> dList = new ArrayList<Department>();
        dList.add(d1);
        dList.add(d2);
        dList.add(d3);
        System.out.println("** calling  deptRepos.save() **");
        final Iterable<Department> deptCollection = deptRepos.save(dList);
        for (final Department d : deptCollection) {
            System.out.println("** saved department **" + d);
        }
        final EmpRepository empRepos = ctx.getBean(EmpRepository.class);
        // get quick size
        System.out.println("** Size of emp repository **");
        System.out.println("Size = " + empRepos.count());
        final Employee e1 = new Employee();
        e1.setEmpno(1);
        e1.setName("X");
        e1.setDeptno(1);
        e1.setJob("J1");
        final Employee e2 = new Employee();
        e2.setEmpno(2);
        e2.setName("Y");
        e2.setDeptno(2);
        e2.setJob("J2");
        final Employee e3 = new Employee();
        e3.setEmpno(3);
        e3.setName("Y");
        e3.setDeptno(5);
        e3.setJob("J5");
        final List<Employee> eList = new ArrayList<Employee>();
        eList.add(e1);
        eList.add(e2);
        eList.add(e3);
        System.out.println("** calling  empRepos.save() **");
        final Iterable<Employee> empCollection = empRepos.save(eList);
        for (final Employee e : empCollection) {
            System.out.println("** saved employee **" + e);
        }
    }
}
GemFireJSONController.java
package spring.gemfire.repository.deptemp.controller;


import java.util.Collection;





import javax.annotation.PostConstruct;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import spring.gemfire.repository.deptemp.beans.Department;
import spring.gemfire.repository.deptemp.beans.Employee;
import spring.gemfire.repository.deptemp.repos.DeptRepository;
import spring.gemfire.repository.deptemp.repos.EmpRepository;
import spring.gemfire.repository.deptemp.util.SpringFactoryConfig;


@Controller
@RequestMapping("/gemfire")
public class GemFireJSONController {


     @RequestMapping(value ="/create/employee", method = RequestMethod.POST,headers="Accept=application/json")
        public   ResponseEntity<?>  createEmployee(@RequestBody Employee emp) {
            System.out.println("Start createEmployee.");

            getEmpRepository().save(emp);

              return new ResponseEntity<>(emp, HttpStatus.CREATED);
        }

     @RequestMapping(value ="/create/department", method = RequestMethod.POST,headers="Accept=application/json")
        public   ResponseEntity<?>  createDepartment(@RequestBody Department dept) {
            System.out.println("Start createEmployee.");
            getDeptRepository().save(dept);
              return new ResponseEntity<>(dept, HttpStatus.CREATED);
        }

      @RequestMapping(value = "/employee/all", method = RequestMethod.GET,headers="Accept=application/json")
        public @ResponseBody Collection<Employee> getAllEmployees() {
          System.out.println("Start getAllEmployeess.");      


            return getEmpRepository().myFindAll();
        }

      @RequestMapping(value = "/department/all", method = RequestMethod.GET,headers="Accept=application/json")
        public @ResponseBody Collection<Department> getAllDepartments() {
          System.out.println("Start getAllDepartments.");          

            return getDeptRepository().myFindAll();
        }

      private DeptRepository getDeptRepository(){
             return SpringFactoryConfig.getDefaultSpringBeanFactory().getBean(DeptRepository.BEAN_ID,
                     DeptRepository.class);
      }

      private EmpRepository getEmpRepository(){
             return SpringFactoryConfig.getDefaultSpringBeanFactory().getBean(EmpRepository.BEAN_ID,
                     EmpRepository.class);
      }


}


Example 2 with DTO:

I have tried example with DTO
package com.viquar.gem.fire.example;

import static java.lang.System.out;
import java.util.Calendar;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.ClientCacheFactory;

public class GemFireDTOExample {
    public static void main(final String[] args) {
        // final ClientCache cache = new ClientCacheFactory().create();
        final ClientCache cache = new ClientCacheFactory().set("cache-xml-file", "gemfiredtoexample.xml").create();
        System.out.println("cache-------------------->" + cache);
        final Region<Employee, Address> sampleRegion = cache.getRegion("dtoTestRegion");
        final Employee emp1 = getEmployee(1L, "shahbaz", 16, 1990);
        final Address add1 = getAddress("B-12", "M.G.ROAD", "PUNE");
        sampleRegion.put(emp1, add1);
        //
        final Address sampleValue = sampleRegion.get(emp1);
        out.println("------------------------------------------------------" + sampleValue);
        // sampleRegion.clear();
        cache.close();
    }

    private static Address getAddress(final String blockName, final String street, final String city) {
        final Address add = new Address();
        add.setBlockName(blockName);
        add.setCity(city);
        add.setStreet(street);
        return add;
    }

    private static Employee getEmployee(final Long empId, final String name, final int date, final int year) {
        final Employee emp = new Employee();
        emp.setEmpId(empId);
        emp.setName(name);
        final Calendar dateOfBirth = Calendar.getInstance();
        dateOfBirth.set(year, Calendar.DECEMBER, date);
        emp.setDateOfBirth(dateOfBirth.getTime());
        return emp;
    }
}
XML:
<!DOCTYPE client-cache PUBLIC
    "-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
    "http://www.gemstone.com/dtd/cache6_5.dtd">
<client-cache>
    <pool name="cacheServerPool">
        <locator host="localhost" port="10334" />
    </pool>
        <region name="dtoTestRegion" refid="PROXY">
            <region-attributes refid="PROXY" data-policy="replicate">
                <key-constraint>com.viquar.gem.fire.example.Employee</key-constraint>
                <value-constraint>com.viquar.gem.fire.example.Address</value-constraint>
            </region-attributes>    
        </region>
</client-cache>
Then after create Jar file with DTO
Address.class Employee.class
Now copy jar file inside following path :
C:\devprogs\Pivotal_GemFire_800_b48398_Windows\bin\locator1\cluster_config\cluster
Now run following commands
gfsh>deploy --jar=C:\devprogs\Pivotal_GemFire_800_b48398_Windows\bin\locator1\cluster_config\cluster\employee_dto.jar
Could not process command due to GemFire error. 'C:\devprogs\Pivotal_GemFire_800_b48398_Windows\bin\locator1\cluster_config\cluster\employee_dto.jar' not found.

gfsh>deploy --jar=C:\devprogs\Pivotal_GemFire_800_b48398_Windows\bin\locator1\cluster_config\cluster\employee_dto.jar
Member  |   Deployed JAR   | Deployed JAR Location
------- | ---------------- | -----------------------------------------------------------------------------------
server1 | employee_dto.jar | C:\devprogs\Pivotal_GemFire_800_b48398_Windows\bin\server1\vf.gf#employee_dto.jar#2
server2 | employee_dto.jar | C:\devprogs\Pivotal_GemFire_800_b48398_Windows\bin\server2\vf.gf#employee_dto.jar#2

gfsh>create region --name=dtoTestRegion --type=REPLICATE --key-constraint=com.viquar.gem.fire.example.Employee --value-constraint=com.viquar.gem.fire.example.Address
Member  | Status
------- | --------------------------------------------
server1 | Region "/dtoTestRegion" created on "server1"
server2 | Region "/dtoTestRegion" created on "server2"
Now run your program after that run following query in gfsh
gfsh>query --query='select * from /dtoTestRegion'

Result     : true
startCount : 0
endCount   : 20
Rows       : 1

blockName | city | street
--------- | ---- | --------
B-12      | PUNE | M.G.ROAD

NEXT_STEP_NAME : END

gfsh>


Example 3
package com.vaquar.example2;

import java.util.List;
import org.json.simple.JSONObject;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.ClientCacheFactory;
import com.gemstone.gemfire.cache.query.SelectResults;
import com.gemstone.gemfire.pdx.JSONFormatter;
import com.gemstone.gemfire.pdx.PdxInstance;


public final class JSONGemFireClient {
    public static final String  REGION_NAME = "regionB";    

    /**
     * @param args
     */
    public static void main(final String[] args) {
        // 
        final JSONGemFireClient test = new JSONGemFireClient();
        try {
            test.run();
        } catch (final Exception e) {
            // 
            e.printStackTrace();
        }
    }

    public ClientCache  cache   = null;

    public JSONGemFireClient() {
        //
        cache = new ClientCacheFactory().set("name", "JSONClient").set("cache-xml-file", "GemFire6.xml").create();
        //

    }

    public void run() throws Exception {
        JSONObject obj = null;
        System.out.println("Connecting to the distributed system and creating the cache.");
        // Get the exampleRegion
        final Region<String, PdxInstance> jsonregion = cache.getRegion(REGION_NAME);
        //
        System.out.println("jsonregion----------jsonregion.getFullPath()--------------->" + cache.getRegionAttributes(REGION_NAME)
                + jsonregion.getFullPath());
        //
        System.out.println("Example region \"" + jsonregion.getFullPath() + "\" created in cache.");
        //
        //
        // add 5 entries with age = 30
        for (int i = 1; i <= 5; i++) {
            obj = new JSONObject();
            obj.put("name", String.format("Person%s", i));
            obj.put("age", 30);
            System.out.println("------------------" + obj.toJSONString());
            final String json = obj.toJSONString();
            System.out.println("************************************" + JSONFormatter.fromJSON(json));
            jsonregion.put(String.valueOf(i), JSONFormatter.fromJSON(json));
        }
        //
        //
        // add 5 entries with age = 20
        for (int i = 6; i <= 10; i++) {
            obj = new JSONObject();
            obj.put("name", String.format("Person%s", i));
            obj.put("age", 20);
            jsonregion.put(String.valueOf(i), JSONFormatter.fromJSON(obj.toJSONString()));
        }
        //
        //
        // Query region
        final SelectResults<PdxInstance> sr = jsonregion.query("age = 30");
        System.out.println("Number of entries where age = 30 is -> " + sr.size());
        //
        final List<PdxInstance> entries = sr.asList();
        //
        for (final PdxInstance val : entries) {
            System.out.println("\n** JSON data ** ");
            System.out.println("Name = " + val.getField("name"));
            System.out.println("Full JSON data -> \n" + JSONFormatter.toJSON(val));
        }
        cache.close();
    }
}
XML
<!DOCTYPE client-cache PUBLIC
    "-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
    "http://www.gemstone.com/dtd/cache6_5.dtd">
<client-cache>
    <pool name="cacheServerPool">
        <locator host="localhost" port="10334" />
    </pool>
        <region name="regionB" refid="PROXY">
            <region-attributes refid="PROXY" data-policy="replicate">
            </region-attributes>    
        </region>
</client-cache>
Cache.xml not required.
Results and commands :
start locator --name=locator1 --port=10334 --J=-Xms256m --J=-Xmx256m
start server --name=server1 --server-port=40411
start server --name=server2 --server-port=40412


 gfsh>create region --name=regionB --type=LOCAL
Member  | Status
------- | --------------------------------------
server1 | Region "/regionB" created on "server1"

gfsh>list regions
List of regions
---------------
myPOregion
regionA
regionB
regionC

gfsh>query --query='select * from /regionB'

Result     : true
startCount : 0
endCount   : 20
Rows       : 10

age | name
--- | --------
30  | Person1
30  | Person3
30  | Person2
20  | Person8
20  | Person9
30  | Person4
30  | Person5
20  | Person6
20  | Person7
20  | Person10

NEXT_STEP_NAME : END
Second example fix :
package com.vaquar.example2;

import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.ClientCacheFactory;

public final class GemFireSample3 {
    public static void main(final String[] args) {

        final ClientCache cache = new ClientCacheFactory().set("cache-xml-file", "HelloWorld1.xml").create();
        final Region<String, String> sampleRegion = cache.getRegion("pingTestRegion");
        sampleRegion.put("samplekey", "Sample Value Test");
        //
        final String sampleValue = sampleRegion.get("samplekey");
        System.out.println("sampleValue"+sampleValue);

        // sampleRegion.clear();
        cache.close();
    }
}
XML
<!DOCTYPE client-cache PUBLIC
    "-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
    "http://www.gemstone.com/dtd/cache6_5.dtd">
<client-cache>
    <pool name="cacheServerPool">
        <locator host="localhost" port="10334" />
    </pool>

        <region name="pingTestRegion" refid="PROXY">
            <region-attributes refid="PROXY" data-policy="replicate">
                <key-constraint>java.lang.String</key-constraint>
                <value-constraint>java.lang.String</value-constraint>
            </region-attributes>    
        </region>
<!--    </region> -->
</client-cache>
Results
gfsh>create region --name=pingTestRegion --type=LOCAL
Member  | Status
------- | ---------------------------------------------
server1 | Region "/pingTestRegion" created on "server1"


gfsh>query --query='select * from /pingTestRegion'

Result     : true
startCount : 0
endCount   : 20
Rows       : 1

Result
-----------------
Sample Value Test

NEXT_STEP_NAME : END

gfsh>

Jar:
antlr-2.7.7.jar
gemfire-8.0.0.jar
jackson-annotations-2.6.0.jar
jackson-core-2.6.0.jar
jackson-databind-2.6.0.jar
json-simple-1.1.1.jar

Sunday, 12 June 2016

Java 9 and Project Jigsaw

Standard








What is modularity : as defination
  Designed with standardized units or dimensions, as for easy assembly and repair or flexible arrangement and use: modular furniture; modular homes.
  In Technial We can say develop independed modules which comunicate using standardised interface (separation of concerns,loose coupling)
  Modularity composed of standardized, self-contained units for easy construction and flexible use.
 


Packages vs module
 
   Package: It is bunch of class(.java) file which is separate by their function or their name. so it is also help for naming,A package is a namespace; it let's you handle naming conflicts.
  
   Module: Large applications are typically built in several parts, which are more tightly connected to each other than to the outside. Therefore, one would like to give these parts more access to each other than the outside world has. One might also want these parts reused at many points in the system without worrying about the synchronization of shared data. this part is called module in java. WAR,EAR jar etc are called module in java language.

Why is modularity required? 
Better encapsulation:  Currently, every public class/interface and every public method is published to the world.  If the intention is to only make a method or class available among more than one package within an application it cannot be encapsulated without carefully omitting said classes from whatever jars a library publishes to client applications.  With a proper module system, a package may be marked as internal to the module and its contents available throughout the module but not to the outside world.  This in turn imposes discipline and makes it much easier for developers to publish more compact APIs, since potentially only one package out of several internal packages could be made visible to the outside world.

  • Versioning: Currently, Java developers much publish versions by hacking the manifest file within a Jar file.  This means there is no intrinsic support for publishing versions or making dependencies among modules clear.   With modularity, it becomes possible for module B to publish that it requires module A version 3 (or versions 3-4), and that any module A version lower than 2 will possibly break module B's functionality.  The lack of such a mechanism is one of the causes of "jar hell" experienced by Java developers.  There are ways around this, such as using the file system (ex a sub-directory of a package directory is the version identifier), but they're far less elegant than a proper module system.
  • Compile-time and runtime symmetry:  Java compile time and runtime dependencies are orthogonal, which causes problems such as the potential for ClassDefNotFound errors.   A proper module system guarantees that compile time and runtime dependencies are satisfied atomically.
  • Tooling:  IDEs should be able to more easily manage a project's dependencies.
  • Deployment and module repositories:  Given the compile time and runtime symmetry above, it will be possible to publish modules within repositories, either on the web or internally on a company network.   This will eliminate a whole host of deployment issues as this mechanism is greatly superior to the currently one of managing jars and classpaths by hand (or within scrips), or any workaround involving an internal filesystem.   Caching of remote modules also becomes possible.
  • Separate Release Schedules:  It should be possible for Oracle to release various libraries separately, according to their own schedules, instead of waiting for the window where the entirely next version of the JDK is deployed.   For example, a new version of a concurrency library with desirable improvements could be published between JDK releases.
  • Quarantine of legacy APIs:  The JDK is currently a monolithic blob of unrelated functionality that's packaged together.  This means if all I want is to run a simple Java program, I need to download CORBA, Swing, the legacy Date/Time API, etc, (especially APIs that have been marked deprecated) even if I only use a subset of the functionality.   What's more, with every new version of the JDK, this functionality must be kept up to date and the commitment has been made to maintain all of these classes.  With modules, this legacy functionality can be quarantined from the rest of the JDK and put into strict maintenance mode, to be used only by those who need them and Oracle's backward compatibility burden could potentially be significantly reduced.
What is Jigsaw

 Java Platform Module System – commonly known as Project Jigsaw, or JSR 376 – is that it has been used to break apart the monolithic Java runtime into smaller modules.
 JEP 201: Modular Source Code


 Class loaders - Jigsaw will not use class loader; it's up to runtimes (such as application servers or OSGi) to work with modules and class loaders

Dependencies - Jigsaw will allow to specify dependency on modules by name but not at the package level

Dynamic Services - The runtime services model provided by OSGi will not be provided by Jigsaw
Jigsaw is not intended to replace and/or compete with any other runtime or build time module system (such as OSGi or Maven). In fact, it's the intention of Jigsaw to be interoperable (somehow) with both.

JEPs
200: The Modular JDK
Module summary
201: Modular Source Code
220: Modular Run-Time Images
260: Encapsulate Most Internal APIs
261: Module System
282: jlink: The Java Linker

http://openjdk.java.net/projects/jigsaw/

Download : https://jdk9.java.net/download/

 Referances :
   http://openjdk.java.net/projects/jigsaw/spec/sotms/
   http://openjdk.java.net/projects/jigsaw/spec/sotms/
   http://www.merriam-webster.com/dictionary/modular
   https://en.wikipedia.org/wiki/Modular_programming
   http://stackoverflow.com/questions/13157377/class-vs-package-vs-module-vs-component-vs-container-vs-service-vs-platform-in-j
   http://ldegruchy.blogspot.com/2014/09/java-9-modularity-is-enough.html