All Downloads are FREE. Search and download functionalities are using the official Maven repository.

generator.server.springboot.database.redis.TestRedisManager.mustache Maven / Gradle / Ivy

There is a newer version: 1.22.0
Show newest version
package {{packageName}};

import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;

class TestRedisManager implements ApplicationListener {

  private static GenericContainer redisContainer;

  @Override
  public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
    if (redisContainer != null) {
      return;
    }

    redisContainer = new GenericContainer(DockerImageName.parse("{{redisDockerImage}}")).withExposedPorts(6379);;

    redisContainer.start();

    System.setProperty("TEST_REDIS_URL", "redis://" + redisContainer.getHost() + ":" + redisContainer.getMappedPort(6379));
    Runtime.getRuntime()
        .addShutdownHook(new Thread(stopContainer()));
  }

  private Runnable stopContainer() {
    return redisContainer::stop;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy