org.springframework.kafka.listener.AbstractMessageListenerContainer Java Examples

The following examples show how to use org.springframework.kafka.listener.AbstractMessageListenerContainer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KafkaConfigurer.java    From bird-java with MIT License 6 votes vote down vote up
@Bean
@ConditionalOnProperty(value = EventbusConstant.Kafka.LISTENER_PACKAGES)
public KafkaMessageListenerContainer kafkaListenerContainer(EventDispatcher eventDispatcher) {

    KafkaEventArgListener listener = new KafkaEventArgListener(eventDispatcher);
    ContainerProperties containerProperties = new ContainerProperties(eventDispatcher.getAllTopics());
    containerProperties.setMessageListener(listener);
    containerProperties.setAckMode(AbstractMessageListenerContainer.AckMode.MANUAL_IMMEDIATE);

    HashMap<String,Object> properties = new HashMap<>(8);
    properties.put("bootstrap.servers", kafkaProperties.getHost());

    KafkaListenerProperties listenerProperties = kafkaProperties.getListener();
    properties.put("group.id", listenerProperties.getGroupId());
    properties.put("auto.offset.reset", "earliest");
    properties.put("enable.auto.commit", false);
    properties.put("auto.commit.interval.ms", 1000);
    properties.put("session.timeout.ms", 15000);
    properties.put("key.deserializer", StringDeserializer.class);
    properties.put("value.deserializer", EventArgDeserializer.class);
    DefaultKafkaConsumerFactory<String,EventArg> consumerFactory = new DefaultKafkaConsumerFactory<>(properties);

    return new KafkaMessageListenerContainer<>(consumerFactory, containerProperties);
}
 
Example #2
Source File: KafkaMessageChannelBinder.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
public KafkaMessageChannelBinder(
		KafkaBinderConfigurationProperties configurationProperties,
		KafkaTopicProvisioner provisioningProvider,
		ListenerContainerCustomizer<AbstractMessageListenerContainer<?, ?>> containerCustomizer,
		KafkaBindingRebalanceListener rebalanceListener) {

	this(configurationProperties, provisioningProvider, containerCustomizer, null, rebalanceListener, null);
}
 
Example #3
Source File: KafkaMessageChannelBinder.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
public KafkaMessageChannelBinder(
		KafkaBinderConfigurationProperties configurationProperties,
		KafkaTopicProvisioner provisioningProvider,
		ListenerContainerCustomizer<AbstractMessageListenerContainer<?, ?>> containerCustomizer,
		MessageSourceCustomizer<KafkaMessageSource<?, ?>> sourceCustomizer,
		KafkaBindingRebalanceListener rebalanceListener,
		DlqPartitionFunction dlqPartitionFunction) {

	super(headersToMap(configurationProperties), provisioningProvider,
			containerCustomizer, sourceCustomizer);
	this.configurationProperties = configurationProperties;
	String txId = configurationProperties.getTransaction().getTransactionIdPrefix();
	if (StringUtils.hasText(txId)) {
		this.transactionManager = new KafkaTransactionManager<>(getProducerFactory(
				txId, new ExtendedProducerProperties<>(configurationProperties
						.getTransaction().getProducer().getExtension()), txId + ".producer"));
		this.transactionTemplate = new TransactionTemplate(this.transactionManager);
	}
	else {
		this.transactionManager = null;
		this.transactionTemplate = null;
	}
	this.rebalanceListener = rebalanceListener;
	this.dlqPartitionFunction = dlqPartitionFunction != null
			? dlqPartitionFunction
			: null;
}
 
Example #4
Source File: ConsumerProducerTransactionTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@Bean
public ListenerContainerCustomizer<AbstractMessageListenerContainer<?, ?>> customizer() {
	return (container, dest, group) -> {
		container.setAfterRollbackProcessor(new DefaultAfterRollbackProcessor<>(new FixedBackOff(0L, 1L)));
		if ("input2".equals(dest)) {
			this.input2Container = container;
		}
	};
}
 
Example #5
Source File: ProcessorApplication.java    From spring-cloud-stream-samples with Apache License 2.0 5 votes vote down vote up
@Bean
public ListenerContainerCustomizer<AbstractMessageListenerContainer<byte[], byte[]>> customizer() {
    // Disable retry in the AfterRollbackProcessor
    return (container, destination, group) -> container.setAfterRollbackProcessor(
            new DefaultAfterRollbackProcessor<byte[], byte[]>(
                    (record, exception) -> System.out.println("Discarding failed record: " + record),
                    new FixedBackOff(0L, 0)));
}
 
Example #6
Source File: TraceMessagingAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Around("anyCreateListenerContainer() || anyCreateContainer()")
public Object wrapListenerContainerCreation(ProceedingJoinPoint pjp)
		throws Throwable {
	MessageListenerContainer listener = (MessageListenerContainer) pjp.proceed();
	if (listener instanceof AbstractMessageListenerContainer) {
		AbstractMessageListenerContainer container = (AbstractMessageListenerContainer) listener;
		Object someMessageListener = container.getContainerProperties()
				.getMessageListener();
		if (someMessageListener == null) {
			if (log.isDebugEnabled()) {
				log.debug("No message listener to wrap. Proceeding");
			}
		}
		else if (someMessageListener instanceof MessageListener) {
			container.setupMessageListener(createProxy(someMessageListener));
		}
		else {
			if (log.isDebugEnabled()) {
				log.debug("ATM we don't support Batch message listeners");
			}
		}
	}
	else {
		if (log.isDebugEnabled()) {
			log.debug("Can't wrap this listener. Proceeding");
		}
	}
	return listener;
}
 
Example #7
Source File: KafkaBinderActuatorTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 4 votes vote down vote up
@Bean
public ListenerContainerCustomizer<AbstractMessageListenerContainer<?, ?>> containerCustomizer() {
	return (c, q, g) -> c.setBeanName("setByCustomizer:" + q);
}
 
Example #8
Source File: KafkaBinderTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testManualAckIsNotPossibleWhenAutoCommitOffsetIsEnabledOnTheBinder()
		throws Exception {
	Binder binder = getBinder();

	DirectChannel moduleOutputChannel = createBindableChannel("output",
			createProducerBindingProperties(createProducerProperties()));
	QueueChannel moduleInputChannel = new QueueChannel();

	Binding<MessageChannel> producerBinding = binder.bindProducer(
			"testManualAckIsNotPossibleWhenAutoCommitOffsetIsEnabledOnTheBinder",
			moduleOutputChannel, createProducerProperties());

	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();

	Binding<MessageChannel> consumerBinding = binder.bindConsumer(
			"testManualAckIsNotPossibleWhenAutoCommitOffsetIsEnabledOnTheBinder",
			"test", moduleInputChannel, consumerProperties);

	AbstractMessageListenerContainer<?, ?> container = TestUtils.getPropertyValue(
			consumerBinding, "lifecycle.messageListenerContainer",
			AbstractMessageListenerContainer.class);
	assertThat(container.getContainerProperties().getAckMode())
			.isEqualTo(ContainerProperties.AckMode.BATCH);

	String testPayload1 = "foo" + UUID.randomUUID().toString();
	Message<?> message1 = org.springframework.integration.support.MessageBuilder
			.withPayload(testPayload1.getBytes()).build();

	// Let the consumer actually bind to the producer before sending a msg
	binderBindUnbindLatency();
	moduleOutputChannel.send(message1);

	Message<?> receivedMessage = receive(moduleInputChannel);
	assertThat(receivedMessage).isNotNull();
	assertThat(receivedMessage.getHeaders().get(KafkaHeaders.ACKNOWLEDGMENT))
			.isNull();

	producerBinding.unbind();
	consumerBinding.unbind();
}
 
Example #9
Source File: KafkaBinderTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 4 votes vote down vote up
@Test
@Override
@SuppressWarnings("unchecked")
public void testTwoRequiredGroups() throws Exception {
	Binder binder = getBinder();
	ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties();

	DirectChannel output = createBindableChannel("output",
			createProducerBindingProperties(producerProperties));

	String testDestination = "testDestination"
			+ UUID.randomUUID().toString().replace("-", "");

	producerProperties.setRequiredGroups("test1", "test2");
	Binding<MessageChannel> producerBinding = binder.bindProducer(testDestination,
			output, producerProperties);

	String testPayload = "foo-" + UUID.randomUUID().toString();
	output.send(new GenericMessage<>(testPayload.getBytes()));

	QueueChannel inbound1 = new QueueChannel();
	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();
	consumerProperties.getExtension().setAutoRebalanceEnabled(false);
	consumerProperties.getExtension().setAckEachRecord(true);
	Binding<MessageChannel> consumerBinding1 = binder.bindConsumer(testDestination,
			"test1", inbound1, consumerProperties);
	QueueChannel inbound2 = new QueueChannel();
	Binding<MessageChannel> consumerBinding2 = binder.bindConsumer(testDestination,
			"test2", inbound2, consumerProperties);

	AbstractMessageListenerContainer<?, ?> container = TestUtils.getPropertyValue(
			consumerBinding2, "lifecycle.messageListenerContainer",
			AbstractMessageListenerContainer.class);
	assertThat(container.getContainerProperties().getAckMode())
			.isEqualTo(ContainerProperties.AckMode.RECORD);

	Message<?> receivedMessage1 = receive(inbound1);
	assertThat(receivedMessage1).isNotNull();
	assertThat(new String((byte[]) receivedMessage1.getPayload(),
			StandardCharsets.UTF_8)).isEqualTo(testPayload);
	Message<?> receivedMessage2 = receive(inbound2);
	assertThat(receivedMessage2).isNotNull();
	assertThat(new String((byte[]) receivedMessage2.getPayload(),
			StandardCharsets.UTF_8)).isEqualTo(testPayload);

	consumerBinding1.unbind();
	consumerBinding2.unbind();
	producerBinding.unbind();
}