gpt4 book ai didi

java - 如何将 Embedded PostgreSQL Server Java 组件用作单独的服务?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:37:05 25 4
gpt4 key购买 nike

我正在尝试为在 Tomcat(7.x) 中运行并依赖于 Postgresql (9.x) 实例的基于 RESTful(服务)Java 的应用程序创建一个全面的集成测试套件。此外,如果可能的话,我希望能够通过使用 maven failsafe 插件,将这个套件作为一个独立的进程运行,专门来自 maven 3.x。这样,测试就可以在 3 个主要平台(Mac OSX、Linux 和 Windows)上运行。

据我所知,我认为实现这一目标的关键是执行以下步骤(按此顺序):

  1. maven lifecycle 的预集成测试阶段以某种方式启动 postgresql 数据库的“嵌入式”版本(和架构设置) , 让 postgres-db-process 在后台运行
  2. 启动加载我的服务应用程序的 Java 容器:我正在使用 Jetty 9.1.5 插件
  3. 在集成测试阶段从 Failsafe 插件运行我的(基于 JUnit 的)测试
  4. 在 Maven 的集成后测试阶段关闭我的 Jetty 容器
  5. 最后,在生命周期的后集成测试阶段(kill/clean-完成这个过程)

在我当前的实现中,成功完成了步骤 1 - 3。在第 2 步中,它使用 exec-maven-plugin,它调用一个使用 postgresql-embedded Java component 的 Java 类。 .POM.xml 的摘录:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>Run Postgres DB start/schema setup</id>
<phase>pre-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.some.package.test.utils.DbSetup</mainClass>
<arguments>
<argument>setup</argument>
</arguments>
</configuration>
</plugin>

下面是使用 postgresql-embedded 启动 postgresql 实例的 DBSetup 类的摘录:

try {
DownloadConfigBuilder downloadConfigBuilder = new DownloadConfigBuilder();
downloadConfigBuilder.defaultsForCommand(Command.Postgres);
downloadConfigBuilder.proxyFactory(new HttpProxyFactory(PROXY_ADDRESS, DEFAULT_PROXY_PORT));
IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
.defaults(Command.Postgres)
.artifactStore(new ArtifactStoreBuilder()
.defaults(Command.Postgres)
.download(downloadConfigBuilder)).build();

PostgresStarter<PostgresExecutable, PostgresProcess> runtime = PostgresStarter.getInstance(runtimeConfig);
final PostgresConfig config = new PostgresConfig(Version.V9_2_4, new AbstractPostgresConfig.Net(
"localhost", 5432
), new AbstractPostgresConfig.Storage(dbName), new AbstractPostgresConfig.Timeout(),
new AbstractPostgresConfig.Credentials(username, password));
config.getAdditionalInitDbParams().addAll(Arrays.asList(
"-E", "UTF-8",
"--locale=en_US.UTF-8",
"--lc-collate=en_US.UTF-8",
"--lc-ctype=en_US.UTF-8"
));

exec = runtime.prepare(config);
process = exec.start();
System.out.println("embedded Postgres started");
Thread.sleep(1200L);

} catch (IOException e) {
System.out.println("Something Went Wrong initializing embedded Postgres: " + e);
e.printStackTrace();
}
catch (InterruptedException e) {
System.out.println("Something Went Wrong Pausing this thread " + e);
e.printStackTrace();
}

请注意,我没有在启动 Postgres 实例的方法中调用 process.stop();。因此,我目前无法关闭 数据库进程。一旦退出此类 DbSetup,所有对该现有进程的引用都将丢失。并且 Postgres 进程永远不会关闭。事实上,jetty 容器似乎也不会关闭,整个 maven 作业都挂了,所以我必须手动杀死它(摘 self 的 maven 控制台输出):

