To create the integration with the third-party system, for which you design the plug-in, you must add the Maven dependencies.
You introduce a dependency with a small Java-based client called Jedis and the Apache common pool library, on which the Jedis library depends.
Procedure
- Define the dependency in the main pom.xml file.
... <properties> <jedis.version>3.6.1</jedis.version> <commons.pool.version>2.10.0</commons.pool.version> </properties> ... <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${jedis.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>${commons.pool.version}</version> </dependency>
- Add a dependency in the pom.xml file in the o11nplugin-redis-core folder.
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency>
- Define the use of Jedis.
//Construct a pool of redis connections JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost"); /// Jedis implements Closable. Hence, the jedis instance will be auto-closed after the last statement. try (Jedis jedis = pool.getResource()) { /// ... do stuff here ... for example jedis.set("foo", "bar"); String foobar = jedis.get("foo"); jedis.zadd("sose", 0, "car"); jedis.zadd("sose", 0, "bike"); Set<String> sose = jedis.zrange("sose", 0, -1); } /// ... when closing your application: pool.destroy();
Multiple threads, such as workflows and user interactions, access theConnection
class. You must make sure that you have either a pool of Redis connections, which is provided by default, or a thread-safe connection.- When you create the
Connection
object, you initialize theJedisPool
. - Retrieve the Jedis object in the try-statement as a resource.
- Close the resource by returning it to the
JedisPool
when it is not used anymore. - When the connection is destroyed, you must destroy the
JedisPool
.
@Component @Qualifier(value = "connection") @Scope(value = "prototype") public class Connection implements Findable { ... private JedisPool pool; @ExtensionMethod public String ping() { try (Jedis jedis = getResource()) { return jedis.ping(); } } @ExtensionMethod public String set(String key, String value) { try (Jedis jedis = getResource()) { return jedis.set(key, value); } } @ExtensionMethod public String get(String key) { try (Jedis jedis = getResource()) { return jedis.get(key); } } public synchronized void destroy() { if (pool != null) { pool.destroy(); } } /** * Returns a redis connection from the pool. */ public Jedis getResource() { return getPool().getResource(); } /* * Lazy initialization of the pool. */ private synchronized JedisPool getPool() { if (pool == null) { JedisPoolConfig jedisConfig = new JedisPoolConfig(); pool = new JedisPool(jedisConfig, connectionInfo.getHost(), connectionInfo.getPort()); } return pool; } ... }
Note: When you destroy a connection, you must destroy all related resources. To prevent a state of inconsistency, thedestroy
and thegetPool
methods are synchronized.You must explicitly close the Jedis connection. For example, if you use the
ping
method, you retrieve a connection from the pool, run a call and close the connection, by returning it to the pool. If you want to run multiple calls, you must retrieve a connection multiple times. For performance reasons, you can expose the Jedis object to the users. - When you create the