如果代理已关闭,则无法创建 Bean

Bean can not be created if the broker is down

我使用 Spring AMQP + RabbitMQ。通常,如果 rabbitmq-server 关闭,Spring 会处理这种情况。应用抛出异常但仍尝试重新连接。但是,如果我将 amqp bean 放入另一个 bean 中,因为它是 属性 应用程序在启动时失败。我收到此错误

31 мая 2018 17:31:24.902 [communication-server-0.15.15][o.s.c.s.AbstractApplicationContext.refresh] WARN  Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'driverManager' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:15672/api/queues/": Connect to localhost:15672 [localhost/127.0.0.1] failed: В соединении отказано (Connection refused); nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:15672 [localhost/127.0.0.1] failed: В соединении отказано (Connection refused)
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'driverManager' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:15672/api/queues/": Connect to localhost:15672 [localhost/127.0.0.1] failed: В соединении отказано (Connection refused); nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:15672 [localhost/127.0.0.1] failed: В соединении отказано (Connection refused)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:306)
  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
  at org.springframework.context.support.ClassPathXmlApplicationContext.<init ClassPathXmlApplicationContext.java:139)
  at org.springframework.context.support.ClassPathXmlApplicationContext.<init ClassPathXmlApplicationContext.java:105)
  at ru.ru.Server.run(CommunicationServer.java:36)
  at ru.rubServer.main(CommunicationServer.java:42)
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:15672/api/queues/": Connect to localhost:15672 [localhost/127.0.0.1] failed: В соединении отказано (Connection refused); nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:15672 [localhost/127.0.0.1] failed: В соединении отказано (Connection refused)
  at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:666)
  at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:628)
  at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:303)
  at com.rabbitmq.http.client.Client.getQueues(Client.java:409)
  at ru.rub.DriverManager.removeUnusedQueues(DriverManager.java:340)
  at ru.rub.DriverManager.initialize(DriverManager.java:158)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1758)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1695)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
  ... 13 more
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:15672 [localhost/127.0.0.1] failed: В соединении отказано (Connection refused)
  at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:142)
  at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:319)
  at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
  at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
  at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
  at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
  at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
  at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
  at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:89)
  at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
  at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
  at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:652)
  ... 25 more
Caused by: java.net.ConnectException: В соединении отказано (Connection refused)
  at java.net.PlainSocketImpl.socketConnect(Native Method)
  at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
  at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
  at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
  at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
  at java.net.Socket.connect(Socket.java:589)
  at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:72)
  at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:125)
  ... 38 more

关于如何处理这个问题有什么想法吗?如果 rabbitmq-server 出现故障,我不希望我的应用崩溃。

您需要显示完整的堆栈跟踪。

但是,看起来您可能会在应用程序上下文完全准备就绪并刷新之前尝试连接到代理。您不得使用 @PostConstruct 方法、afterPropertiesSet() 方法等连接到代理

在构建应用程序上下文时,您永远不应在构建应用程序上下文的线程上对外部系统执行操作。