[INFO] --- exec-maven-plugin:1.2.1:java (Run Postgres DB start/schema setup) @ BLAHBLAH ---
***START of Postgres DB Setup Process ***
Extract /Users/myUserName/.embedpostgresql/postgresql-9.2.4-1-osx-binaries.zip START
...................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................Extract /Users/myUserName/.embedpostgresql/postgresql-9.2.4-1-osx-binaries.zip DONE
INFO:20161021 12:58:00: de.flapdoodle.embed.process.runtime.Executable de.flapdoodle.embed.process.runtime.Executable start AbstractPostgresConfig{storage=Storage{dbDir=/var/folders/8g/69wh31fn7nx3q81phwfdpld00000gn/T/postgresql-embed-66cfc41f-0e16-439f-a24b-6e5b6dbc683d/db-content-3bc4b9cc-dd21-43a7-9058-285767f5c53d, dbName='BLAH', isTmpDir=true}, network=Net{host='localhost', port=5432}, timeout=Timeout{startupTimeout=15000}, credentials=Credentials{BLAH, BLAH}, args=[], additionalInitDbParams=[-E, UTF-8, --locale=en_US.UTF-8, --lc-collate=en_US.UTF-8, --lc-ctype=en_US.UTF-8]}
INFO:20161021 12:58:01: de.flapdoodle.embed.process.runtime.Executable de.flapdoodle.embed.process.runtime.Executable start AbstractPostgresConfig{storage=Storage{dbDir=/var/folders/8g/69wh31fn7nx3q81phwfdpld00000gn/T/postgresql-embed-66cfc41f-0e16-439f-a24b-6e5b6dbc683d/db-content-3bc4b9cc-dd21-43a7-9058-285767f5c53d, dbName='BLAH', isTmpDir=true}, network=Net{host='localhost', port=5432}, timeout=Timeout{startupTimeout=15000}, credentials=Credentials{BLAH, BLAH}, args=[BLAH], additionalInitDbParams=[]}
INFO:20161021 12:58:04: de.flapdoodle.embed.process.runtime.Executable de.flapdoodle.embed.process.runtime.Executable start AbstractPostgresConfig{storage=Storage{dbDir=/var/folders/8g/69wh31fn7nx3q81phwfdpld00000gn/T/postgresql-embed-66cfc41f-0e16-439f-a24b-6e5b6dbc683d/db-content-3bc4b9cc-dd21-43a7-9058-285767f5c53d, dbName='BLAH', isTmpDir=true}, network=Net{host='localhost', port=5432}, timeout=Timeout{startupTimeout=15000}, credentials=Credentials{BLAH, BLAH}, args=[], additionalInitDbParams=[-E, UTF-8, --locale=en_US.UTF-8, --lc-collate=en_US.UTF-8, --lc-ctype=en_US.UTF-8]}
embedded Postgres started
***END of Postgres DB Setup Process ***

...

