Implementation:APIJSON APIJSON Demo RedisDataSource
| Knowledge Sources | |
|---|---|
| Domains | Database, Redis, Connection_Pooling |
| Last Updated | 2026-10-14 00:00 GMT |
Overview
Concrete tool for managing Jedis connection pools (single, sentinel, cluster) for Redis data source access in the APIJSON Redis demo.
Description
The RedisDataSource class manages three Jedis connection pool types: JedisPool (single node), JedisSentinelPool (sentinel mode), and JedisCluster (cluster mode). It configures pool parameters (maxTotal, maxIdle, maxWaitMillis, testOnBorrow, etc.) and provides factory methods (createSingle, createSentinel, createCluster) to initialize each pool type. It uses Lombok @Data for getters/setters.
Usage
Use this class when you need to initialize and manage Redis connections (single, sentinel, or cluster) in the APIJSON Redis multi-datasource demo.
Code Reference
Source Location
- Repository: APIJSON_APIJSON_Demo
- File: APIJSON-Java-Server/APIJSONDemo-MultiDataSource-Redis/src/main/java/apijson/demo/redis/RedisDataSource.java
- Lines: 1-120
Signature
@Data
@Slf4j
public class RedisDataSource {
private JedisCluster jedisCluster;
private JedisPool jedisPool;
private JedisSentinelPool sentinelPool;
public void createSingle(); // single node pool
public void createSentinel(); // sentinel pool
public void createCluster(); // cluster pool
}
Import
import apijson.demo.redis.RedisDataSource;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| MAX_TOTAL | Integer | No | Max connections (default 1024) |
| MAX_IDLE | Integer | No | Max idle connections (default 200) |
| TIMEOUT | Integer | No | Connection timeout ms (default 10000) |
Outputs
| Name | Type | Description |
|---|---|---|
| jedisPool | JedisPool | Single-node connection pool |
| sentinelPool | JedisSentinelPool | Sentinel-mode pool |
| jedisCluster | JedisCluster | Cluster-mode pool |
Usage Examples
Create Single Node Pool
import apijson.demo.redis.RedisDataSource;
RedisDataSource ds = new RedisDataSource();
ds.createSingle(); // initializes jedisPool on localhost:6379
// Use pool
try (var jedis = ds.getJedisPool().getResource()) {
jedis.set("key", "value");
}