diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index 932b62a235..f57566b879 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -48,6 +48,7 @@ spring-data-eclipselink spring-data-elasticsearch spring-data-gemfire + spring-data-geode spring-data-jpa spring-data-keyvalue spring-data-mongodb diff --git a/persistence-modules/spring-data-geode/pom.xml b/persistence-modules/spring-data-geode/pom.xml new file mode 100644 index 0000000000..abd3049755 --- /dev/null +++ b/persistence-modules/spring-data-geode/pom.xml @@ -0,0 +1,94 @@ + + + 4.0.0 + spring-data-geode + spring-data-geode + jar + Intro to Spring Data Geode + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + + + org.springframework.boot + spring-boot-starter + 2.1.9.RELEASE + + + org.springframework.boot + spring-boot-starter-logging + + + + + org.springframework.boot + spring-boot-starter-web + ${spring-boot-version} + + + org.springframework.boot + spring-boot-starter-logging + + + + + org.springframework.geode + spring-geode-starter + ${spring-geode-starter-version} + + + + org.springframework.boot + spring-boot-autoconfigure + ${spring-boot-version} + + + org.springframework.boot + spring-boot-starter-test + ${spring-boot-version} + test + + + + + ${project.artifactId} + + + com.mysema.maven + maven-apt-plugin + 1.0 + + + generate-sources + + process + + + target/generated-sources + com.querydsl.apt.jpa.JPAAnnotationProcessor + + + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring-boot-version} + + + + + + 2.1.9.RELEASE + UTF-8 + com.baeldung.springdatageode.app.ClientCacheApp + 1.1.1.RELEASE + + + \ No newline at end of file diff --git a/persistence-modules/spring-data-geode/src/main/java/com/baeldung/springdatageode/app/ClientCacheApp.java b/persistence-modules/spring-data-geode/src/main/java/com/baeldung/springdatageode/app/ClientCacheApp.java new file mode 100644 index 0000000000..6e9a33e953 --- /dev/null +++ b/persistence-modules/spring-data-geode/src/main/java/com/baeldung/springdatageode/app/ClientCacheApp.java @@ -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); + } + +} diff --git a/persistence-modules/spring-data-geode/src/main/java/com/baeldung/springdatageode/controller/AppController.java b/persistence-modules/spring-data-geode/src/main/java/com/baeldung/springdatageode/controller/AppController.java new file mode 100644 index 0000000000..32f931820a --- /dev/null +++ b/persistence-modules/spring-data-geode/src/main/java/com/baeldung/springdatageode/controller/AppController.java @@ -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 addAuthor(@RequestBody Author author) throws Exception { + authorRepositoryImpl.save(author); + return new ResponseEntity<>("OK", HttpStatus.OK); + } + + @GetMapping(path = "/author" ) + public ResponseEntity getAuthor(@RequestParam("id") String id) throws Exception { + Optional author = authorRepositoryImpl.findById(Long.parseLong(id)); + return new ResponseEntity<>(author.isPresent() ? author.get() : null, HttpStatus.OK); + } + +} diff --git a/persistence-modules/spring-data-geode/src/main/java/com/baeldung/springdatageode/domain/Author.java b/persistence-modules/spring-data-geode/src/main/java/com/baeldung/springdatageode/domain/Author.java new file mode 100644 index 0000000000..4a2e58ff34 --- /dev/null +++ b/persistence-modules/spring-data-geode/src/main/java/com/baeldung/springdatageode/domain/Author.java @@ -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"; + } + +} + diff --git a/persistence-modules/spring-data-geode/src/main/java/com/baeldung/springdatageode/repo/AuthorRepository.java b/persistence-modules/spring-data-geode/src/main/java/com/baeldung/springdatageode/repo/AuthorRepository.java new file mode 100644 index 0000000000..3a311039a4 --- /dev/null +++ b/persistence-modules/spring-data-geode/src/main/java/com/baeldung/springdatageode/repo/AuthorRepository.java @@ -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 { + + Optional findById(Long id); +} diff --git a/persistence-modules/spring-data-geode/src/main/java/com/baeldung/springdatageode/service/AuthorService.java b/persistence-modules/spring-data-geode/src/main/java/com/baeldung/springdatageode/service/AuthorService.java new file mode 100644 index 0000000000..1bf4cbaba1 --- /dev/null +++ b/persistence-modules/spring-data-geode/src/main/java/com/baeldung/springdatageode/service/AuthorService.java @@ -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()); + } +} diff --git a/persistence-modules/spring-data-geode/src/main/resources/application.properties b/persistence-modules/spring-data-geode/src/main/resources/application.properties new file mode 100644 index 0000000000..7b7cb5a37b --- /dev/null +++ b/persistence-modules/spring-data-geode/src/main/resources/application.properties @@ -0,0 +1 @@ +server.port=9091 \ No newline at end of file diff --git a/persistence-modules/spring-data-geode/src/main/resources/logback.xml b/persistence-modules/spring-data-geode/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/persistence-modules/spring-data-geode/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file