{{announcement.body}}
{{announcement.title}}

Reactive Spring Transactions in Redis

DZone 's Guide to

Reactive Spring Transactions in Redis

Learn how Redisson supports reactive Spring transactions.

· Java Zone ·
Free Resource

Speaking of transactions...

Spring is an open-source, lightweight Java application framework that simplifies the process of developing enterprise software for the Java programming language. The Spring framework includes many core features that promote good software development practices, such as dependency injection and a model-view-controller (MVC) architectural pattern.

Reactive programming is a declarative programming paradigm that works with asynchronous data streams in order to respond to real-time events and build scalable, resilient, concurrent applications. Beginning with Spring Framework 5.2 M2, Spring includes support for reactive transaction management.

Spring includes a Spring Data Redis module for compatibility with Redis, an in-memory data structure store often used to build NoSQL databases. However, Spring doesn't provide support for Redis-based transactions out of the box. For this reason, many Java developers use a third-party Java Redis framework like Redisson to perform reactive Spring transactions in Redis.

Looking for more things to do with Spring? Check out our curated collection of Spring Framework tutorials.

Redisson: Reactive Spring Transactions in Redis

Redisson is a third-party open-source Redis Java client. By including distributed implementations of many common Java objects and collections, Redisson dramatically simplifies the process of using Java with Redis.

Redisson offers support for reactive Spring transactions in Redis via interfaces such as RedissonReactiveClient, RTransactionReactive, and ReactiveRedissonTransactionManager. To understand how these all work together, below is an example of how to do reactive Spring transactions in Redis using Redisson:

Java




xxxxxxxxxx
1
38


 
1
public class TransactionalBean {
2
 
          
3
    @Autowired
4
    private ReactiveRedissonTransactionManager transactionManager;
5
 
          
6
    @Transactional
7
    public Mono<Void> commitData() {
8
        Mono<RTransactionReactive> transaction = transactionManager.getCurrentTransaction();
9
        return transaction.flatMap(t -> {
10
            RMapReactive<String, String> map = t.getMap("myMap");
11
            return map.put("1", "2");
12
        }).then();
13
    }
14
 
          
15
}
16
 
          
17
 
          
18
@Configuration
19
@EnableTransactionManagement
20
public class RedissonReactiveTransactionContextConfig {
21
     
22
     @Bean
23
     public TransactionalBean transactionBean() {
24
         return new TransactionalBean();
25
     }
26
     
27
     @Bean
28
     public ReactiveRedissonTransactionManager transactionManager(RedissonReactiveClient redisson) {
29
         return new ReactiveRedissonTransactionManager(redisson);
30
     }
31
     
32
     @Bean(destroyMethod="shutdown")
33
     public RedissonReactiveClient redisson(@Value("classpath:/redisson.yaml") Resource configFile) throws IOException {
34
          Config config = Config.fromYAML(configFile.getInputStream());
35
         return Redisson.createReactive(config);
36
     }
37
     
38
}



Reactive Spring transactions in Redis are just one example of how developers can implement reactive programming, Java transactions, and the Spring framework using Redisson. The Spring framework is fully compatible with Redis, thanks to the Redisson Java client for Redis.

To get started using Spring with Redisson, check out the Configuration section in the Redisson project wiki. For more information about reactive programming and reactive Spring transactions in Redis, check out the Redisson website.

Further Reading

Using Spring Data Redis in Spring Boot 2.0 to Write a Custom Query

Implementation of Redis in a Microservice/Spring Boot Application

Topics:
redis ,java ,reactive api ,transactions in redis ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}