Spring Boot ReactiveRedis自定义注解,用于订阅频道并获取数据。

huangapple go评论50阅读模式
英文:

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&lt;String, Object&gt; reactiveRedisTemplate;
 
@PostConstruct
	public void init(){
		System.out.println(&quot;*****SampleLoader***** initialized - SampleLoader&quot;);

		this.reactiveRedisTemplate.listenTo(ChannelTopic.of(&quot;some-topic&quot;)).subscribe(data -&gt; {
				System.out.println(
						&quot;data.getChannel():-&quot; + data.getChannel() + &quot;:&quot; + &quot;data.getMessage():-&quot; + 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(&quot;some-topic&quot;)
        public void redisData(String channel, Object data){
        
        }

@RedisListener will subscribe to the topic and return the call-back data.

答案1

得分: 1

  1. 通过扫描获取添加注解 RedisListener 的所有方法。

  2. 获取注解的值,并通过 reactiveRedisTemplate 监听这些主题。

  3. 在回调方法中通过反射调用方法。

英文:
  1. Obtain all methods of adding annotation RedisListener through scanning

  2. Get the values of the annotation, and listen to these topics through the reactiveRedisTemplate

  3. Call the method through reflection in the callback method

huangapple
  • 本文由 发表于 2023年2月6日 19:14:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定