嵌入式mongo地址已在使用中

时间:2018-01-06 05:30:58

标签: mongodb spring-boot

我的项目是带有嵌入式mongo的spring boot,当我运行我的应用程序时,它工作正常,但是如果我停止我的应用程序并再次启动它会给出以下异常。

如果我第二次启动弹簧启动应用程序,我将获得以下异常 如果我再次运行第三次工作正常。

    java.io.IOException: Could not start process: failed errno:48 Address already in use for socket: 127.0.0.1:27017
    2018-01-06T10:24:07.899+0530 E NETWORK  [initandlisten]   addr already in use
    2018-01-06T10:24:07.899+0530 E STORAGE  [initandlisten] Failed to set up sockets during startup.
    2018-01-06T10:24:07.899+0
        at de.flapdoodle.embed.mongo.AbstractMongoProcess.onAfterProcessStart(AbstractMongoProcess.java:79) [de.flapdoodle.embed.mongo-1.50.0.jar:na]
        at de.flapdoodle.embed.process.runtime.AbstractProcess.<init>(AbstractProcess.java:114) ~[de.flapdoodle.embed.process-1.50.0.jar:na]
        at de.flapdoodle.embed.mongo.AbstractMongoProcess.<init>(AbstractMongoProcess.java:53) [de.flapdoodle.embed.mongo-1.50.0.jar:na]
        at de.flapdoodle.embed.mongo.MongodProcess.<init>(MongodProcess.java:50) [de.flapdoodle.embed.mongo-1.50.0.jar:na]
        at de.flapdoodle.embed.mongo.MongodExecutable.start(MongodExecutable.java:44) [de.flapdoodle.embed.mongo-1.50.0.jar:na]
        at de.flapdoodle.embed.mongo.MongodExecutable.start(MongodExecutable.java:34) [de.flapdoodle.embed.mongo-1.50.0.jar:na]
        at de.flapdoodle.embed.process.runtime.Executable.start(Executable.java:101) [de.flapdoodle.embed.process-1.50.0.jar:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1719) [spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1656) [spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1585) [spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:554) [spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) [spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) [spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) [spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) [spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759) [spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) [spring-context-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) [spring-context-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.4.5.RELEASE.jar:1.4.5.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) [spring-boot-1.4.5.RELEASE.jar:1.4.5.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:372) [spring-boot-1.4.5.RELEASE.jar:1.4.5.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-1.4.5.RELEASE.jar:1.4.5.RELEASE]
        at com.reputation.r4e.integrator.IntegratorMain.main(IntegratorMain.java:36) [classes/:na]
    [r4e-integrator][2018-01-06 10:24:07,908][main]          [INFO ][AbstractProcess.<init>:118] construct de.flapdoodle.embed.mongo.config.MongodConfigBuilder$ImmutableMongodConfig@4fbebd8e - [] - [] 
    [r4e-integrator][2018-01-06 10:24:08,013][main]          [WARN ][AbstractApplicationContext.refresh:550] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embeddedMongoServer' defined in class path resource [org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.class]: Invocation of init method failed; nested exception is java.io.IOException: Could not start process: failed errno:48 Address already in use for socket: 127.0.0.1:27017
    2018-01-06T10:24:07.899+0530 E NETWORK  [initandlisten]   addr already in use
    2018-01-06T10:24:07.899+0530 E STORAGE  [initandlisten] Failed to set up sockets during startup.
    2018-01-06T10:24:07.899+0 - [] - [] 
    [r4e-integrator][2018-01-06 10:24:08,020][main]          [INFO ][DirectJDKLog.log:179] Stopping service Tomcat - [] - [] 
    [r4e-integrator][2018-01-06 10:24:08,034][main]          [WARN ][SpringApplicationRunListeners.callFinishedListener:91] Error handling failed (ch.qos.logback.classic.LoggerContext.removeObject(Ljava/lang/String;)V) - [] - [] 
    [r4e-integrator][2018-01-06 10:24:08,040][main]          [ERROR][SpringApplication.reportFailure:840] Application startup failed - [] - [] 
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embeddedMongoServer' defined in class path resource [org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.class]: Invocation of init method failed; nested exception is java.io.IOException: Could not start process: failed errno:48 Address already in use for socket: 127.0.0.1:27017
    2018-01-06T10:24:07.899+0530 E NETWORK  [initandlisten]   addr already in use
    2018-01-06T10:24:07.899+0530 E STORAGE  [initandlisten] Failed to set up sockets during startup.
    2018-01-06T10:24:07.899+0
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1589) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:554) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.5.RELEASE.jar:1.4.5.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) ~[spring-boot-1.4.5.RELEASE.jar:1.4.5.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:372) ~[spring-boot-1.4.5.RELEASE.jar:1.4.5.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) ~[spring-boot-1.4.5.RELEASE.jar:1.4.5.RELEASE]
        at com.reputation.r4e.integrator.IntegratorMain.main(IntegratorMain.java:36) [classes/:na]
    Caused by: java.io.IOException: Could not start process: failed errno:48 Address already in use for socket: 127.0.0.1:27017
    2018-01-06T10:24:07.899+0530 E NETWORK  [initandlisten]   addr already in use
    2018-01-06T10:24:07.899+0530 E STORAGE  [initandlisten] Failed to set up sockets during startup.
    2018-01-06T10:24:07.899+0
        at de.flapdoodle.embed.mongo.AbstractMongoProcess.onAfterProcessStart(AbstractMongoProcess.java:79) ~[de.flapdoodle.embed.mongo-1.50.0.jar:na]
        at de.flapdoodle.embed.process.runtime.AbstractProcess.<init>(AbstractProcess.java:114) ~[de.flapdoodle.embed.process-1.50.0.jar:na]
        at de.flapdoodle.embed.mongo.AbstractMongoProcess.<init>(AbstractMongoProcess.java:53) ~[de.flapdoodle.embed.mongo-1.50.0.jar:na]
        at de.flapdoodle.embed.mongo.MongodProcess.<init>(MongodProcess.java:50) ~[de.flapdoodle.embed.mongo-1.50.0.jar:na]
        at de.flapdoodle.embed.mongo.MongodExecutable.start(MongodExecutable.java:44) ~[de.flapdoodle.embed.mongo-1.50.0.jar:na]
        at de.flapdoodle.embed.mongo.MongodExecutable.start(MongodExecutable.java:34) ~[de.flapdoodle.embed.mongo-1.50.0.jar:na]
        at de.flapdoodle.embed.process.runtime.Executable.start(Executable.java:101) ~[de.flapdoodle.embed.process-1.50.0.jar:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1719) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1656) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1585) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
        ... 14 common frames omitted

