From fcc07cff845cd44b75e2cf8d61f99faedf7819b6 Mon Sep 17 00:00:00 2001 From: Chris Bono Date: Sat, 26 Nov 2022 20:40:26 -0600 Subject: [PATCH] Set proxy target class on caching producer factory - Required when template accessed via web request --- .../build.gradle | 1 + .../SpringPulsarBootAppSanityTests.java | 40 ++++++++++++++++++- .../core/CachingPulsarProducerFactory.java | 1 + 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/spring-pulsar-spring-boot-autoconfigure/build.gradle b/spring-pulsar-spring-boot-autoconfigure/build.gradle index 547062c4..ddbc74da 100644 --- a/spring-pulsar-spring-boot-autoconfigure/build.gradle +++ b/spring-pulsar-spring-boot-autoconfigure/build.gradle @@ -18,6 +18,7 @@ dependencies { testRuntimeOnly 'org.apache.logging.log4j:log4j-core' testRuntimeOnly 'org.apache.logging.log4j:log4j-jcl' testImplementation 'org.springframework.boot:spring-boot-starter-test' + testImplementation 'org.springframework.boot:spring-boot-starter-web' testImplementation 'org.testcontainers:junit-jupiter' testImplementation 'org.testcontainers:pulsar' } diff --git a/spring-pulsar-spring-boot-autoconfigure/src/test/java/org/springframework/pulsar/autoconfigure/SpringPulsarBootAppSanityTests.java b/spring-pulsar-spring-boot-autoconfigure/src/test/java/org/springframework/pulsar/autoconfigure/SpringPulsarBootAppSanityTests.java index 8c6c1062..4687a82c 100644 --- a/spring-pulsar-spring-boot-autoconfigure/src/test/java/org/springframework/pulsar/autoconfigure/SpringPulsarBootAppSanityTests.java +++ b/spring-pulsar-spring-boot-autoconfigure/src/test/java/org/springframework/pulsar/autoconfigure/SpringPulsarBootAppSanityTests.java @@ -18,6 +18,8 @@ package org.springframework.pulsar.autoconfigure; import static org.assertj.core.api.Assertions.assertThat; +import org.apache.pulsar.client.api.MessageId; +import org.apache.pulsar.client.api.PulsarClientException; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.ObjectProvider; @@ -25,8 +27,14 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.pulsar.autoconfigure.SpringPulsarBootAppSanityTests.SpringPulsarBootTestApp; import org.springframework.pulsar.core.PulsarTemplate; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; /** * Sanity tests to ensure that {@code Spring Pulsar} can be auto-configured into a Spring @@ -34,19 +42,49 @@ import org.springframework.pulsar.core.PulsarTemplate; * * @author Chris Bono */ -@SpringBootTest(classes = SpringPulsarBootTestApp.class) +@SpringBootTest(classes = SpringPulsarBootTestApp.class, webEnvironment = WebEnvironment.RANDOM_PORT) class SpringPulsarBootAppSanityTests implements PulsarTestContainerSupport { + @DynamicPropertySource + static void pulsarProperties(DynamicPropertyRegistry registry) { + registry.add("spring.pulsar.client.service-url", PulsarTestContainerSupport::getPulsarBrokerUrl); + } + @Test void appStartsWithAutoConfiguredSpringPulsarComponents( @Autowired ObjectProvider> pulsarTemplate) { assertThat(pulsarTemplate.getIfAvailable()).isNotNull(); } + @Test + void templateCanBeAccessedDuringWebRequest(@Autowired TestRestTemplate restTemplate) { + String body = restTemplate.getForObject("/hello", String.class); + assertThat(body).startsWith("Hello World -> "); + } + @SpringBootConfiguration @EnableAutoConfiguration static class SpringPulsarBootTestApp { + @Autowired + private ObjectProvider> pulsarTemplateProvider; + + @RestController + class TestWebController { + + @GetMapping("/hello") + String sayHello() throws PulsarClientException { + + PulsarTemplate pulsarTemplate = pulsarTemplateProvider.getIfAvailable(); + if (pulsarTemplate == null) { + return "NOPE! Not hello world"; + } + MessageId msgId = pulsarTemplate.send("spbast-hello-topic", "hello"); + return "Hello World -> " + msgId; + } + + } + } } diff --git a/spring-pulsar/src/main/java/org/springframework/pulsar/core/CachingPulsarProducerFactory.java b/spring-pulsar/src/main/java/org/springframework/pulsar/core/CachingPulsarProducerFactory.java index f91c94f9..066ccbe7 100644 --- a/spring-pulsar/src/main/java/org/springframework/pulsar/core/CachingPulsarProducerFactory.java +++ b/spring-pulsar/src/main/java/org/springframework/pulsar/core/CachingPulsarProducerFactory.java @@ -119,6 +119,7 @@ public class CachingPulsarProducerFactory extends DefaultPulsarProducerFactor @SuppressWarnings("unchecked") private Producer wrapProducerWithCloseCallback(Producer producer, Consumer> closeCallback) { ProxyFactory factory = new ProxyFactory(producer); + factory.setProxyTargetClass(true); factory.addAdvice(new MethodInterceptor() { @Nullable @Override