英文:
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
英文:
I have a RestController which calls KafkaSendMethod() to send message to Kafka MQ
 @RestController
@RequestMapping("string-rest")
public class SimpleBookRestController {
   @Autowire
    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);
	
	}
Here is the class which sends message to Kafka
@Service
    class KafkaSendClass
    {
    
    @Autowired
private KafkaTemplate<String, String> kafkaTemplate;
    void KafkaSendMethod(List<Strings> 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(); //call only once for one call to KafkaSendMethod() ;
        }
    
        @Override
        public void onFailure(Throwable ex) {
            System.out.println(" sending failed");
        }
    });
);
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论