如何将嵌入式 PostgreSQL Server Java 组件作为单独的服务使用?

2024-04-18

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

根据我所了解到的情况,我相信实现这一目标的关键是执行以下步骤(按顺序):

  1. 在预集成测试阶段以某种方式启动 postgresql DB 的“嵌入式”版本(和模式设置)Maven生命周期 http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html,让 postgres-db-process 在后台运行
  2. 启动加载我的服务应用程序的 Java 容器:我正在使用 Jetty 9.1.5 插件
  3. 在集成测试阶段从 Failsafe 插件运行我的(基于 JUnit 的)测试
  4. 在 Maven 的集成后测试阶段关闭我的 Jetty 容器
  5. 最后,有一些机制shutdown先前在生命周期的集成测试阶段启动的(后台)postgres-db-process(终止/清理此进程)

在我当前的实现中,成功完成步骤 1 - 3。在第 2 步中,它使用了 exec-maven-plugin,该插件调用一个 Java 类,该类使用postgresql 嵌入式 Java 组件 https://github.com/yandex-qatools/postgresql-embedded。 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();
    }

请注意,我不是在打电话process.stop();在启动 Postgres 实例的方法中。所以,我目前没有办法shutdown数据库进程。一旦我退出这门课DbSetup,所有对该现有流程的引用都将丢失。并且 Postgres 进程永远不会关闭。事实上,jetty 容器似乎也不会关闭,并且整个 Maven 作业挂起,所以我必须手动终止它(摘自我的 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 嵌入式服务某种可以从 Maven 插件启动的东西,然后可以用来关闭该 postgres 进程。

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


这里的核心问题是能够在插件的两个不同目标之间共享某些状态:start目标将启动一个流程,然后stop会杀死它的目标。一个好的方法是利用ContextEnabled https://maven.apache.org/ref/3.3.9/maven-plugin-api/apidocs/org/apache/maven/plugin/ContextEnabled.html所有 mojo 都实现的接口。它提供了一个getPluginContext() https://maven.apache.org/ref/3.3.9/maven-plugin-api/apidocs/org/apache/maven/plugin/ContextEnabled.html#getPluginContext()返回一个(原始)映射的方法,您可以在其中存储要在 mojo 之间共享的对象。

通过这种方法,您可以将您创建的内容存储在start插件的目标,并将其返回到stop目标。下面是一个简单的示例来展示这一点,其中一个简单的字符串值在 mojo 之间共享。

设置 Maven 插件项目 https://maven.apache.org/guides/plugin/guide-java-plugin-development.html。这基本上归结为拥有一个具有以下 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 https://maven.apache.org/plugin-tools-archives/plugin-tools-3.5/maven-plugin-annotations/apidocs/org/apache/maven/plugins/annotations/Mojo.html默认情况下绑定到pre-integration-test https://maven.apache.org/plugin-tools-archives/plugin-tools-3.5/maven-plugin-annotations/apidocs/org/apache/maven/plugins/annotations/LifecyclePhase.html#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());
    }

}

这是宣告一个新的stopmojo 默认绑定到post-integration-test https://maven.apache.org/plugin-tools-archives/plugin-tools-3.5/maven-plugin-annotations/apidocs/org/apache/maven/plugins/annotations/LifecyclePhase.html#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在那个示例项目中,你最终会得到"foo"打印在您的日志中post-integration-test阶段。这表明该值已正确设置startmojo,然后由stop mojo.

当然,您可以在此地图中存储复杂的对象,而不仅仅是一个String(对此可能有更简单的解决方案)。值得注意的是,它可能是您的主机process您想要停止的实例。你可以摆脱exec-maven-plugin,创建一个新的 Maven 插件,其中包含您已经拥有的用于在 a 中设置嵌入式数据库的代码start目标,将流程实例存储在该目标的插件上下文中,最后在另一个目标中停止该流程stop通过从插件上下文中检索它来实现魔力。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将嵌入式 PostgreSQL Server Java 组件作为单独的服务使用? 的相关文章

