-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename proton examples -> AMQP examples and use Vert.x AMQP client in…
…stead of Proton for sender/receiver verticles.
- Loading branch information
Showing
11 changed files
with
193 additions
and
567 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
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
66 changes: 66 additions & 0 deletions
66
amqp-examples/src/main/java/io/vertx/example/amqp/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,66 @@ | ||
/* | ||
* 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.amqp.client; | ||
|
||
import io.vertx.amqp.AmqpClient; | ||
import io.vertx.amqp.AmqpClientOptions; | ||
import io.vertx.amqp.AmqpConnection; | ||
import io.vertx.amqp.AmqpReceiver; | ||
import io.vertx.core.Future; | ||
import io.vertx.core.VerticleBase; | ||
import io.vertx.launcher.application.VertxApplication; | ||
|
||
public class Receiver extends VerticleBase { | ||
|
||
private String address = "examples"; | ||
|
||
public static void main(String[] args) { | ||
VertxApplication.main(new String[]{Receiver.class.getName()}); | ||
} | ||
|
||
private AmqpClient client; | ||
private AmqpConnection connection; | ||
private AmqpReceiver receiver; | ||
|
||
@Override | ||
public Future<?> start() { | ||
client = AmqpClient.create(vertx, new AmqpClientOptions() | ||
.setPort(5672) | ||
.setHost("localhost") | ||
); | ||
|
||
return client.connect().compose(conn -> conn | ||
.createReceiver(address) | ||
.andThen(ar -> { | ||
if (ar.succeeded()) { | ||
connection = conn; | ||
receiver = ar.result(); | ||
|
||
receiver.handler(msg -> { | ||
System.out.println("Received message with content: " + msg.bodyAsString()); | ||
}); | ||
} else { | ||
conn.close(); | ||
} | ||
})) | ||
.onSuccess(v -> System.out.println("Received created")); | ||
} | ||
|
||
@Override | ||
public Future<?> stop() { | ||
return client.close(); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
amqp-examples/src/main/java/io/vertx/example/amqp/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,79 @@ | ||
/* | ||
* 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.amqp.client; | ||
|
||
import io.vertx.amqp.*; | ||
import io.vertx.core.Future; | ||
import io.vertx.core.VerticleBase; | ||
import io.vertx.launcher.application.VertxApplication; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import static io.vertx.proton.ProtonHelper.message; | ||
|
||
public class Sender extends VerticleBase { | ||
|
||
private String address = "examples"; | ||
private AtomicInteger sent = new AtomicInteger(); | ||
|
||
public static void main(String[] args) { | ||
VertxApplication.main(new String[]{Sender.class.getName()}); | ||
} | ||
|
||
private AmqpClient client; | ||
private AmqpConnection connection; | ||
private AmqpSender sender; | ||
|
||
@Override | ||
public Future<?> start() throws Exception { | ||
AmqpClient client = AmqpClient.create(vertx, new AmqpClientOptions() | ||
.setPort(5672) | ||
.setHost("localhost")); | ||
|
||
return client.connect().compose(conn -> conn | ||
.createSender(address) | ||
.andThen(ar -> { | ||
if (ar.succeeded()) { | ||
connection = conn; | ||
sender = ar.result(); | ||
|
||
// Schedule sending of a message every second | ||
System.out.println("Sender created, scheduling sends."); | ||
|
||
vertx.setPeriodic(1000, x -> { | ||
if(!sender.writeQueueFull()) { | ||
final int msgNum = sent.incrementAndGet(); | ||
AmqpMessage message = AmqpMessage.create().withBody("Hello " + msgNum + " from Sender").build(); | ||
|
||
sender.sendWithAck(message).onComplete(ack -> { | ||
if (ack.succeeded()) { | ||
System.out.printf("Message " + msgNum + " was received by the server%n"); | ||
} else { | ||
System.out.println("Ack failed " + ack.cause().getMessage()); | ||
} | ||
}); | ||
|
||
System.out.println("Sent message: " + msgNum); | ||
} else { | ||
System.out.println("No credit to send, waiting."); | ||
} | ||
}); | ||
} else { | ||
conn.close(); | ||
} | ||
})); | ||
} | ||
} |
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
Oops, something went wrong.