Rabbitmq 一个用户的双连接

Rabbitmq 一个用户的双连接,rabbitmq,spring-amqp,spring-rabbit,Rabbitmq,Spring Amqp,Spring Rabbit,我是RabbitMQ的新用户,我非常喜欢它,但我有一个问题(它不会抛出任何错误,也不会影响任何东西,除了我的思维…) 每次运行消费者时,它都会创建2个连接。我不知道为什么,所以我请求你的帮助 我正在使用Spring Boot和Spring AMQP(可能是因为Spring…) 代码如下: receiver context.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframe

我是RabbitMQ的新用户,我非常喜欢它,但我有一个问题(它不会抛出任何错误,也不会影响任何东西,除了我的思维…)

每次运行消费者时,它都会创建2个连接。我不知道为什么,所以我请求你的帮助

我正在使用Spring Boot和Spring AMQP(可能是因为Spring…)

代码如下:

receiver context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">

  <rabbit:connection-factory id="connectionFactory" host="localhost" username="admin" password="admin" />

  <bean id="receiver" class="com.test.Receiver" />

  <bean id="messageListener" class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter" >
      <constructor-arg name="delegate" ref="receiver"/>
      <constructor-arg name="defaultListenerMethod" value="receiveMessage" />
  </bean>

  <bean id="container" class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer" > 
    <property name="connectionFactory" ref="connectionFactory" />
    <property name="queueNames" value="AMQP-PoC" />
    <property name="messageListener" ref="messageListener" />
    <property name="defaultRequeueRejected" value="false" />
  </bean>
Receiver.java

@SpringBootApplication
@ImportResource("classpath:com.test/rabbit-receiver-context.xml")
public class AMQPPoCReceiverApplication implements CommandLineRunner {

  private AnnotationConfigApplicationContext context;

  @Override
  public void run(String... strings) throws Exception {
      context = new   AnnotationConfigApplicationContext(AMQPPoCReceiverApplication.class);
      System.out.println("Waiting for message");
  }

  @Override
  protected void finalize() throws Throwable {
      super.finalize();
      this.context.close();
  }

  public static void main(String[] args) {
      SpringApplication.run(AMQPPoCReceiverApplication.class, args);
  }
}
public class Receiver {

  public void receiveMessage(String message) {
      System.out.println("Message received : " + message);
  }
}
这里是开始处的日志(注意带有双“*”的行):

2016-02-18 11:32:51.956  INFO 10196 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-02-18 11:32:51.966  INFO 10196 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase -2147482648
2016-02-18 11:32:51.967  INFO 10196 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 2147483647
**2016-02-18 11:32:52.062  INFO 10196 --- [cTaskExecutor-1] o.s.a.r.c.CachingConnectionFactory       : Created new connection: SimpleConnection@2069bb0a [delegate=amqp://admin@127.0.0.1:5672/]**
2016-02-18 11:32:52.148  INFO 10196 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 52752 (http)
2016-02-18 11:32:52.153  INFO 10196 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@57bf85b2: startup date [Thu Feb 18 11:32:52 GMT+01:00 2016]; root of contex
t hierarchy
**2016-02-18 11:32:52.320  INFO 10196 --- [           main] o.s.b.f.xml.XmlBeanDefinitionReader      : Loading XML bean definitions from class path resource [com.test/receiver-context.xml]**
2016-02-18 11:32:52.357  INFO 10196 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2016-02-18 11:32:52.362  INFO 10196 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type [class org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfigur
ation$$EnhancerBySpringCGLIB$$eccd4a65] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-02-18 11:32:52.487  INFO 10196 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-02-18 11:32:52.489  INFO 10196 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase -2147482648
2016-02-18 11:32:52.489  INFO 10196 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 2147483647
**2016-02-18 11:32:52.498  INFO 10196 --- [cTaskExecutor-1] o.s.a.r.c.CachingConnectionFactory       : Created new connection: SimpleConnection@768748cf [delegate=amqp://admin@127.0.0.1:5672/]**
Waiting for message
2016-02-18 11:32:52.505  INFO 10196 --- [           main] com.test.AMQPPoCReceiverApplication   : Started AMQPPoCReceiverApplication in 3.509 seconds (JVM running for 6.961)
这里是双重连接:

如果我停止客户端,它会同时关闭这两个连接(这就是为什么我确信这是同一消费者的双重连接)

如果你需要更多的信息,请在这里询问,我会尽快回复


谢谢大家的帮助。

答案很简单:我在同一个应用程序中创建了两个上下文

new AnnotationConfigApplicationContext(AMQPPoCReceiverApplication.class);

只创建一个,就完成了

SpringApplication.run(AMQPPoCReceiverApplication.class, args);