英文:
Spring Boot ReactiveRedis custom annotation to subscribe the channel and get the data
问题
我正在使用Spring Boot ReactiveRedis来实现发布-订阅机制,以下是我的代码,它正常运行,我在订阅回调中获取数据。
@Autowired
private ReactiveRedisOperations<String, Object> reactiveRedisTemplate;
@PostConstruct
public void init(){
System.out.println("*****SampleLoader***** initialized - SampleLoader");
this.reactiveRedisTemplate.listenTo(ChannelTopic.of("some-topic")).subscribe(data -> {
System.out.println(
"data.getChannel():-" + data.getChannel() + ":" + "data.getMessage():-" + data.getMessage());
});
}
但我的问题是如何创建一个自定义注解,类似于
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@MessageMapping
@Documented
@Repeatable(RedisListeners.class)
public @interface RedisListener{
}
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RedisListeners {
RedisListener[] value();
}
@RedisListener("some-topic")
public void redisData(String channel, Object data){
}
@RedisListener
将订阅主题并返回回调数据。
英文:
I am working on spring boot ReactiveRedis for the pub-sub mechanism following is my code and it is working fine, I am getting data in subscribe call back.
@Autowired
private ReactiveRedisOperations<String, Object> reactiveRedisTemplate;
@PostConstruct
public void init(){
System.out.println("*****SampleLoader***** initialized - SampleLoader");
this.reactiveRedisTemplate.listenTo(ChannelTopic.of("some-topic")).subscribe(data -> {
System.out.println(
"data.getChannel():-" + data.getChannel() + ":" + "data.getMessage():-" + data.getMessage());
});
}
But my question is how to create a custom annotation like
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@MessageMapping
@Documented
@Repeatable(RedisListeners.class)
public @interface RedisListener{
}
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RedisListeners {
RedisListener[] value();
}
@RedisListener("some-topic")
public void redisData(String channel, Object data){
}
@RedisListener will subscribe to the topic and return the call-back data.
答案1
得分: 1
-
通过扫描获取添加注解 RedisListener 的所有方法。
-
获取注解的值,并通过 reactiveRedisTemplate 监听这些主题。
-
在回调方法中通过反射调用方法。
英文:
-
Obtain all methods of adding annotation RedisListener through scanning
-
Get the values of the annotation, and listen to these topics through the reactiveRedisTemplate
-
Call the method through reflection in the callback method
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论