我是第一次使用 AWS SQS。我在某家公司工作,由于某些我无法控制的原因,该公司通过防火墙阻止我们从本地网络连接到 SQS。我可以将我开发的任何东西上传到 AWS 中的 TomCat,甚至可以通过我们的 ec2 用户通过 Linux 命令行运行(通过 WinSCP 上传并通过 Putty 触发)。
由于我需要使用 sqs 提供某些解决方案,但我无法从我的 Eclipse 调试访问它,所以我有兴趣模拟 sqs。此外,我从测试的角度看到了这种方法的优势。也就是说,经过几个小时的搜索,我找到了我需要的东西:http ://www.schibsted.pl/blog/mocking-amazon-sqs-with-elasticmq/
我下载了示例(https://github.com/JanGurda/elastc-mq-rule-sample),但无法启动。我知道嵌入了一个 elasticmq,它应该与这样的 jar 一起启动。我通过下载 elasticmq 并在运行示例之前启动它尝试了其他方式,但我仍然得到相同的输出。
基本上,我得到了这个输出
usage: java -jar project.jar [-h] [-v] {server,check} ...
positional arguments:
{server,check} available commands
optional arguments:
-h, --help show this help message and exit
-v, --version show the application version and exit
似乎我在做一些非常愚蠢的事情,但我没有找到北方。PS。我检查了一下,pom 有 lombok 和 elasticmq 正确设置。
绒球
<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>pl.schibsted.spid</groupId>
<artifactId>elastc-mq-rule-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>0.8.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-sqs</artifactId>
<version>1.10.1</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-testing</artifactId>
<version>0.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.elasticmq</groupId>
<artifactId>elasticmq-rest-sqs_2.11</artifactId>
<version>0.8.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>pl.schibsted.spid.elasticmq.server.ElasticMqRuleSampleApplication</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>**/ITest*.java</include>
</includes>
</configuration>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
DropwizardAppRule:
public class ITestPingResource {
@ClassRule
public static DropwizardAppRule<ElasticMqRuleSampleApplicationConfiguration> app =
new DropwizardAppRule<>(ElasticMqRuleSampleApplication.class,
ITestPingResource.class.getClassLoader().getResource("test.yml").getPath());
@ClassRule
public static SqsRule sqs = new SqsRule(SqsRuleConfiguration.builder()
.queue("sample-queue").port(8888).build());
private Client client = ClientBuilder.newClient();
@After
public void tearDown() {
sqs.purgeAllQueues();
}
@Test
public void shouldPublishProcessedRequestPayload() throws Exception {
// given
String toSend = "abcdefgh";
// when
Response response = client
.target("http://127.0.0.1:" + app.getLocalPort() + "/ping")
.request().post(Entity.json(toSend));
// then
assertEquals(Status.NO_CONTENT.getStatusCode(), response.getStatus());
List<Message> messagesFromQueue = sqs.getQueue("sample-queue").read(10);
assertEquals(1, messagesFromQueue.size());
assertEquals("ABCDEFGH", messagesFromQueue.get(0).getBody());
}
}
测试.yml
queueUrl: http://localhost:8888/queue/sample-queue
awsAccessKey: x
awsSecretKey: x
主要的:
public class ElasticMqRuleSampleApplication extends Application<ElasticMqRuleSampleApplicationConfiguration> {
public static void main(String[] args) throws Exception {
new ElasticMqRuleSampleApplication().run(args);
}
@Override
public void run(ElasticMqRuleSampleApplicationConfiguration configuration, Environment environment) throws Exception {
PingResource resource = new PingResource(configuration);
environment.jersey().register(resource);
}
}