执行 Kafka 回调方法

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

Execute a method Kafka Callback

问题

我有一个RestController,调用KafkaSendMethod()将消息发送到Kafka消息队列。

@RestController
@RequestMapping("string-rest")
public class SimpleBookRestController {

    @Autowired
    private KafkaSendClass kafkaSendClass;

    @GetMapping(produces = "application/json")
    public void postMethod() {

        ArrayList<String> gfg = new ArrayList<String>(); 
  
        gfg.add("a"); 
        gfg.add("b"); 
        gfg.add("c");
    	
    	kafkaSendClass.KafkaSendMethod(gfg);
    }
}

这是发送消息到Kafka的类。

@Service
class KafkaSendClass {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    void KafkaSendMethod(List<String> strList) {
		
		for (String str : strList) {
            ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send(str, str);
            future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
        
                @Override
                public void onSuccess(SendResult<String, String> result) {
                    syso("sent success");
                    m1(); //每次调用KafkaSendMethod()都只调用一次m1();
                }
        
                @Override
                public void onFailure(Throwable ex) {
                    System.out.println("sending failed");
                }
            });
        }
    }
}

所以我的问题是,如何确保每次调用KafkaSendMethod(List strList)时只调用一次m1()方法。

英文:

I have a RestController which calls KafkaSendMethod() to send message to Kafka MQ

 @RestController
@RequestMapping(&quot;string-rest&quot;)
public class SimpleBookRestController {

   @Autowire
    private KafkaSendClass kafkaSendClass;
    
    @GetMapping( produces = &quot;application/json&quot;)
    public void postMethod() {
	
	ArrayList&lt;String&gt; gfg = new ArrayList&lt;String&gt;(); 

    gfg.add(&quot;a&quot;); 
    gfg.add(&quot;b&quot;); 
    gfg.add(&quot;c&quot;);
	
	kafkaSendClass.KafkaSendMethod(gfg);
	
	}

Here is the class which sends message to Kafka

@Service
    class KafkaSendClass
    {
    
    @Autowired
private KafkaTemplate&lt;String, String&gt; kafkaTemplate;


    void KafkaSendMethod(List&lt;Strings&gt; strList)
    {
	
	for ( String str : strList )
	(
    ListenableFuture&lt;SendResult&lt;String,String&gt;&gt;   future = kafkaTemplate.send(str , str );
    future.addCallback(new ListenableFutureCallback&lt;SendResult&lt;String, String&gt;&gt;() {
    
        @Override
        public void onSuccess(SendResult&lt;String, String&gt; result) {
            syso(&quot;sent success&quot;);
    		m1(); //call only once for one call to KafkaSendMethod() ;
        }
    
        @Override
        public void onFailure(Throwable ex) {
            System.out.println(&quot; sending failed&quot;);
        }
    });

);
So my question is how do i call m1() once for every call to KafkaSendMethod(List<String> strList) call.

答案1

得分: 1

尝试使用 CompletableFuture.allOf(...)

将这些 Future 收集到一个列表中,并使用它们的 completable() 适配。然后对该 CompletableFuture 调用 List.toArray()。使用 CompletableToListenableFutureAdapter 添加你的最终回调。

英文:

See if you can use a CompletableFuture.allOf(...).

Gather those Futures into a list and use their completable() adaptation. Then call List.toArray() for that CompletableFuture. The CompletableToListenableFutureAdapter to add your final callback.

huangapple
  • 本文由 发表于 2020年9月14日 23:46:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63887651.html
匿名

发表评论

匿名网友

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

确定