fix(syslog-consumer-rule)解决ObjectMapper多实例冲突
This commit is contained in:
+3
-1
@@ -3,6 +3,7 @@ package com.common.service;
|
|||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -35,7 +36,8 @@ public class RawLogCaptureClusterStore {
|
|||||||
@Value("${syslog.raw-capture.cluster-activation-delay-ms:1000}")
|
@Value("${syslog.raw-capture.cluster-activation-delay-ms:1000}")
|
||||||
private long activationDelayMs = 1000L;
|
private long activationDelayMs = 1000L;
|
||||||
|
|
||||||
public RawLogCaptureClusterStore(StringRedisTemplate redis, ObjectMapper mapper,
|
public RawLogCaptureClusterStore(StringRedisTemplate redis,
|
||||||
|
@Qualifier("httpObjectMapper") ObjectMapper mapper,
|
||||||
@Value("${syslog.raw-capture.node-id:${HOSTNAME:unknown}}")
|
@Value("${syslog.raw-capture.node-id:${HOSTNAME:unknown}}")
|
||||||
String configuredNodeId) {
|
String configuredNodeId) {
|
||||||
this.redis = redis;
|
this.redis = redis;
|
||||||
|
|||||||
+45
@@ -0,0 +1,45 @@
|
|||||||
|
package com.common.service;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
class RawLogCaptureClusterStoreWiringTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void usesHttpObjectMapperWhenApplicationHasMultipleObjectMappers() {
|
||||||
|
try (AnnotationConfigApplicationContext context =
|
||||||
|
new AnnotationConfigApplicationContext(TestConfiguration.class,
|
||||||
|
RawLogCaptureClusterStore.class)) {
|
||||||
|
RawLogCaptureClusterStore store = context.getBean(RawLogCaptureClusterStore.class);
|
||||||
|
|
||||||
|
assertSame(context.getBean("httpObjectMapper"),
|
||||||
|
ReflectionTestUtils.getField(store, "mapper"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
static class TestConfiguration {
|
||||||
|
@Bean
|
||||||
|
ObjectMapper objectMapper() {
|
||||||
|
return new ObjectMapper();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
ObjectMapper httpObjectMapper() {
|
||||||
|
return new ObjectMapper();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
StringRedisTemplate stringRedisTemplate() {
|
||||||
|
return mock(StringRedisTemplate.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user