Set proxy target class on caching producer factory
- Required when template accessed via web request
This commit is contained in:
@@ -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'
|
||||
}
|
||||
|
||||
@@ -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<String>> 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<PulsarTemplate<String>> pulsarTemplateProvider;
|
||||
|
||||
@RestController
|
||||
class TestWebController {
|
||||
|
||||
@GetMapping("/hello")
|
||||
String sayHello() throws PulsarClientException {
|
||||
|
||||
PulsarTemplate<String> pulsarTemplate = pulsarTemplateProvider.getIfAvailable();
|
||||
if (pulsarTemplate == null) {
|
||||
return "NOPE! Not hello world";
|
||||
}
|
||||
MessageId msgId = pulsarTemplate.send("spbast-hello-topic", "hello");
|
||||
return "Hello World -> " + msgId;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -119,6 +119,7 @@ public class CachingPulsarProducerFactory<T> extends DefaultPulsarProducerFactor
|
||||
@SuppressWarnings("unchecked")
|
||||
private Producer<T> wrapProducerWithCloseCallback(Producer<T> producer, Consumer<Producer<T>> closeCallback) {
|
||||
ProxyFactory factory = new ProxyFactory(producer);
|
||||
factory.setProxyTargetClass(true);
|
||||
factory.addAdvice(new MethodInterceptor() {
|
||||
@Nullable
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user