Reflect grouping in directory structure
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package com.example.data.redis;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.awaitility.Awaitility;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.aot.smoketest.support.assertj.AssertableOutput;
|
||||
import org.springframework.aot.smoketest.support.junit.ApplicationTest;
|
||||
|
||||
@ApplicationTest
|
||||
class DataRedisApplicationAotTests {
|
||||
|
||||
@Test
|
||||
void connectionTest(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output).hasSingleLineContaining("connection: OK");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void templateOps(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output).hasSingleLineContaining("template ops: OK");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void keyBoundOps(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output).hasSingleLineContaining("key bound ops: OK");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void redisDataStructure(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output).hasSingleLineContaining("redis set: OK");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void jsonSerializer(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output).hasSingleLineContaining(
|
||||
"json-serializer: Person{firstname='json-serialized-1', lastname='value'}");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void pubSub(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output).hasSingleLineContaining("pub/sub: [payload]");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void findAll(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output).hasSingleLineContaining("findAll(): Person{firstname='first-1', lastname='last-1'}")
|
||||
.hasSingleLineContaining("findAll(): Person{firstname='first-2', lastname='last-2'}")
|
||||
.hasSingleLineContaining("findAll(): Person{firstname='first-3', lastname='last-3'}");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void findByLastName(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output)
|
||||
.hasSingleLineContaining("findByLastname(): Person{firstname='first-3', lastname='last-3'}");
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
110
data/data-redis/src/main/java/com/example/data/redis/CLR.java
Normal file
110
data/data-redis/src/main/java/com/example/data/redis/CLR.java
Normal file
@@ -0,0 +1,110 @@
|
||||
package com.example.data.redis;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.data.redis.core.BoundValueOperations;
|
||||
import org.springframework.data.redis.core.RedisCallback;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
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.RedisSerializer;
|
||||
import org.springframework.data.redis.support.collections.DefaultRedisSet;
|
||||
import org.springframework.data.redis.support.collections.RedisSet;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
class CLR implements CommandLineRunner {
|
||||
|
||||
@Autowired
|
||||
StringRedisTemplate template;
|
||||
|
||||
@Autowired
|
||||
private PersonRepository personRepository;
|
||||
|
||||
@Autowired
|
||||
private PubSubMessageHandler messageHandler;
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
|
||||
connectionCommand();
|
||||
templateOperations();
|
||||
keyBoundOperations();
|
||||
redisBackedSet();
|
||||
jsonSerializer();
|
||||
pubSub();
|
||||
|
||||
this.personRepository.save(new Person("first-1", "last-1"));
|
||||
this.personRepository.save(new Person("first-2", "last-2"));
|
||||
this.personRepository.save(new Person("first-3", "last-3"));
|
||||
|
||||
for (Person person : this.personRepository.findAll()) {
|
||||
System.out.printf("findAll(): %s%n", person);
|
||||
}
|
||||
|
||||
for (Person person : this.personRepository.findByLastname("last-3")) {
|
||||
System.out.printf("findByLastname(): %s%n", person);
|
||||
}
|
||||
}
|
||||
|
||||
private void jsonSerializer() {
|
||||
RedisTemplate<String, Person> t = new RedisTemplate<>();
|
||||
t.setConnectionFactory(template.getConnectionFactory());
|
||||
t.setKeySerializer(RedisSerializer.string());
|
||||
t.setValueSerializer(RedisSerializer.json());
|
||||
t.afterPropertiesSet();
|
||||
t.opsForValue().set("json-serializer", new Person("json-serialized-1", "value"));
|
||||
System.out.printf("json-serializer: %s%n", t.opsForValue().get("json-serializer"));
|
||||
}
|
||||
|
||||
private void redisBackedSet() {
|
||||
RedisSet<String> redisSet = new DefaultRedisSet<>("redis-set", template);
|
||||
redisSet.add("OK");
|
||||
System.out.printf("redis set: %s%n", redisSet.iterator().next());
|
||||
}
|
||||
|
||||
private void keyBoundOperations() {
|
||||
BoundValueOperations<String, String> keyBoundOps = template.boundValueOps("bound-key");
|
||||
keyBoundOps.set("OK");
|
||||
System.out.printf("key bound ops: %s%n", keyBoundOps.get());
|
||||
}
|
||||
|
||||
private void templateOperations() {
|
||||
this.template.opsForValue().set("success-token", "OK");
|
||||
System.out.printf("template ops: %s%n", this.template.opsForValue().get("success-token"));
|
||||
}
|
||||
|
||||
private void connectionCommand() {
|
||||
this.template.execute((RedisCallback<String>) (connection) -> {
|
||||
connection.serverCommands().flushAll();
|
||||
System.out.println("connection: OK");
|
||||
return "OK";
|
||||
});
|
||||
}
|
||||
|
||||
private void pubSub() throws InterruptedException {
|
||||
|
||||
String channel = "pubsub::test";
|
||||
|
||||
MessageListenerAdapter adapter = new MessageListenerAdapter(messageHandler);
|
||||
adapter.setSerializer(template.getValueSerializer());
|
||||
adapter.afterPropertiesSet();
|
||||
|
||||
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||
container.setConnectionFactory(template.getConnectionFactory());
|
||||
container.setBeanName("container");
|
||||
container.addMessageListener(adapter, Arrays.asList(new ChannelTopic(channel)));
|
||||
container.afterPropertiesSet();
|
||||
container.start();
|
||||
|
||||
template.convertAndSend(channel, "payload");
|
||||
|
||||
Thread.sleep(100);
|
||||
System.out.printf("pub/sub: %s%n", messageHandler.receivedMessages());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.example.data.redis;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DataRedisApplication {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
SpringApplication.run(DataRedisApplication.class, args);
|
||||
Thread.currentThread().join(); // To be able to measure memory consumption
|
||||
}
|
||||
|
||||
@Bean
|
||||
PubSubMessageHandler messageHandler() {
|
||||
return new PubSubMessageHandler();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.example.data.redis;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.redis.core.RedisHash;
|
||||
import org.springframework.data.redis.core.index.Indexed;
|
||||
|
||||
@RedisHash("persons")
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
@Indexed
|
||||
private String firstname;
|
||||
|
||||
@Indexed
|
||||
private String lastname;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(String firstname, String lastname) {
|
||||
this.firstname = firstname;
|
||||
this.lastname = lastname;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String 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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person{" + "firstname='" + firstname + '\'' + ", lastname='" + lastname + '\'' + '}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.example.data.redis;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.repository.ListCrudRepository;
|
||||
|
||||
public interface PersonRepository extends ListCrudRepository<Person, String> {
|
||||
|
||||
List<Person> findByLastname(String lastname);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.example.data.redis;
|
||||
|
||||
import java.util.Deque;
|
||||
import java.util.concurrent.BlockingDeque;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
|
||||
import org.springframework.aot.hint.annotation.Reflective;
|
||||
|
||||
public class PubSubMessageHandler {
|
||||
|
||||
private BlockingDeque<Object> bag = new LinkedBlockingDeque<>(99);
|
||||
|
||||
@Reflective
|
||||
public void handleMessage(Object message) {
|
||||
bag.add(message);
|
||||
}
|
||||
|
||||
public Deque<Object> receivedMessages() {
|
||||
return bag;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
spring.data.redis.host=${REDIS_HOST:localhost}
|
||||
spring.data.redis.port=${REDIS_PORT_6379:6379}
|
||||
Reference in New Issue
Block a user