BAEL-1871 - Spring Data Geode

This commit is contained in:
Anshul BANSAL
2019-10-08 15:40:45 +03:00
parent 5cffe22228
commit cd0d79d8f6
9 changed files with 262 additions and 0 deletions

View File

@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<artifactId>spring-data-geode</artifactId>
<name>spring-data-geode</name>
<packaging>jar</packaging>
<description>Intro to Spring Data Geode</description>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.9.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot-version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.geode</groupId>
<artifactId>spring-geode-starter</artifactId>
<version>${spring-geode-starter-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${spring-boot-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot-version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>maven-apt-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-version}</version>
</plugin>
</plugins>
</build>
<properties>
<spring-boot-version>2.1.9.RELEASE</spring-boot-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.baeldung.springdatageode.app.ClientCacheApp</start-class>
<spring-geode-starter-version>1.1.1.RELEASE</spring-geode-starter-version>
</properties>
</project>

View File

@@ -0,0 +1,32 @@
package com.baeldung.springdatageode.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.gemfire.config.annotation.ClientCacheApplication;
import org.springframework.data.gemfire.config.annotation.EnableClusterConfiguration;
import org.springframework.data.gemfire.config.annotation.EnableContinuousQueries;
import org.springframework.data.gemfire.config.annotation.EnableEntityDefinedRegions;
import org.springframework.data.gemfire.config.annotation.EnableIndexing;
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;
import com.baeldung.springdatageode.controller.AppController;
import com.baeldung.springdatageode.domain.Author;
import com.baeldung.springdatageode.repo.AuthorRepository;
import com.baeldung.springdatageode.service.AuthorService;
@SpringBootApplication
@ClientCacheApplication(subscriptionEnabled = true)
@EnableEntityDefinedRegions(basePackageClasses = Author.class)
@EnableIndexing
@EnableGemfireRepositories(basePackageClasses = AuthorRepository.class)
@ComponentScan(basePackageClasses = {AppController.class, AuthorService.class})
@EnableClusterConfiguration(useHttp = true, requireHttps=false)
@EnableContinuousQueries
public class ClientCacheApp {
public static void main(String[] args) {
SpringApplication.run(ClientCacheApp.class, args);
}
}

View File

@@ -0,0 +1,37 @@
package com.baeldung.springdatageode.controller;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.springdatageode.domain.Author;
import com.baeldung.springdatageode.repo.AuthorRepository;
@RestController
@CrossOrigin
public class AppController {
@Autowired
private AuthorRepository authorRepositoryImpl;
@PostMapping(path = "/author" )
public ResponseEntity<String> addAuthor(@RequestBody Author author) throws Exception {
authorRepositoryImpl.save(author);
return new ResponseEntity<>("OK", HttpStatus.OK);
}
@GetMapping(path = "/author" )
public ResponseEntity<Author> getAuthor(@RequestParam("id") String id) throws Exception {
Optional<Author> author = authorRepositoryImpl.findById(Long.parseLong(id));
return new ResponseEntity<>(author.isPresent() ? author.get() : null, HttpStatus.OK);
}
}

View File

@@ -0,0 +1,58 @@
package com.baeldung.springdatageode.domain;
import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.mapping.annotation.Indexed;
import org.springframework.data.gemfire.mapping.annotation.Region;
@Region(name = "Authors")
public class Author {
@Id
private Long id;
private String firstName;
private String lastName;
@Indexed
private int age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return this.firstName + " " + this.lastName + ", " + this.age + " years old";
}
}

View File

@@ -0,0 +1,12 @@
package com.baeldung.springdatageode.repo;
import com.baeldung.springdatageode.domain.Author;
import java.util.Optional;
import org.springframework.data.repository.CrudRepository;
public interface AuthorRepository extends CrudRepository<Author, Long> {
Optional<Author> findById(Long id);
}

View File

@@ -0,0 +1,14 @@
package com.baeldung.springdatageode.service;
import org.apache.geode.cache.query.CqEvent;
import org.springframework.data.gemfire.listener.annotation.ContinuousQuery;
import org.springframework.stereotype.Service;
@Service
public class AuthorService {
@ContinuousQuery(query = "SELECT * FROM /Authors a WHERE a.id = 1")
public void process(CqEvent event) {
System.out.println("Author #" + event.getKey() + " updated to " + event.getNewValue());
}
}

View File

@@ -0,0 +1 @@
server.port=9091

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>