@@ -29,13 +29,16 @@ Local Elasticsearch instance must be running to run the tests.
|
||||
== Spring Data JPA
|
||||
|
||||
* `eclipselink` - Sample project to show how to use Spring Data JPA with Spring Boot and https://www.eclipse.org/eclipselink/[Eclipselink].
|
||||
* `example` - Probably the project you want to have a look at first. Contains a variety of sample packages, showcasing the different levels at which you can use Spring Data JPA. Have a look at the `simple` package for the most basic setup.
|
||||
* `example` - Probably the project you want to have a look at first.
|
||||
Contains a variety of sample packages, showcasing the different levels at which you can use Spring Data JPA.
|
||||
Have a look at the `simple` package for the most basic setup.
|
||||
Contains also examples running on Virtual Threads.
|
||||
* `interceptors` - Example of how to enrich the repositories with AOP.
|
||||
* `jpa21` - Shows support for JPA 2.1 specific features (stored procedures support).
|
||||
* `multiple-datasources` - Examples of how to use Spring Data JPA with multiple `DataSource`s.
|
||||
* `query-by-example` - Example project showing usage of Query by Example with Spring Data JPA.
|
||||
* `security` - Example of how to integrate Spring Data JPA Repositories with Spring Security.
|
||||
* `showcase` - Refactoring show case of how to improve a plain-JPA-based persistence layer by using Spring Data JPA (read: removing close to all of the implementation code). Follow the `demo.txt` file for detailed instructions.
|
||||
* `showcase` - Refactoring show case of how to improve a plain-JPA-based persistence layer by using Spring Data JPA (read: removing close to all of the implementation code).Follow the `demo.txt` file for detailed instructions.
|
||||
* `vavr` - Shows the support of https://www.vavr.io[Vavr] collection types as return types for query methods.
|
||||
|
||||
== Spring Data LDAP
|
||||
@@ -72,6 +75,7 @@ Local Elasticsearch instance must be running to run the tests.
|
||||
|
||||
* `cluster` - Example for Redis Cluster support.
|
||||
* `example` - Example for basic Spring Data Redis setup.
|
||||
* `pubsub` - Example project to show Pub/Sub usage using Platform and Virtual Threads.
|
||||
* `reactive` - Example project to show reactive template support.
|
||||
* `repositories` - Example demonstrating Spring Data repository abstraction on top of Redis.
|
||||
* `sentinel` - Example for Redis Sentinel support.
|
||||
|
||||
@@ -16,9 +16,11 @@
|
||||
package example.springdata.jpa.simple;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableAsync
|
||||
class SimpleConfiguration {}
|
||||
|
||||
@@ -24,7 +24,7 @@ import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Slice;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.ListCrudRepository;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
|
||||
/**
|
||||
@@ -35,7 +35,7 @@ import org.springframework.scheduling.annotation.Async;
|
||||
* @author Thomas Darimont
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
public interface SimpleUserRepository extends CrudRepository<User, Long> {
|
||||
public interface SimpleUserRepository extends ListCrudRepository<User, Long> {
|
||||
|
||||
/**
|
||||
* Find the user with the given username. This method will be translated into a query using the
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright 2023 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
|
||||
*
|
||||
* http://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 example.springdata.jpa.simple;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledOnJre;
|
||||
import org.junit.jupiter.api.condition.JRE;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* Integration test showing the basic usage of {@link SimpleUserRepository} with Virtual Threads.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@Transactional
|
||||
@SpringBootTest(properties = "spring.threads.virtual.enabled=true")
|
||||
@EnabledOnJre(JRE.JAVA_21)
|
||||
class VirtualThreadsTests {
|
||||
|
||||
@Autowired SimpleUserRepository repository;
|
||||
private User user;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
|
||||
user = new User();
|
||||
user.setUsername("foobar");
|
||||
user.setFirstname("firstname");
|
||||
user.setLastname("lastname");
|
||||
}
|
||||
|
||||
/**
|
||||
* This repository invocation runs on a dedicated virtual thread.
|
||||
*/
|
||||
@Test
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
void supportsVirtualThreads() throws Exception {
|
||||
|
||||
BlockingQueue<String> thread = new LinkedBlockingQueue<>();
|
||||
repository.save(new User("Customer1", "Foo"));
|
||||
repository.save(new User("Customer2", "Bar"));
|
||||
|
||||
try (SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor()) {
|
||||
executor.setVirtualThreads(true);
|
||||
|
||||
var future = executor.submit(() -> {
|
||||
thread.add(Thread.currentThread().toString());
|
||||
return repository.findAll();
|
||||
});
|
||||
|
||||
List<User> users = future.get();
|
||||
String threadName = thread.poll(1, TimeUnit.SECONDS);
|
||||
|
||||
assertThat(threadName).contains("VirtualThread");
|
||||
assertThat(users).hasSize(2);
|
||||
}
|
||||
|
||||
repository.deleteAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Here we demonstrate the usage of {@link CompletableFuture} as a result wrapper for asynchronous repository query
|
||||
* methods running on Virtual Threads. Note, that we need to disable the surrounding transaction to be able to
|
||||
* asynchronously read the written data from another thread within the same test method.
|
||||
*/
|
||||
@Test
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
void asyncUsesVirtualThreads() throws Exception {
|
||||
|
||||
BlockingQueue<String> thread = new LinkedBlockingQueue<>();
|
||||
repository.save(new User("Customer1", "Foo"));
|
||||
repository.save(new User("Customer2", "Bar"));
|
||||
|
||||
var future = repository.readAllBy().thenAccept(users -> {
|
||||
|
||||
assertThat(users).hasSize(2);
|
||||
thread.add(Thread.currentThread().toString());
|
||||
});
|
||||
|
||||
future.join();
|
||||
String threadName = thread.poll(1, TimeUnit.SECONDS);
|
||||
|
||||
assertThat(threadName).contains("VirtualThread");
|
||||
|
||||
repository.deleteAll();
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@
|
||||
<modules>
|
||||
<module>cluster</module>
|
||||
<module>example</module>
|
||||
<module>pubsub</module>
|
||||
<module>reactive</module>
|
||||
<module>repositories</module>
|
||||
<module>sentinel</module>
|
||||
|
||||
4
redis/pubsub/README.md
Normal file
4
redis/pubsub/README.md
Normal file
@@ -0,0 +1,4 @@
|
||||
# Spring Data Redis Pub/Sub Example
|
||||
|
||||
This project contains samples of specific features of Spring Data Redis.
|
||||
|
||||
27
redis/pubsub/pom.xml
Normal file
27
redis/pubsub/pom.xml
Normal file
@@ -0,0 +1,27 @@
|
||||
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-data-redis-pubsub</artifactId>
|
||||
<name>Spring Data Redis - Pub/Sub</name>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.data.examples</groupId>
|
||||
<artifactId>spring-data-redis-examples</artifactId>
|
||||
<version>2.0.0.BUILD-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>spring-data-redis-example-utils</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright 2023 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
|
||||
*
|
||||
* http://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 example.springdata.redis;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
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.StringRedisSerializer;
|
||||
|
||||
/**
|
||||
* Show usage of Redis Pub/Sub operations.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@SpringBootTest
|
||||
public class PubSubTests {
|
||||
|
||||
@Autowired RedisConnectionFactory connectionFactory;
|
||||
|
||||
@Autowired StringRedisTemplate redisTemplate;
|
||||
|
||||
@Test
|
||||
void shouldListenToPubSubEvents() throws Exception {
|
||||
|
||||
BlockingQueue<String> events = new LinkedBlockingDeque<>();
|
||||
|
||||
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||
container.setConnectionFactory(connectionFactory);
|
||||
container.afterPropertiesSet();
|
||||
container.addMessageListener(
|
||||
(message, pattern) -> events.add(String.format("%s@%s", new String(message.getBody()), new String(pattern))),
|
||||
ChannelTopic.of("my-channel"));
|
||||
|
||||
container.start();
|
||||
|
||||
redisTemplate.convertAndSend("my-channel", "Hello, world!");
|
||||
|
||||
String event = events.poll(5, TimeUnit.SECONDS);
|
||||
|
||||
container.stop();
|
||||
container.destroy();
|
||||
|
||||
assertThat(event).isEqualTo("Hello, world!@my-channel");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotifyListener() throws Exception {
|
||||
|
||||
BlockingQueue<String> events = new LinkedBlockingDeque<>();
|
||||
|
||||
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||
container.setConnectionFactory(connectionFactory);
|
||||
container.afterPropertiesSet();
|
||||
|
||||
MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(new MyListener(events));
|
||||
messageListenerAdapter.afterPropertiesSet();
|
||||
messageListenerAdapter.setSerializer(StringRedisSerializer.UTF_8);
|
||||
container.addMessageListener(messageListenerAdapter, ChannelTopic.of("my-channel"));
|
||||
|
||||
container.start();
|
||||
|
||||
redisTemplate.convertAndSend("my-channel", "Hello, world!");
|
||||
|
||||
String event = events.poll(5, TimeUnit.SECONDS);
|
||||
|
||||
container.stop();
|
||||
container.destroy();
|
||||
|
||||
assertThat(event).isEqualTo("Hello, world!@my-channel");
|
||||
}
|
||||
|
||||
static class MyListener {
|
||||
private final Collection<String> events;
|
||||
|
||||
public MyListener(Collection<String> events) {
|
||||
this.events = events;
|
||||
}
|
||||
|
||||
public void handleMessage(String message, String channel) {
|
||||
events.add(String.format("%s@%s", message, channel));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 2023 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
|
||||
*
|
||||
* http://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 example.springdata.redis;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledOnJre;
|
||||
import org.junit.jupiter.api.condition.JRE;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.core.task.AsyncTaskExecutor;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.listener.ChannelTopic;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
|
||||
/**
|
||||
* Show usage of Redis Pub/Sub operations using Virtual Threads.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@SpringBootTest(properties = "spring.threads.virtual.enabled=true")
|
||||
@EnabledOnJre(JRE.JAVA_21)
|
||||
public class PubSubVirtualThreadsTests {
|
||||
|
||||
@Autowired RedisConnectionFactory connectionFactory;
|
||||
|
||||
@Autowired AsyncTaskExecutor taskExecutor;
|
||||
|
||||
@Autowired StringRedisTemplate redisTemplate;
|
||||
|
||||
@Test
|
||||
void shouldListenToPubSubEvents() throws Exception {
|
||||
|
||||
BlockingQueue<String> events = new LinkedBlockingDeque<>();
|
||||
|
||||
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||
container.setConnectionFactory(connectionFactory);
|
||||
container.setTaskExecutor(taskExecutor);
|
||||
container.afterPropertiesSet();
|
||||
container.addMessageListener(
|
||||
(message, pattern) -> events
|
||||
.add(String.format("%s on Thread %s", new String(message.getBody()), Thread.currentThread())),
|
||||
ChannelTopic.of("my-channel"));
|
||||
|
||||
container.start();
|
||||
|
||||
redisTemplate.convertAndSend("my-channel", "Hello, world!");
|
||||
|
||||
String event = events.poll(5, TimeUnit.SECONDS);
|
||||
|
||||
container.stop();
|
||||
container.destroy();
|
||||
|
||||
assertThat(event).isNotNull().contains("Hello, world!").contains("VirtualThread");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2014-2021 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 example.springdata.redis;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class RedisTestConfiguration {}
|
||||
1
redis/pubsub/src/test/resources/application.properties
Normal file
1
redis/pubsub/src/test/resources/application.properties
Normal file
@@ -0,0 +1 @@
|
||||
logging.level.root=WARN
|
||||
Reference in New Issue
Block a user