- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试为在 Tomcat(7.x) 中运行并依赖于 Postgresql (9.x) 实例的基于 RESTful(服务)Java 的应用程序创建一个全面的集成测试套件。此外,如果可能的话,我希望能够通过使用 maven failsafe 插件,将这个套件作为一个独立的进程运行,专门来自 maven 3.x。这样,测试就可以在 3 个主要平台(Mac OSX、Linux 和 Windows)上运行。
据我所知,我认为实现这一目标的关键是执行以下步骤(按此顺序):
在我当前的实现中,成功完成了步骤 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"
-teststart
mojo 正确设置,然后由 stop
mojo 正确检索。
当然,您可以在此映射中存储复杂的对象,而不仅仅是 String
(可能有更简单的解决方案)。值得注意的是,它可能是您要停止的 process
实例的主机。您可以删除 exec-maven-plugin
,创建一个新的 Maven 插件,其中包含您在 start
目标中设置嵌入式数据库所需的代码,存储在此目标的插件上下文中处理实例,并最终在另一个 stop
mojo 中通过从插件上下文中检索它来停止此过程。
关于java - 如何将 Embedded PostgreSQL Server Java 组件用作单独的服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40182104/
ASIDE:是的,这可以被认为是一个主观问题,但我希望从回答的统计数据中得出结论。 有各种各样的计算设备。它们的物理尺寸、计算能力和电力各不相同。我想知道嵌入式开发人员认为什么是使系统“嵌入式”的决定
当我听到这个时,我总是想到移动设备。但为什么硬件“嵌入”在那里?整个设备不就是硬件吗?为什么个人电脑没有嵌入式硬件系统? 最佳答案 在当今世界,嵌入式仅指具有以下一项或多项特征的系统: 单一用途(即,
我想测试嵌入式 PowerBI 所以我下载了 the sample app能够发布 pbix 文件并嵌入它。 所以我创建了最简单的 PowerBI 文件,可以使用 Azure SQL 制作,使用 Di
我需要问几个关于词嵌入的问题......可能是基本的。 当我们转换一个词的 one-hot 向量时,例如 king [0 0 0 1 0]嵌入向量 E = [0.2, 0.4, 0.2, 0.2] .
我想知道如何将 CEF 添加到我的 Yocto 项目中。此时,我还没有对项目进行任何修改。它由我们的电路板制造商提供。该板有一个ARM 9。 最佳答案 直接在 CEF 论坛上问这个问题是个好主意,可能
实体是否可以访问其 Embedded 对象的 Embedded?例如: @Embeddable public class Address { @Embedded protected A
我有一个类似于这个的 Morphia 架构: @Entity class BlogEntry { @Embedded List comments } @Embedded class B
我的 pom.xml 中有以下插件配置: com.day.jcr.vault maven-vault-plugi
直到现在,我仍然对 Openembedded-core 和 meta-openembedded 中的食谱感到困惑。很多时候,很难将食谱放在正确的目录中。它们真的很相似,但在食谱的内容上似乎如此不同。
我正在尝试使用此处找到的 Tensorflow 运行单词教程的矢量表示: http://www.tensorflow.org/tutorials/word2vec/index.md 第一个脚本 wor
谁能帮我解释一下 power BI premium 和 power BI Embedded 之间的区别? 最佳答案 Power BI Embedded 容量(也称为 SKU)是 billed hour
我在执行一个 MongoDB 请求时遇到了一些麻烦。我在 Node.js 上下文中使用 MongoDB 3.2 和 Mongoose。这是文档: { _id: ObjectId('12345'),
Xcode 常规选项卡中的“嵌入式二进制文件”和构建阶段选项卡中的“嵌入式框架”有什么区别? General 选项卡中的“Linked Frameworks”和 Build Phrases 选项卡中的
我正在尝试执行 maven install在 pom 上,显示的结果是: Grave: SEC5054: Certificate has expired 此结果会在测试执行开始后立即出现。 我一直在搜
我正在研究 ppc32 和 ppc64 架构来为我的编译器实现一个新的后端,但是我对一个函数的序言有疑问,我已经阅读了几个关于 PowerPC 的 IBM 文档,但是我读到的关于堆栈的信息很少.一个程
我大部分时间都在使用 Atmel Studio 等工具和 IDE 开发微 Controller ,并抽象出幕后发生的事情。 假设在这种情况下,我们直接从闪存执行代码,这在嵌入式系统中可能是这种情况。
我刚刚发现我在(Cortex M0)上编写代码的ARM不支持未对齐的内存访问。 现在,在我的代码中,我使用了很多打包结构,并且从未收到任何警告或硬故障,所以当Cortex不允许不对齐访问时,Corte
我正在研究 Uboot bootstrap 。我有一些关于 Bootloader 的功能和它要处理的应用程序的基本问题: Q1:据我所知,引导加载程序用于将应用程序下载到内存中。在互联网上,我还发现引
我想了解基本的RISC架构。经过一些研发,我想使用MIPS架构。但是,我没有获得有关带有MIPS处理器的嵌入式开发板的良好信息。 如果有人可以提出好的董事会建议,将会有很大的帮助。 问候, 拉姆吉 最
考虑我们正在为裸机 MCU 编写固件,即没有操作系统。有人告诉我不可能(非法?)将参数传递给中断处理函数? 我无法准确理解为什么会这样?这有什么问题? 附注。是否可以在某些 RTOS-es、嵌入式 L
我是一名优秀的程序员,十分优秀!