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

时间:2018-06-02 15:47:54

标签: java spring rabbitmq amqp spring-amqp

我使用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$1.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关闭,我不希望我的应用程序崩溃。

1 个答案:

答案 0 :(得分:1)

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

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

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