forked from vert-x3/vertx-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
415 changed files
with
1,699 additions
and
3,166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
= Vert.x Proton examples | ||
|
||
Here you will find examples demonstrating the Vert.x Proton AMQP library in action. | ||
|
||
This component facilitates AMQP integrations for Vert.x by providing a thin wrapper around the [Apache Qpid](http://qpid.apache.org) Proton-J AMQP 1.0 protocol engine. | ||
Please consult the Vert.x Proton documentation for more information. | ||
|
||
**NOTE: The Client examples require a server with AMQP 1.0 support listening at port 5672 on localhost and offering SASL ANONYMOUS.** | ||
|
||
For instance, you could use a server such as Apache ActiveMQ (5.x or Artemis), Apache Qpid (Dispatch router, Broker-J or C++ broker), or the Vert.x Proton server example. | ||
|
||
== Client | ||
|
||
These examples demonstrate messaging between a client sender, client receiver, and a server. They can be used in conjunction with each other to send and receive messages. | ||
|
||
The Receiver example consumes incoming messages from an address, prints their content, and accepts them. The Sender example sends a message to that address every second. | ||
|
||
link:src/main/java/io/vertx/example/proton/client/Receiver.java[Receiver] + | ||
link:src/main/java/io/vertx/example/proton/client/Sender.java[Sender] + | ||
|
||
== Server | ||
|
||
This server example listens for incoming connections on port 5672. It prints any messages received from client senders, and periodically sends generated messages to client receivers. | ||
link:src/main/java/io/vertx/example/proton/server/HelloServer.java[HelloServer] + |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?xml version="1.0" encoding="UTF-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>io.vertx</groupId> | ||
<artifactId>amqp-proton-examples</artifactId> | ||
<version>3.6.0</version> | ||
|
||
<dependencies> | ||
<!-- primary deps --> | ||
<dependency> | ||
<groupId>io.vertx</groupId> | ||
<artifactId>vertx-proton</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<pluginManagement> | ||
<plugins> | ||
<!-- We specify the Maven compiler plugin as we need to set it to Java 1.8 --> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.1</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</pluginManagement> | ||
</build> | ||
|
||
<profiles> | ||
<profile> | ||
<id>staging</id> | ||
<repositories> | ||
<repository> | ||
<id>staging</id> | ||
<url>https://oss.sonatype.org/content/repositories/iovertx-3750/</url> | ||
</repository> | ||
</repositories> | ||
</profile> | ||
</profiles> | ||
</project> |
59 changes: 59 additions & 0 deletions
59
amqp-proton-examples/src/main/java/io/vertx/example/proton/client/Receiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.vertx.example.proton.client; | ||
|
||
import io.vertx.proton.ProtonClient; | ||
import io.vertx.proton.ProtonConnection; | ||
|
||
import org.apache.qpid.proton.amqp.messaging.AmqpValue; | ||
|
||
import io.vertx.core.AbstractVerticle; | ||
import io.vertx.example.util.Runner; | ||
|
||
public class Receiver extends AbstractVerticle { | ||
|
||
private String address = "examples"; | ||
|
||
// Convenience method so you can run it in your IDE | ||
public static void main(String[] args) { | ||
Runner.runExample(Receiver.class); | ||
} | ||
|
||
@Override | ||
public void start() throws Exception { | ||
ProtonClient client = ProtonClient.create(vertx); | ||
|
||
client.connect("localhost", 5672, res -> { | ||
if(!res.succeeded()) { | ||
System.out.println("Connect failed: " + res.cause()); | ||
return; | ||
} | ||
|
||
ProtonConnection connection = res.result(); | ||
connection.open(); | ||
|
||
connection.createReceiver(address).handler((delivery, msg) -> { | ||
String content = (String) ((AmqpValue) msg.getBody()).getValue(); | ||
System.out.println("Received message with content: " + content); | ||
|
||
// By default, receivers automatically accept (and settle) the delivery | ||
// when the handler returns, if no other disposition has been applied. | ||
// To change this and always manage dispositions yourself, use the | ||
// setAutoAccept method on the receiver. | ||
}).open(); | ||
}); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
amqp-proton-examples/src/main/java/io/vertx/example/proton/client/Sender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.vertx.example.proton.client; | ||
|
||
import io.vertx.proton.ProtonClient; | ||
import io.vertx.proton.ProtonConnection; | ||
import io.vertx.proton.ProtonSender; | ||
|
||
import static io.vertx.proton.ProtonHelper.message; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.apache.qpid.proton.message.Message; | ||
|
||
import io.vertx.core.AbstractVerticle; | ||
import io.vertx.example.util.Runner; | ||
|
||
public class Sender extends AbstractVerticle { | ||
|
||
private String address = "examples"; | ||
private AtomicInteger sent = new AtomicInteger(); | ||
|
||
// Convenience method so you can run it in your IDE | ||
public static void main(String[] args) { | ||
Runner.runExample(Sender.class); | ||
} | ||
|
||
@Override | ||
public void start() throws Exception { | ||
ProtonClient client = ProtonClient.create(vertx); | ||
|
||
client.connect("localhost", 5672, res -> { | ||
if(!res.succeeded()) { | ||
System.out.println("Connect failed: " + res.cause()); | ||
return; | ||
} | ||
|
||
ProtonConnection connection = res.result(); | ||
connection.open(); | ||
|
||
ProtonSender sender = connection.createSender(address); | ||
|
||
// Can optionally add an openHandler and/or sendQueueDrainHandler | ||
// to await remote sender open completing and credit to send being | ||
// granted. Here we will just schedule sends to happen if there | ||
// is credit at the time. | ||
sender.open(); | ||
|
||
// Schedule sending of a message every second | ||
System.out.println("Sender created, scheduling sends."); | ||
|
||
vertx.setPeriodic(1000, x -> { | ||
if(!sender.sendQueueFull()) { | ||
final int msgNum = sent.incrementAndGet(); | ||
Message message = message("Hello " + msgNum); | ||
|
||
sender.send(message, delivery -> { | ||
System.out.println(String.format("Message " + msgNum + " was received by the server: remote state=%s", delivery.getRemoteState())); | ||
}); | ||
|
||
System.out.println("Sent message: " + msgNum); | ||
} else { | ||
System.out.println("No credit to send, waiting."); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
Oops, something went wrong.