[INFO] --- jetty-maven-plugin:9.1.2.v20140210:run-war (start-jetty) @ BLAH ---
[INFO] Configuring Jetty for project: BlahProject
[INFO] Context path = /
[INFO] Tmp directory = some/path/to/my/webapp/target/tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides = none
[INFO] jetty-9.1.2.v20140210
[INFO] Scanned 1 container path jars, 133 WEB-INF/lib jars, 1 WEB-INF/classes dirs in 1887ms for context o.e.j.m.p.JettyWebAppContext@444942b0{/,file:/some/path/to/my/webapp/,STARTING}{/some/path/to/my/Application-2.3.33+46be96b464dc5b57b2e2e04ce31718a01360e5fb.war}
[INFO] Initializing Spring root WebApplicationContext
INFO:20161021 12:58:27: org.springframework.web.context.ContextLoader org.springframework.web.context.ContextLoader Root WebApplicationContext: initialization started
INFO:20161021 12:58:27: org.springframework.web.context.support.XmlWebApplicationContext org.springframework.context.support.AbstractApplicationContext Refreshing Root WebApplicationContext: startup date [Fri Oct 21 12:58:27 EDT 2016]; root of context hierarchy
INFO:20161021 12:58:27: org.springframework.beans.factory.xml.XmlBeanDefinitionReader org.springframework.beans.factory.xml.XmlBeanDefinitionReader Loading XML bean definitions from class path resource [spring/app-config.xml]
INFO:20161021 12:58:27: org.springframework.beans.factory.xml.XmlBeanDefinitionReader org.springframework.beans.factory.xml.XmlBeanDefinitionReader Loading XML bean definitions from class path resource [spring/db-config.xml]
INFO:20161021 12:58:28: org.springframework.beans.factory.support.DefaultListableBeanFactory org.springframework.beans.factory.support.DefaultListableBeanFactory Overriding bean definition for bean 'endpointLTERepository': replacing [Root bean: class [org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Root bean: class [org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]

[INFO] Started ServerConnector@3a8f9130{HTTP/1.1}{0.0.0.0:8080}
[INFO] Started Jetty Server

/Maven控制台输出结束

我知道嵌入 postgresql 的组件旨在在同一个单元测试中启动和关闭 Postgres 数据库实例。我正在尝试找到一种使用它的方法,它比最初预期的用例更进一步。本质上,我想要某种postgresql-embedded 服务,它可以从 maven 插件启动,随后可以用于关闭该 postgres 进程。

关于如何创建/构建这样的服务和/或插件有什么建议吗?

最佳答案

这里的核心问题是能够在插件的两个不同目标之间共享一些状态:一个 start 目标将启动一个进程,然后是一个 stop 会杀死它的目标。做到这一点的一个好方法是使用 ContextEnabled所有 mojos 实现的接口(interface)。它提供了一个 getPluginContext()返回(原始)映射的方法,您可以在其中存储要在 mojo 之间共享的对象。

使用这种方法,您可以将创建的内容存储在插件的 start 目标中,并在 stop 目标中取回。这里有一个简单的例子来展示这一点,其中一个简单的字符串值在 mojos 之间共享。

Set up a Maven plugin project .这基本上归结为具有以下 POM 的项目,这是 Maven 插件的标准 POM,使用 Java 8 和注释进行配置:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sample.plugin</groupId>
<artifactId>test-maven-plugin</artifactId>
<version>1.0.0</version>
<packaging>maven-plugin</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.5</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.3.9</version>
</dependency>

<!-- dependencies to annotations -->
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

注意 maven-plugin 类型的打包,它向 Maven 声明这是一个插件项目。在这个新项目中,考虑以下 StartMojo:

@Mojo(name = "start", defaultPhase = LifecyclePhase.PRE_INTEGRATION_TEST)
public class StartMojo extends AbstractMojo {

@SuppressWarnings("unchecked")
@Override
public void execute() throws MojoExecutionException {
getPluginContext().put("myService", new MyService("foo"));
}

}

这是在声明一个新的 start mojo默认绑定(bind)到 pre-integration-test阶段。它检索插件上下文并在其中放入一个新对象。在上面,它是一个简单的自定义 POJO,称为 MyService,它在其构造函数中获取一个值。此对象映射到 “myService” 的键,用作查找。

那么,我们可以有:

@Mojo(name = "stop", defaultPhase = LifecyclePhase.POST_INTEGRATION_TEST)
public class StopMojo extends AbstractMojo {

@Override
public void execute() throws MojoExecutionException {
MyService service = (MyService) getPluginContext().get("myService");
getLog().info(service.getValue());
}

}

这是声明一个新的 stop mojo,它默认绑定(bind)到 post-integration-test阶段。它检索插件上下文,提取键 “myService” 下的对象,最后获取它的值并记录它。

将此 Maven 插件打包并安装(使用 mvn clean install)到本地存储库后,您可以在示例项目中使用它

<plugin>
<groupId>sample.plugin</groupId>
<artifactId>test-maven-plugin</artifactId>
<executions>
<execution>
<id>sample</id>
<goals>
<goal>start</goal>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>

如果您在该示例项目上运行 mvn clean verify,您最终会在 post-integration 的日志中打印 "foo" -test 阶段。这表明该值已由 start mojo 正确设置,然后由 stop mojo 正确检索。

当然,您可以在此映射中存储复杂的对象,而不仅仅是 String(可能有更简单的解决方案)。值得注意的是,它可能是您要停止的 process 实例的主机。您可以删除 exec-maven-plugin,创建一个新的 Maven 插件,其中包含您在 start 目标中设置嵌入式数据库所需的代码,存储在此目标的插件上下文中处理实例,并最终在另一个 stop mojo 中通过从插件上下文中检索它来停止此过程。

关于java - 如何将 Embedded PostgreSQL Server Java 组件用作单独的服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40182104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com