Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable the use of a custom ObjectMapper #735

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import com.fasterxml.jackson.databind.ObjectMapper;
import reactor.core.publisher.Flux;

/**
Expand All @@ -71,6 +72,8 @@ public class MethodReactivePulsarListenerEndpoint<V> extends AbstractReactivePul

private Method method;

private ObjectMapper objectMapper;

private MessageHandlerMethodFactory messageHandlerMethodFactory;

private SmartMessageConverter messagingConverter;
Expand Down Expand Up @@ -212,10 +215,12 @@ protected AbstractPulsarMessageToSpringMessageAdapter<V> createMessageListenerIn

AbstractPulsarMessageToSpringMessageAdapter<V> listener;
if (isFluxListener()) {
listener = new PulsarReactiveStreamingMessagingMessageListenerAdapter<>(this.bean, this.method);
listener = new PulsarReactiveStreamingMessagingMessageListenerAdapter<>(this.bean, this.method,
this.objectMapper);
}
else {
listener = new PulsarReactiveOneByOneMessagingMessageListenerAdapter<>(this.bean, this.method);
listener = new PulsarReactiveOneByOneMessagingMessageListenerAdapter<>(this.bean, this.method,
this.objectMapper);
}

if (messageConverter instanceof PulsarMessageConverter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.pulsar.listener.adapter.AbstractPulsarMessageToSpringMessageAdapter;
import org.springframework.pulsar.reactive.listener.ReactivePulsarMessageHandler;

import com.fasterxml.jackson.databind.ObjectMapper;
import reactor.core.publisher.Flux;

/**
Expand All @@ -36,8 +37,8 @@
public abstract class PulsarReactiveMessagingMessageListenerAdapter<V>
extends AbstractPulsarMessageToSpringMessageAdapter<V> {

public PulsarReactiveMessagingMessageListenerAdapter(Object bean, Method method) {
super(bean, method);
public PulsarReactiveMessagingMessageListenerAdapter(Object bean, Method method, ObjectMapper objectMapper) {
super(bean, method, objectMapper);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.pulsar.reactive.listener.ReactivePulsarMessageHandler;
import org.springframework.pulsar.reactive.listener.ReactivePulsarOneByOneMessageHandler;

import com.fasterxml.jackson.databind.ObjectMapper;
import reactor.core.publisher.Mono;

/**
Expand All @@ -39,8 +40,9 @@
public class PulsarReactiveOneByOneMessagingMessageListenerAdapter<V>
extends PulsarReactiveMessagingMessageListenerAdapter<V> implements ReactivePulsarOneByOneMessageHandler<V> {

public PulsarReactiveOneByOneMessagingMessageListenerAdapter(Object bean, Method method) {
super(bean, method);
public PulsarReactiveOneByOneMessagingMessageListenerAdapter(Object bean, Method method,
ObjectMapper objectMapper) {
super(bean, method, objectMapper);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.pulsar.reactive.listener.ReactivePulsarMessageHandler;
import org.springframework.pulsar.reactive.listener.ReactivePulsarStreamingHandler;

import com.fasterxml.jackson.databind.ObjectMapper;
import reactor.core.publisher.Flux;

/**
Expand All @@ -39,8 +40,9 @@
public class PulsarReactiveStreamingMessagingMessageListenerAdapter<V>
extends PulsarReactiveMessagingMessageListenerAdapter<V> implements ReactivePulsarStreamingHandler<V> {

public PulsarReactiveStreamingMessagingMessageListenerAdapter(Object bean, Method method) {
super(bean, method);
public PulsarReactiveStreamingMessagingMessageListenerAdapter(Object bean, Method method,
ObjectMapper objectMapper) {
super(bean, method, objectMapper);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* Bean post-processor that registers methods annotated with {@link PulsarListener} to be
* invoked by a Pulsar message listener container created under the covers by a
Expand Down Expand Up @@ -106,8 +108,14 @@ public class PulsarReaderAnnotationBeanPostProcessor<V> extends AbstractPulsarAn

private final List<MethodPulsarReaderEndpoint<?>> processedEndpoints = new ArrayList<>();

private ObjectMapper objectMapper;

@Override
public void afterSingletonsInstantiated() {
this.beanFactory.getBeanProvider(ObjectMapper.class)
.stream()
.findAny()
.ifPresent(objectMapper1 -> this.objectMapper = objectMapper1);
this.registrar.setBeanFactory(this.beanFactory);
this.beanFactory.getBeanProvider(PulsarReaderConfigurer.class)
.forEach(c -> c.configurePulsarReaders(this.registrar));
Expand Down Expand Up @@ -211,6 +219,7 @@ private PulsarReaderContainerFactory resolveContainerFactory(PulsarReader pulsar
private void processPulsarReaderAnnotation(MethodPulsarReaderEndpoint<?> endpoint, PulsarReader pulsarReader,
Object bean, String[] topics) {
endpoint.setBean(bean);
endpoint.setObjectMapper(this.objectMapper);
endpoint.setMessageHandlerMethodFactory(this.messageHandlerMethodFactory);
endpoint.setId(getEndpointId(pulsarReader));
endpoint.setTopics(topics);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* A {@link PulsarListenerEndpoint} providing the method to invoke to process an incoming
* message for this endpoint.
Expand All @@ -74,6 +76,8 @@ public class MethodPulsarListenerEndpoint<V> extends AbstractPulsarListenerEndpo

private Method method;

private ObjectMapper objectMapper;

private MessageHandlerMethodFactory messageHandlerMethodFactory;

private SmartMessageConverter messagingConverter;
Expand Down Expand Up @@ -111,6 +115,14 @@ public Method getMethod() {
return this.method;
}

public ObjectMapper getObjectMapper() {
return this.objectMapper;
}

public void setObjectMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

public void setMessageHandlerMethodFactory(MessageHandlerMethodFactory messageHandlerMethodFactory) {
this.messageHandlerMethodFactory = messageHandlerMethodFactory;
}
Expand Down Expand Up @@ -239,12 +251,12 @@ protected AbstractPulsarMessageToSpringMessageAdapter<V> createMessageListenerIn
AbstractPulsarMessageToSpringMessageAdapter<V> listener;
if (isBatchListener()) {
PulsarBatchMessagesToSpringMessageListenerAdapter<V> messageListener = new PulsarBatchMessagesToSpringMessageListenerAdapter<>(
this.bean, this.method);
this.bean, this.method, this.objectMapper);
listener = messageListener;
}
else {
PulsarRecordMessageToSpringMessageListenerAdapter<V> messageListener = new PulsarRecordMessageToSpringMessageListenerAdapter<>(
this.bean, this.method);
this.bean, this.method, this.objectMapper);
if (messageConverter instanceof PulsarMessageConverter) {
messageListener.setMessageConverter((PulsarMessageConverter) messageConverter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import org.springframework.pulsar.support.converter.PulsarMessageConverter;
import org.springframework.util.Assert;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* A {@link PulsarReaderEndpoint} providing the method to invoke to process an incoming
* message for this endpoint.
Expand All @@ -63,6 +65,8 @@ public class MethodPulsarReaderEndpoint<V> extends AbstractPulsarReaderEndpoint<

private Method method;

private ObjectMapper objectMapper;

private SmartMessageConverter messagingConverter;

private MessageHandlerMethodFactory messageHandlerMethodFactory;
Expand All @@ -89,6 +93,14 @@ public Method getMethod() {
return this.method;
}

public void setObjectMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

public ObjectMapper getObjectMapper() {
return this.objectMapper;
}

@Override
protected AbstractPulsarMessageToSpringMessageAdapter<V> createReaderListener(
PulsarMessageReaderContainer container, @Nullable MessageConverter messageConverter) {
Expand Down Expand Up @@ -175,7 +187,7 @@ protected AbstractPulsarMessageToSpringMessageAdapter<V> createMessageListenerIn

AbstractPulsarMessageToSpringMessageAdapter<V> listener;
PulsarRecordMessageToSpringMessageReaderAdapter<V> messageListener = new PulsarRecordMessageToSpringMessageReaderAdapter<>(
this.bean, this.method);
this.bean, this.method, this.objectMapper);
if (messageConverter instanceof PulsarMessageConverter) {
messageListener.setMessageConverter((PulsarMessageConverter) messageConverter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import org.springframework.pulsar.support.header.JsonPulsarHeaderMapper;
import org.springframework.util.Assert;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* An abstract {@link org.apache.pulsar.client.api.MessageListener} adapter providing the
* necessary infrastructure to extract the payload from a Pulsar message.
Expand Down Expand Up @@ -81,13 +83,14 @@ public abstract class AbstractPulsarMessageToSpringMessageAdapter<V> {

private boolean converterSet;

private PulsarMessageConverter<V> messageConverter = new PulsarRecordMessageConverter<V>(
JsonPulsarHeaderMapper.builder().build());
private PulsarMessageConverter<V> messageConverter;

private Type fallbackType = Object.class;

public AbstractPulsarMessageToSpringMessageAdapter(Object bean, Method method) {
public AbstractPulsarMessageToSpringMessageAdapter(Object bean, Method method, ObjectMapper objectMapper) {
this.bean = bean;
this.messageConverter = new PulsarRecordMessageConverter<V>(
JsonPulsarHeaderMapper.builder().objectMapper(objectMapper).build());
this.inferredType = determineInferredType(method);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import org.springframework.pulsar.listener.Acknowledgement;
import org.springframework.pulsar.listener.PulsarBatchAcknowledgingMessageListener;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* A {@link org.apache.pulsar.client.api.MessageListener MessageListener} adapter that
* invokes a configurable {@link HandlerAdapter}; used when the factory is configured for
Expand All @@ -43,8 +45,8 @@
public class PulsarBatchMessagesToSpringMessageListenerAdapter<V> extends AbstractPulsarMessageToSpringMessageAdapter<V>
implements PulsarBatchAcknowledgingMessageListener<V> {

public PulsarBatchMessagesToSpringMessageListenerAdapter(Object bean, Method method) {
super(bean, method);
public PulsarBatchMessagesToSpringMessageListenerAdapter(Object bean, Method method, ObjectMapper objectMapper) {
super(bean, method, objectMapper);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.springframework.pulsar.listener.Acknowledgement;
import org.springframework.pulsar.listener.PulsarAcknowledgingMessageListener;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* A {@link MessageListener MessageListener} adapter that invokes a configurable
* {@link HandlerAdapter}; used when the factory is configured for the listener to receive
Expand All @@ -37,8 +39,8 @@
public class PulsarRecordMessageToSpringMessageListenerAdapter<V> extends AbstractPulsarMessageToSpringMessageAdapter<V>
implements PulsarAcknowledgingMessageListener<V> {

public PulsarRecordMessageToSpringMessageListenerAdapter(Object bean, Method method) {
super(bean, method);
public PulsarRecordMessageToSpringMessageListenerAdapter(Object bean, Method method, ObjectMapper objectMapper) {
super(bean, method, objectMapper);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.apache.pulsar.client.api.Reader;
import org.apache.pulsar.client.api.ReaderListener;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* A {@link MessageListener MessageListener} adapter that invokes a configurable
* {@link HandlerAdapter}; used when the factory is configured for the listener to receive
Expand All @@ -34,8 +36,8 @@
public class PulsarRecordMessageToSpringMessageReaderAdapter<V> extends AbstractPulsarMessageToSpringMessageAdapter<V>
implements ReaderListener<V> {

public PulsarRecordMessageToSpringMessageReaderAdapter(Object bean, Method method) {
super(bean, method);
public PulsarRecordMessageToSpringMessageReaderAdapter(Object bean, Method method, ObjectMapper objectMapper) {
super(bean, method, objectMapper);
}

@Override
Expand Down