随机推荐

  • 使 K 不同(基数) google OR-TOOLS

    我想知道 google or tools 中是否存在 Solver AllDifferent x 的泛化 允许指定我允许的不同元素的数量 因此 如果 len x 4 则 AllDifferent x 意味着 len set x 4 但是 如
  • 如何让 js2-mode 在 Emacs 中使用空格而不是制表符?

    我在用js2 mode http code google com p js2 mode 在 Emacs 中编辑 Javascript 但我似乎无法让它停止使用制表符而不是空格进行缩进 我的其他模式工作正常 只是遇到 js2 问题 你有 se
  • HtmlAgilityPack获取Title和meta

    我尝试练习 HtmlAgilityPack 但我遇到了一些与此相关的问题 这是我编码的内容 但我无法正确获取网页的标题和描述 如果有人能启发我纠正我的错误 public static void Main string args string
  • 在 R 中测试多个相同的列

    有没有一种简单的方法来测试身份multiple列 例如 通过这个输入 data data table one c 1 2 3 4 two c 7 8 9 10 three c 1 2 3 4 four c 1 2 3 4 有什么东西可以返回
  • 如何向 HPA 提供外部指标?

    问题设置 假设我有 2 个 pod A 和 B 我希望能够根据任意来源的任意数量动态扩展 pod A 假设 pod B 是这样一个源 例如 它可以拥有一个带有端点的 HTTP 服务器 该端点在请求时以 pod A 所需的副本数量进行响应 或
  • 如何使用 LiveData 和 ViewModel 发布改进 API 调用请求方法

    我是 android 和 java 的初学者 我在使用mvvm架构登录时遇到问题 并在android studio中用JAVA语言进行改造 我的代码如下所示 我的代码如下所示 API接口 FormUrlEncoded POST login
  • 从简单的 JSON 字符串加载 D3.js 数据

    图库中的大多数示例都会从 TSV 文件加载数据 如何将以下内容转换为使用本地 json 变量而不是 TSV 数据 d3 tsv data tsv function error data var myEntitiesJson getEntit
  • SQL Server:将 GROUP BY 的结果拆分为单独的列

    我有一个 SQL Server 2008 R2 数据库 其中包含大约 5 亿行数据 目前看起来像这样 ID Eventtype 201 1 201 3 201 4 201 1 201 1 664 1 664 0 664 1 664 3 我似
  • 在没有朋友的情况下给予基类受保护的访问

    我先解释一下我的情况 我有一个基类 它自动实现一种引用计数 它允许我包装 C 风格init and free 库调用引用计数 API template
  • 将 Spring Boot 2.0 与 Tomcat 7.0.82 结合使用

    我有一个使用 Spring Boot 2 0 0 RC2 的项目 我需要使用 Tomcat 7 0 82 的传统部署将其部署到客户环境 我已经成功构建了一场可以通过配置成功部署的战争web xml以 Spring 应用程序的典型方式 使用
  • 如何从 firebug 控制台隐藏 ajax 请求?

    如何隐藏来自 firebug 控制台或任何显示 ajax 调用的 ajax 请求 请在ajax成功或失败后调用此函数 result load testtemplateboth testpagetpl clearconsole functio
  • 在指定时间后自动删除 mongodb 中的文档

    我想自动删除文档collection在 mongodb 中基于ttl 我查看了其他答案并找到了以下方法 db collection createIndex createdAt 1 expireAfterSeconds 3600 这将删除之后
  • PySpark:反序列化 eventhub 捕获 avro 文件中包含的 Avro 序列化消息

    初始情况 AVRO 序列化事件被发送到 azure 事件中心 这些事件使用 azure 事件中心捕获功能持久存储 捕获的数据以及事件中心元数据以 Apache Avro 格式写入 应使用 py Spark 分析捕获 avro 文件中包含的原
  • Google Translator API 和一个单词的多种翻译

    我正在使用 google api translate java 0 92 jar Translate setHttpReferrer http translate google com http translate google com t
  • LWJGL 窗口具有透明背景?

    我想创建一个没有 黑色背景 区域的窗口 但您可以看到任何其他打开的窗口等 也就是说 渲染场景并且仅渲染场景 不留框架 不留背景区域 我读过一种方法 该方法涉及渲染到隐藏的 OpenGL 窗口并将其缓冲在内存中 创建透明分层窗口以及从内存复制
  • 如何强制 Idea 和 Maven 下载我的项目的所有源代码?

    我的 Java 项目是使用 Maven 构建的 并在 Intellij Idea 的帮助下编写的 我有很多开源项目依赖项 我想广泛研究它们以了解它们是如何工作的 为此 我经常在 Idea 中使用用法搜索 即 查找用法 选项 它告诉我在哪里可
  • 使用 Groovy 在 Java 属性中进行变量扩展

    我经常使用标准 Java 属性文件来配置我的 Groovy 应用程序 我一直缺少的一项功能是能够使用变量作为属性值的一部分 以便它们可以在使用过程中动态扩展 我想我可以使用以下设计提供此功能 使用特殊的格式来注释应该扩展的属性 我选择将此类
  • Zookeeper 错过了连续更改的事件

    我目前有一个带有单个 Zookeeper 节点和 Curator 的设置来访问数据 读取数据是通过 Curator TreeCache 完成的 我有以下测试 public void test callback successive chan
  • GNU 链接器:适应名称修改算法的更改

    我正在尝试重新编译现有的 C 应用程序 不幸的是 我必须依赖一个专有库 我只有一个预编译的静态存档 我使用 g 版本 7 3 0 和 ld 版本 2 30 无论它是用什么 GCC 版本编译的 它都是古老的 头文件定义了方法 class fo
  • 如何将嵌入式 PostgreSQL Server Java 组件作为单独的服务使用?

    我正在尝试为基于 RESTful 服务 Java 的应用程序创建一个全面的集成测试套件 该应用程序在 Tomcat 7 x 中运行并依赖于 Postgresql 9 x 实例 此外 我希望能够将这个套件作为一个独立的进程运行 如果可能的话