group persistence modules (#2890)
* move security content from spring-security-rest-full * swagger update * move query language to new module * rename spring-security-rest-full to spring-rest-full * group persistence modules
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
dbeb5f8ba4
commit
26c50909be
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.spring.data.redis.config;
|
||||
|
||||
import com.baeldung.spring.data.redis.queue.MessagePublisher;
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessagePublisher;
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.listener.ChannelTopic;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
|
||||
import org.springframework.data.redis.serializer.GenericToStringSerializer;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("com.baeldung.spring.data.redis")
|
||||
public class RedisConfig {
|
||||
|
||||
@Bean
|
||||
JedisConnectionFactory jedisConnectionFactory() {
|
||||
return new JedisConnectionFactory();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RedisTemplate<String, Object> redisTemplate() {
|
||||
final RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
|
||||
template.setConnectionFactory(jedisConnectionFactory());
|
||||
template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
|
||||
return template;
|
||||
}
|
||||
|
||||
@Bean
|
||||
MessageListenerAdapter messageListener() {
|
||||
return new MessageListenerAdapter(new RedisMessageSubscriber());
|
||||
}
|
||||
|
||||
@Bean
|
||||
RedisMessageListenerContainer redisContainer() {
|
||||
final RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||
container.setConnectionFactory(jedisConnectionFactory());
|
||||
container.addMessageListener(messageListener(), topic());
|
||||
return container;
|
||||
}
|
||||
|
||||
@Bean
|
||||
MessagePublisher redisPublisher() {
|
||||
return new RedisMessagePublisher(redisTemplate(), topic());
|
||||
}
|
||||
|
||||
@Bean
|
||||
ChannelTopic topic() {
|
||||
return new ChannelTopic("pubsub:queue");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.spring.data.redis.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Student implements Serializable {
|
||||
|
||||
public enum Gender {
|
||||
MALE, FEMALE
|
||||
}
|
||||
|
||||
private String id;
|
||||
private String name;
|
||||
private Gender gender;
|
||||
private int grade;
|
||||
|
||||
public Student(String id, String name, Gender gender, int grade) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.gender = gender;
|
||||
this.grade = grade;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Gender getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(Gender gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
public int getGrade() {
|
||||
return grade;
|
||||
}
|
||||
|
||||
public void setGrade(int grade) {
|
||||
this.grade = grade;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Student{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", gender=" + gender + ", grade=" + grade + '}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.spring.data.redis.queue;
|
||||
|
||||
public interface MessagePublisher {
|
||||
|
||||
void publish(final String message);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.spring.data.redis.queue;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.listener.ChannelTopic;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class RedisMessagePublisher implements MessagePublisher {
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
@Autowired
|
||||
private ChannelTopic topic;
|
||||
|
||||
public RedisMessagePublisher() {
|
||||
}
|
||||
|
||||
public RedisMessagePublisher(final RedisTemplate<String, Object> redisTemplate, final ChannelTopic topic) {
|
||||
this.redisTemplate = redisTemplate;
|
||||
this.topic = topic;
|
||||
}
|
||||
|
||||
public void publish(final String message) {
|
||||
redisTemplate.convertAndSend(topic.getTopic(), message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.spring.data.redis.queue;
|
||||
|
||||
import org.springframework.data.redis.connection.Message;
|
||||
import org.springframework.data.redis.connection.MessageListener;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class RedisMessageSubscriber implements MessageListener {
|
||||
|
||||
public static List<String> messageList = new ArrayList<String>();
|
||||
|
||||
public void onMessage(final Message message, final byte[] pattern) {
|
||||
messageList.add(message.toString());
|
||||
System.out.println("Message received: " + new String(message.getBody()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.spring.data.redis.repo;
|
||||
|
||||
import com.baeldung.spring.data.redis.model.Student;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface StudentRepository {
|
||||
|
||||
void saveStudent(Student person);
|
||||
|
||||
void updateStudent(Student student);
|
||||
|
||||
Student findStudent(String id);
|
||||
|
||||
Map<Object, Object> findAllStudents();
|
||||
|
||||
void deleteStudent(String id);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.spring.data.redis.repo;
|
||||
|
||||
import com.baeldung.spring.data.redis.model.Student;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.HashOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.Map;
|
||||
|
||||
@Repository
|
||||
public class StudentRepositoryImpl implements StudentRepository {
|
||||
|
||||
private static final String KEY = "Student";
|
||||
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
private HashOperations hashOperations;
|
||||
|
||||
@Autowired
|
||||
public StudentRepositoryImpl(RedisTemplate<String, Object> redisTemplate) {
|
||||
this.redisTemplate = redisTemplate;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
hashOperations = redisTemplate.opsForHash();
|
||||
}
|
||||
|
||||
public void saveStudent(final Student student) {
|
||||
hashOperations.put(KEY, student.getId(), student);
|
||||
}
|
||||
|
||||
public void updateStudent(final Student student) {
|
||||
hashOperations.put(KEY, student.getId(), student);
|
||||
}
|
||||
|
||||
public Student findStudent(final String id) {
|
||||
return (Student) hashOperations.get(KEY, id);
|
||||
}
|
||||
|
||||
public Map<Object, Object> findAllStudents() {
|
||||
return hashOperations.entries(KEY);
|
||||
}
|
||||
|
||||
public void deleteStudent(final String id) {
|
||||
hashOperations.delete(KEY, id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 855 B |
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.spring.data.redis;
|
||||
|
||||
import com.baeldung.spring.data.redis.config.RedisConfig;
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber;
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessagePublisher;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = RedisConfig.class)
|
||||
public class RedisMessageListenerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private RedisMessagePublisher redisMessagePublisher;
|
||||
|
||||
@Test
|
||||
public void testOnMessage() throws Exception {
|
||||
String message = "Message " + UUID.randomUUID();
|
||||
redisMessagePublisher.publish(message);
|
||||
Thread.sleep(100);
|
||||
assertTrue(RedisMessageSubscriber.messageList.get(0).contains(message));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.spring.data.redis.repo;
|
||||
|
||||
import com.baeldung.spring.data.redis.config.RedisConfig;
|
||||
import com.baeldung.spring.data.redis.model.Student;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = RedisConfig.class)
|
||||
public class StudentRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private StudentRepository studentRepository;
|
||||
|
||||
@Test
|
||||
public void whenSavingStudent_thenAvailableOnRetrieval() throws Exception {
|
||||
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
|
||||
studentRepository.saveStudent(student);
|
||||
final Student retrievedStudent = studentRepository.findStudent(student.getId());
|
||||
assertEquals(student.getId(), retrievedStudent.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpdatingStudent_thenAvailableOnRetrieval() throws Exception {
|
||||
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
|
||||
studentRepository.saveStudent(student);
|
||||
student.setName("Richard Watson");
|
||||
studentRepository.saveStudent(student);
|
||||
final Student retrievedStudent = studentRepository.findStudent(student.getId());
|
||||
assertEquals(student.getName(), retrievedStudent.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSavingStudents_thenAllShouldAvailableOnRetrieval() throws Exception {
|
||||
final Student engStudent = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
|
||||
final Student medStudent = new Student("Med2015001", "Gareth Houston", Student.Gender.MALE, 2);
|
||||
studentRepository.saveStudent(engStudent);
|
||||
studentRepository.saveStudent(medStudent);
|
||||
final Map<Object, Object> retrievedStudent = studentRepository.findAllStudents();
|
||||
assertEquals(retrievedStudent.size(), 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeletingStudent_thenNotAvailableOnRetrieval() throws Exception {
|
||||
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
|
||||
studentRepository.saveStudent(student);
|
||||
studentRepository.deleteStudent(student.getId());
|
||||
final Student retrievedStudent = studentRepository.findStudent(student.getId());
|
||||
assertNull(retrievedStudent);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user