代码之家  ›  专栏  ›  技术社区  ›  syd

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

  •  0
  • syd  · 技术社区  · 7 年前

    我使用spring amqp+rabbitmq。通常,如果rabbitmq服务器关闭,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服务器关闭,我不希望我的应用崩溃。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Gary Russell    7 年前

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

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

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