Feature - Helidon CDI integration. #3649
parent
feb76ce553
commit
89f22762b3
@ -0,0 +1,54 @@
|
||||
# Helidon CDI extension for Redis
|
||||
|
||||
Integrates Redisson with [Helidon](https://helidon.io/) framework.
|
||||
|
||||
Supports Helidon 1.4.x - 2.3.x
|
||||
|
||||
## Usage
|
||||
|
||||
### 1. Add `redisson-helidon` dependency into your project:
|
||||
|
||||
Maven
|
||||
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson-helidon</artifactId>
|
||||
<version>3.16.0</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
Gradle
|
||||
|
||||
```groovy
|
||||
compile 'org.redisson:redisson-helidon:3.16.0'
|
||||
```
|
||||
|
||||
### 2. Add settings into `META-INF/microprofile-config.properties` file
|
||||
|
||||
Config structure is a flat Redisson YAML configuration -
|
||||
[single mode](https://github.com/redisson/redisson/wiki/2.-Configuration#262-single-instance-yaml-config-format),
|
||||
[replicated mode](https://github.com/redisson/redisson/wiki/2.-Configuration#252-replicated-yaml-config-format),
|
||||
[cluster mode](https://github.com/redisson/redisson/wiki/2.-Configuration#242-cluster-yaml-config-format),
|
||||
[sentinel mode](https://github.com/redisson/redisson/wiki/2.-Configuration#272-sentinel-yaml-config-format),
|
||||
[proxy mode](https://github.com/redisson/redisson/wiki/2.-Configuration#292-proxy-mode-yaml-config-format)
|
||||
|
||||
Below is the configuration for Redisson instance named `simple`.
|
||||
```
|
||||
org.redisson.Redisson.simple.singleServerConfig.address=redis://127.0.0.1:6379
|
||||
org.redisson.Redisson.simple.singleServerConfig.connectionPoolSize=64
|
||||
org.redisson.Redisson.simple.threads=16
|
||||
org.redisson.Redisson.simple.nettyThreads=32
|
||||
```
|
||||
|
||||
### 3. Use Redisson
|
||||
|
||||
```java
|
||||
@Inject
|
||||
@Named("simple")
|
||||
private RedissonClient redisson;
|
||||
```
|
||||
|
||||
For injection without @Named annotation use instance name - `default`.
|
||||
|
||||
Consider __[Redisson PRO](https://redisson.pro)__ version for **ultra-fast performance** and **support by SLA**.
|
@ -0,0 +1,135 @@
|
||||
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson-parent</artifactId>
|
||||
<version>3.15.7-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>redisson-helidon</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Redisson/Helidon integration</name>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>com.mycila</groupId>
|
||||
<artifactId>license-maven-plugin</artifactId>
|
||||
<version>3.0</version>
|
||||
<configuration>
|
||||
<basedir>${basedir}</basedir>
|
||||
<header>${basedir}/../header.txt</header>
|
||||
<quiet>false</quiet>
|
||||
<failIfMissing>true</failIfMissing>
|
||||
<aggregate>false</aggregate>
|
||||
<includes>
|
||||
<include>src/main/java/org/redisson/</include>
|
||||
</includes>
|
||||
<excludes>
|
||||
<exclude>target/**</exclude>
|
||||
</excludes>
|
||||
<useDefaultExcludes>true</useDefaultExcludes>
|
||||
<mapping>
|
||||
<java>JAVADOC_STYLE</java>
|
||||
</mapping>
|
||||
<strictCheck>true</strictCheck>
|
||||
<useDefaultMapping>true</useDefaultMapping>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>check</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>jakarta.enterprise</groupId>
|
||||
<artifactId>jakarta.enterprise.cdi-api</artifactId>
|
||||
<version>2.0.2</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jboss</groupId>
|
||||
<artifactId>jandex</artifactId>
|
||||
<version>2.3.0.Final</version>
|
||||
<scope>runtime</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.helidon.microprofile.config</groupId>
|
||||
<artifactId>helidon-microprofile-config</artifactId>
|
||||
<version>[1.4.0,)</version>
|
||||
<scope>runtime</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.eclipse.microprofile.config</groupId>
|
||||
<artifactId>microprofile-config-api</artifactId>
|
||||
<version>1.4</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.helidon.microprofile.cdi</groupId>
|
||||
<artifactId>helidon-microprofile-cdi</artifactId>
|
||||
<version>[1.4.0,)</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>5.7.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-params</artifactId>
|
||||
<version>5.7.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.micronaut.test</groupId>
|
||||
<artifactId>micronaut-test-junit5</artifactId>
|
||||
<version>2.0.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.2.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>3.18.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,90 @@
|
||||
/**
|
||||
* Copyright (c) 2013-2021 Nikita Koksharov
|
||||
*
|
||||
* 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 org.redisson.helidon;
|
||||
|
||||
import org.eclipse.microprofile.config.Config;
|
||||
import org.redisson.Redisson;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.config.PropertiesConvertor;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.enterprise.event.Observes;
|
||||
import javax.enterprise.inject.spi.*;
|
||||
import javax.inject.Named;
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Nikita Koksharov
|
||||
*
|
||||
*/
|
||||
public class RedissonExtension implements Extension {
|
||||
|
||||
private final Set<Annotation> qualifiers = new HashSet<>();
|
||||
|
||||
private <T extends RedissonClient> void processRedissonInjectionPoint(@Observes ProcessInjectionPoint<?, T> point) {
|
||||
if (point == null) {
|
||||
return;
|
||||
}
|
||||
InjectionPoint injectionPoint = point.getInjectionPoint();
|
||||
if (injectionPoint == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
qualifiers.addAll(injectionPoint.getQualifiers());
|
||||
}
|
||||
|
||||
private void addBeans(@Observes AfterBeanDiscovery discovery, BeanManager beanManager) {
|
||||
if (discovery == null || beanManager == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Annotation qualifier : qualifiers) {
|
||||
Set<Annotation> qualifiers = Collections.singleton(qualifier);
|
||||
|
||||
discovery.addBean()
|
||||
.scope(ApplicationScoped.class)
|
||||
.addQualifiers(qualifiers)
|
||||
.addTransitiveTypeClosure(RedissonClient.class)
|
||||
.produceWith((instance) -> {
|
||||
|
||||
String instanceName = "default";
|
||||
if (qualifier instanceof Named) {
|
||||
instanceName = ((Named) qualifier).value();
|
||||
}
|
||||
|
||||
Config cfg = instance.select(Config.class).get();
|
||||
String yamlConfig = PropertiesConvertor.toYaml(Redisson.class.getName() + "." + instanceName + ".",
|
||||
cfg.getPropertyNames(), prop -> {
|
||||
return cfg.getValue(prop, String.class);
|
||||
});
|
||||
|
||||
try {
|
||||
org.redisson.config.Config config = org.redisson.config.Config.fromYAML(yamlConfig);
|
||||
return Redisson.create(config);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
|
||||
bean-discovery-mode="annotated" version="2.0">
|
||||
</beans>
|
@ -0,0 +1 @@
|
||||
org.redisson.helidon.RedissonExtension
|
@ -0,0 +1,51 @@
|
||||
package org.redisson;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.redisson.api.RBucket;
|
||||
import org.redisson.api.RedissonClient;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.enterprise.context.Initialized;
|
||||
import javax.enterprise.event.Observes;
|
||||
import javax.enterprise.inject.Instance;
|
||||
import javax.enterprise.inject.literal.NamedLiteral;
|
||||
import javax.enterprise.inject.se.SeContainerInitializer;
|
||||
import javax.enterprise.inject.spi.CDI;
|
||||
import javax.inject.Named;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Nikita Koksharov
|
||||
*
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class RedissonExtensionTest {
|
||||
|
||||
@BeforeEach
|
||||
void startCdiContainer() {
|
||||
System.setProperty("org.redisson.Redisson.simple.singleServerConfig.address", "redis://127.0.0.1:6379");
|
||||
SeContainerInitializer initializer = SeContainerInitializer.newInstance();
|
||||
initializer.initialize();
|
||||
}
|
||||
|
||||
private void onInit(@Observes @Initialized(ApplicationScoped.class) Object event,
|
||||
@Named("simple") RedissonClient client) {
|
||||
assertThat(client).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Instance<RedissonClient> instance = CDI.current().select(RedissonClient.class, NamedLiteral.of("simple"));
|
||||
RedissonClient redissonClient = instance.get();
|
||||
|
||||
RBucket<String> b = redissonClient.getBucket("test");
|
||||
b.set("1");
|
||||
assertThat(b.get()).isEqualTo("1");
|
||||
assertThat(b.delete()).isTrue();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
|
||||
bean-discovery-mode="annotated" version="2.0">
|
||||
</beans>
|
@ -0,0 +1 @@
|
||||
mp.initializer.allow=true
|
Loading…
Reference in New Issue