将BlockingQueue传递给Spring KafkaListener

时间:2018-09-17 18:17:49

标签: java spring apache-kafka listener spring-kafka

我是Java,Spring和Kafka的新手。情况如下:

我已经使用@KafkaListener批注制作了一个Kafka Consumer,看起来像这样:

public class Listener {

private ExecutorService executorService;
private List<Future> futuresThread1 = new ArrayList<>();
public Listener() {
    Properties appProps = new AppProperties().get();
    this.executorService = Executors.newFixedThreadPool(Integer.parseInt(appProps.getProperty("listenerThreads")));
}
//TODO: how can I pass an approp into this annotation?
@KafkaListener(id = "id0", topics = "bose.cdp.ingest.marge.boseaccount.normalized")
public void listener(ConsumerRecord<?, ?> record, ArrayBlockingQueue<ConsumerRecord> arrayBlockingQueue) throws InterruptedException, ExecutionException
    {
        futuresThread1.add(executorService.submit(new Runnable() {
                @Override public void run() {
                    System.out.println(record);
                    arrayBlockingQueue.add(record);
                }
        }));
    }

}

我向侦听器添加了一个参数ArrayBlockingQueue,我希望它可以将来自Kafka的消息添加到其中。

我遇到的问题是我无法弄清楚如何将ArrayBlockingQueue真正传递给侦听器,因为Spring正在后台处理侦听器的实例化和运行。

我需要这个阻塞队列,以便侦听器之外的另一个对象可以访问消息并对其进行一些处理。例如,在我的主目录中:

@SpringBootApplication
public class SourceAccountListenerApp {
    public static void main(String[] args) {
        Properties appProps = new AppProperties().get();
        ArrayBlockingQueue<ConsumerRecord> arrayBlockingQueue = new ArrayBlockingQueue<>(
           Integer.parseInt(appProps.getProperty("blockingQueueSize"))
        );
        //TODO: This starts my listener. How do I pass the queue to it?
        SpringApplication.run(SourceAccountListenerApp.class, args);
    }
}

1 个答案:

答案 0 :(得分:2)

有很多方法可以将阻塞队列声明为bean。

一个例子,主要:

@SpringBootApplication
public class SourceAccountListenerApp {
    public static void main(String[] args) {
        SpringApplication.run(SourceAccountListenerApp.class, args);
    }

    @Bean
    public ArrayBlockingQueue arrayBlockingQueue() {
        Properties appProps = new AppProperties().get();
        ArrayBlockingQueue<ConsumerRecord> arrayBlockingQueue = new ArrayBlockingQueue<>(
           Integer.parseInt(appProps.getProperty("blockingQueueSize"))
        );
        return arrayBlockingQueue;
    }
}

监听器:

public class Listener {

    @Autowired
    ArrayBlockingQueue arrayBlockingQueue;