Java 在spring boot rabbitmq的运行时创建队列/交换/绑定/侦听器
我将spring boot与rabbitmq一起使用。我已经创建了一些固定的队列/交换/绑定/侦听器 侦听器的创建如下所示:Java 在spring boot rabbitmq的运行时创建队列/交换/绑定/侦听器,java,spring-boot,spring-rabbit,Java,Spring Boot,Spring Rabbit,我将spring boot与rabbitmq一起使用。我已经创建了一些固定的队列/交换/绑定/侦听器 侦听器的创建如下所示: @RabbitListener public void foo(String msg) {...} 现在,我想在运行时为每个用户创建一个队列,每当他与exchange/binding/listener一起登录时,并在用户注销时销毁这些队列。如何在spring boot中做到这一点。使用@RabbitListener(可以,但必须为每个应用程序创建一个新的子应用程序上下文
@RabbitListener
public void foo(String msg) {...}
现在,我想在运行时为每个用户创建一个队列,每当他与exchange/binding/listener一起登录时,并在用户注销时销毁这些队列。如何在spring boot中做到这一点。使用
@RabbitListener
(可以,但必须为每个应用程序创建一个新的子应用程序上下文)
您可以使用RabbitAdmin
动态创建队列和绑定,并为每个新队列创建消息侦听器容器
编辑
这是一种使用@RabbitListener
和子上下文的方法;使用Spring引导时,ListenerConfig
类不能与引导应用程序本身位于同一个包(或子包)中
@SpringBootApplication
public class So48617898Application {
public static void main(String[] args) {
SpringApplication.run(So48617898Application.class, args).close();
}
private final Map<String, ConfigurableApplicationContext> children = new HashMap<>();
@Bean
public ApplicationRunner runner(RabbitTemplate template, ApplicationContext context) {
return args -> {
Scanner scanner = new Scanner(System.in);
String line = null;
while (true) {
System.out.println("Enter a new queue");
line = scanner.next();
if ("quit".equals(line)) {
break;
}
children.put(line, addNewListener(line, context));
template.convertAndSend(line, "test to " + line);
}
scanner.close();
for (ConfigurableApplicationContext ctx : this.children.values()) {
ctx.stop();
}
};
}
private ConfigurableApplicationContext addNewListener(String queue, ApplicationContext context) {
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
child.setParent(context);
ConfigurableEnvironment environment = child.getEnvironment();
Properties properties = new Properties();
properties.setProperty("queue.name", queue);
PropertiesPropertySource pps = new PropertiesPropertySource("props", properties);
environment.getPropertySources().addLast(pps);
child.register(ListenerConfig.class);
child.refresh();
return child;
}
}
你能举个例子吗?有关如何使用
@RabbitListener
s执行此操作的示例,请参见我的编辑。当我尝试使用此逻辑创建具有独占和自动删除功能的队列时,它会引发异常-通道错误;协议方法:#方法(reply code=405,reply text=RESOURCE_LOCKED-无法以独占方式访问vhost'/'中锁定的队列'ewfasdfas-asdtweasfd-11703B'。它可能最初在另一个连接上声明,或者独占属性值与原始声明的值不匹配。
。这就是我如何修饰我的侦听器-@RabbitListener(bindings=@QueueBinding(value=@org.springframework.amqp.rabbit.annotation.Queue(value=“${Queue.name}”、dustable=“true”、autoDelete=“true”、exclusive=“true”、arguments=@Argument(name=“x-expires”、value=“20000”、type=“java.lang.Integer”)、exchange=@exchange(value=“${exchange.name}”、ignoreDeclarationExceptions=“true”)、key=“${routing.key}”)
不要对3年前的答案发表评论,也不要在评论中添加代码;它不会很好地呈现。错误对我来说似乎很清楚;现有队列不是独占的,或者是在不同的连接上声明的。如果仍然无法理解,请提出一个显示配置的新问题。
@Configuration
@EnableRabbit
public class ListenerConfig {
@RabbitListener(queues = "${queue.name}")
public void listen(String in, @Header(AmqpHeaders.CONSUMER_QUEUE) String queue) {
System.out.println("Received " + in + " from queue " + queue);
}
@Bean
public Queue queue(@Value("${queue.name}") String name) {
return new Queue(name);
}
@Bean
public RabbitAdmin admin(ConnectionFactory cf) {
return new RabbitAdmin(cf);
}
}