这个问题仅在eclipse中出现,如果使用intillij它工作正常

如何解决此问题。

1 个答案:

答案 0 :(得分:1)

默认情况下,Spring会为每个测试类创建一个新的应用程序上下文。

似乎还启动了嵌入式Mongo服务器的新实例,但由于某种原因,所需端口仍然被先前测试的实例使用。

作为一种解决方法,我现在使用@contextconfiguration批注仅在单个(集成)测试类中加载Mongo而不在其他测试类中加载。

使用@DirtiesContext强制重置。例如:

@ContextConfiguration(classes={BlahTestConfig.class})
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class SomeTest {

@Autowired XXXX xx;
@Autowired YYYY yy;

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    when(YYYY.newYY()).thenReturn(zz);
}

@Test
public void testSomeTest() {
    XX.changeSomething("StringTest");
    XX.doSomething();
    check_for_effects();
}

@Test
public void testSomeOtherTest() {
    XX.changeSomething("SomeotherString");
    XX.doSomething();
    check_for_effects();
}

DirtiesContext 指示在执行测试期间底层的Spring ApplicationContext已被弄脏(修改),并且应该关闭,无论测试是否通过:

  • 在当前测试类之后,在类模式设置为AFTER_CLASS的类上声明,这是默认的类模式。

  • 在当前测试类中的每个测试方法之后,在类模式设置为AFTER_EACH_TEST_METHOD的类上声明。

  • 在当前测试之后,在方法上声明。

  

在JUnit 3.8环境中,@ DirtiesContext仅支持方法,因此不支持类级别。



参考1: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/issues/176

参考2: https://stackoverflow.com/a/15948370/4227658