Spring-Boot AMQP 根据数据库数据创建队列
Spring-Boot AMQP create queues based on database data
我想使用我的 Spring-Boot 应用程序根据我数据库中的一些数据在 RabbitMQ 中创建队列。
例如:我有一个带有一些队列名称的实体。我需要为他们每个人创建一个新队列。稍后我想使用它们而不创建它们。
我已经创建了一些配置来创建队列并将它们绑定到交换器。但是我的数据库中的数据会发生变化,对于特定实体中的每个新条目,我以后都需要一个新的队列。
一个想法是 @Autowired 在我的一个 @Configuraion 服务中,它从数据库中获取所需的数据。不幸的是,这没有用,因为我自动连接的服务出现了 BeanCreationException。
我哪里做错了,或者有其他方法可以根据数据库条目创建队列吗?
这是我构建的class:
@Configuration
public class RabbitMQQueueConfiguration {
private QueueNameService queueNameService;
@Autowired
public RabbitMQQueueConfiguration(QueueNameService queueNameService){
this.queueNameService =queueNameService;
}
private String queueA = getQueueNameA(1L);
private String queueB="queueB";
@Bean
public Queue queueA() {
return new Queue(queueA);
}
@Bean
public Queue queueB() {
return new Queue(queueB);
}
private String getQueueNameA(Long queueID){
return queueNameService.getQueueName(queueID);
}
}
这是我得到的堆栈跟踪:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rabbitMQQueueConfiguration' defined in file [...\classes\com\example\DemoRabbitMq\config\RabbitMQQueueConfiguration.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.DemoRabbitMq.config.RabbitMQQueueConfiguration$$EnhancerBySpringCGLIB$$bc4b2845]: Constructor threw exception; nested exception is java.lang.NullPointerException: Cannot invoke "com.example.DemoRabbitMq.service.QueueNameService.getQueueName(java.lang.Long)" because "this.queueNameService" is null
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:313) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:294) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1356) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1203) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=12=](AbstractBeanFactory.java:324) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at com.example.DemoRabbitMq.DemoRabbitMqApplication.main(DemoRabbitMqApplication.java:24) ~[classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.DemoRabbitMq.config.RabbitMQQueueConfiguration$$EnhancerBySpringCGLIB$$bc4b2845]: Constructor threw exception; nested exception is java.lang.NullPointerException: Cannot invoke "com.example.DemoRabbitMq.service.QueueNameService.getQueueName(java.lang.Long)" because "this.queueNameService" is null
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:217) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:309) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
... 19 common frames omitted
Caused by: java.lang.NullPointerException: Cannot invoke "com.example.DemoRabbitMq.service.QueueNameService.getQueueName(java.lang.Long)" because "this.queueNameService" is null
at com.example.DemoRabbitMq.config.RabbitMQQueueConfiguration.getQueueNameA(RabbitMQQueueConfiguration.java:34) ~[classes/:na]
at com.example.DemoRabbitMq.config.RabbitMQQueueConfiguration.<init>(RabbitMQQueueConfiguration.java:19) ~[classes/:na]
at com.example.DemoRabbitMq.config.RabbitMQQueueConfiguration$$EnhancerBySpringCGLIB$$bc4b2845.<init>(<generated>) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500) ~[na:na]
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481) ~[na:na]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:204) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
... 21 common frames omitted
编辑:
作为请求我的服务
@Service
public class QueueNameService {
private static final Logger LOGGER = LoggerFactory.getLogger(QueueNameService.class);
private final SomeOtherRepository someOtherRepository;
private final SomeRepository someRepository;
@Autowired
public QueueNameService(SomeOtherRepository someOtherRepository, SomeRepository someRepository) {
this.someOtherRepository= someOtherRepository;
this.someRepository= someRepository;
}
public String getQueueName(int tld_id) throws NoSuchElementException {
Optional<Foo> foo= someOtherRepository.findById(foo_id);
Optional<Bar> bar= someRepository.findById(foo.get().getBarId());
return bar.map(Bar::getName).orElse(null);
}
}
您有这样的代码:
private String queueA = getQueueNameA(1L);
在构造函数调用之前调用。由于您尝试访问从构造函数初始化的 属性 (queueNameService
),因此它以 NullPointerException
.
结束
您的问题没有任何 Spring(或 AMQP)具体内容:这只是一般性的 Java 使用问题。
我想使用我的 Spring-Boot 应用程序根据我数据库中的一些数据在 RabbitMQ 中创建队列。 例如:我有一个带有一些队列名称的实体。我需要为他们每个人创建一个新队列。稍后我想使用它们而不创建它们。
我已经创建了一些配置来创建队列并将它们绑定到交换器。但是我的数据库中的数据会发生变化,对于特定实体中的每个新条目,我以后都需要一个新的队列。
一个想法是 @Autowired 在我的一个 @Configuraion 服务中,它从数据库中获取所需的数据。不幸的是,这没有用,因为我自动连接的服务出现了 BeanCreationException。
我哪里做错了,或者有其他方法可以根据数据库条目创建队列吗?
这是我构建的class:
@Configuration
public class RabbitMQQueueConfiguration {
private QueueNameService queueNameService;
@Autowired
public RabbitMQQueueConfiguration(QueueNameService queueNameService){
this.queueNameService =queueNameService;
}
private String queueA = getQueueNameA(1L);
private String queueB="queueB";
@Bean
public Queue queueA() {
return new Queue(queueA);
}
@Bean
public Queue queueB() {
return new Queue(queueB);
}
private String getQueueNameA(Long queueID){
return queueNameService.getQueueName(queueID);
}
}
这是我得到的堆栈跟踪:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rabbitMQQueueConfiguration' defined in file [...\classes\com\example\DemoRabbitMq\config\RabbitMQQueueConfiguration.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.DemoRabbitMq.config.RabbitMQQueueConfiguration$$EnhancerBySpringCGLIB$$bc4b2845]: Constructor threw exception; nested exception is java.lang.NullPointerException: Cannot invoke "com.example.DemoRabbitMq.service.QueueNameService.getQueueName(java.lang.Long)" because "this.queueNameService" is null
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:313) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:294) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1356) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1203) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=12=](AbstractBeanFactory.java:324) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at com.example.DemoRabbitMq.DemoRabbitMqApplication.main(DemoRabbitMqApplication.java:24) ~[classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.DemoRabbitMq.config.RabbitMQQueueConfiguration$$EnhancerBySpringCGLIB$$bc4b2845]: Constructor threw exception; nested exception is java.lang.NullPointerException: Cannot invoke "com.example.DemoRabbitMq.service.QueueNameService.getQueueName(java.lang.Long)" because "this.queueNameService" is null
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:217) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:309) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
... 19 common frames omitted
Caused by: java.lang.NullPointerException: Cannot invoke "com.example.DemoRabbitMq.service.QueueNameService.getQueueName(java.lang.Long)" because "this.queueNameService" is null
at com.example.DemoRabbitMq.config.RabbitMQQueueConfiguration.getQueueNameA(RabbitMQQueueConfiguration.java:34) ~[classes/:na]
at com.example.DemoRabbitMq.config.RabbitMQQueueConfiguration.<init>(RabbitMQQueueConfiguration.java:19) ~[classes/:na]
at com.example.DemoRabbitMq.config.RabbitMQQueueConfiguration$$EnhancerBySpringCGLIB$$bc4b2845.<init>(<generated>) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500) ~[na:na]
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481) ~[na:na]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:204) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
... 21 common frames omitted
编辑: 作为请求我的服务
@Service
public class QueueNameService {
private static final Logger LOGGER = LoggerFactory.getLogger(QueueNameService.class);
private final SomeOtherRepository someOtherRepository;
private final SomeRepository someRepository;
@Autowired
public QueueNameService(SomeOtherRepository someOtherRepository, SomeRepository someRepository) {
this.someOtherRepository= someOtherRepository;
this.someRepository= someRepository;
}
public String getQueueName(int tld_id) throws NoSuchElementException {
Optional<Foo> foo= someOtherRepository.findById(foo_id);
Optional<Bar> bar= someRepository.findById(foo.get().getBarId());
return bar.map(Bar::getName).orElse(null);
}
}
您有这样的代码:
private String queueA = getQueueNameA(1L);
在构造函数调用之前调用。由于您尝试访问从构造函数初始化的 属性 (queueNameService
),因此它以 NullPointerException
.
您的问题没有任何 Spring(或 AMQP)具体内容:这只是一般性的 Java 使用问题。