BAEL-3091: The Prototype Pattern in Java (changed code based on valid comments from a reader)

This commit is contained in:
Vivek Balasubramaniam
2019-10-29 22:27:15 +05:30
parent db85c8f275
commit d3d5b060e7
20517 changed files with 1642290 additions and 0 deletions
@@ -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);
}
}
@@ -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);
}
}
@@ -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";
}
}
@@ -0,0 +1,11 @@
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> {
}
@@ -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());
}
}
@@ -0,0 +1 @@
server.port=9091
@@ -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>