use of io.aeron.Publication in project Aeron by real-logic.
the class MultiplePublishersWithFragmentation method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
*/
public static void main(final String[] args) {
System.out.println("Publishing to " + CHANNEL + " on stream id " + STREAM_ID_1 + " and stream id " + STREAM_ID_2);
try (Aeron aeron = Aeron.connect();
Publication publication1 = aeron.addPublication(CHANNEL, STREAM_ID_1);
Publication publication2 = aeron.addPublication(CHANNEL, STREAM_ID_2)) {
int j = 1;
int k = 1;
final String message1 = "Hello World! " + j;
BUFFER_1.putBytes(0, message1.getBytes());
final String message2 = "Hello World! " + k;
BUFFER_2.putBytes(0, message2.getBytes());
while (j <= 5000 || k <= 5000) {
boolean offerStatus1 = false;
boolean offerStatus2 = false;
long result1;
long result2;
while (!(offerStatus1 || offerStatus2)) {
if (j <= 5000) {
result1 = publication1.offer(BUFFER_1, 0, BUFFER_1.capacity());
if (result1 > 0) {
j++;
offerStatus1 = true;
System.out.println("Successfully sent data on stream id " + STREAM_ID_1 + " and data length " + BUFFER_1.capacity() + " at offset " + result1);
} else {
if (result1 == Publication.BACK_PRESSURED) {
System.out.println(" Offer failed due to back pressure for stream id " + STREAM_ID_1);
} else if (result1 == Publication.NOT_CONNECTED) {
System.out.println(" Offer failed because publisher is not yet " + "connected to subscriber for stream id " + STREAM_ID_1);
} else {
System.out.println(" Offer failed due to unexpected reason: " + result1);
}
offerStatus1 = false;
}
}
if (k <= 5000) {
result2 = publication2.offer(BUFFER_2, 0, BUFFER_2.capacity());
if (result2 > 0) {
k++;
offerStatus2 = true;
System.out.println("Successfully sent data on stream id " + STREAM_ID_2 + " and data length " + BUFFER_2.capacity() + " at offset " + result2);
} else {
if (result2 == Publication.BACK_PRESSURED) {
System.out.println(" Offer failed because publisher is not yet " + "connected to subscriber for stream id " + STREAM_ID_2);
} else if (result2 == Publication.NOT_CONNECTED) {
System.out.println("Offer failed - publisher is not yet connected to subscriber" + STREAM_ID_2);
} else {
System.out.println("Offer failed due to unexpected reason: " + result2);
}
offerStatus2 = false;
}
}
}
}
System.out.println("Done sending total messages for stream id " + STREAM_ID_1 + " = " + (j - 1) + " and stream id " + STREAM_ID_2 + " = " + (k - 1));
}
}
use of io.aeron.Publication in project Aeron by real-logic.
the class Pong method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
*/
public static void main(final String[] args) {
final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
final Aeron.Context ctx = new Aeron.Context();
if (EMBEDDED_MEDIA_DRIVER) {
ctx.aeronDirectoryName(driver.aeronDirectoryName());
}
if (INFO_FLAG) {
ctx.availableImageHandler(SamplesUtil::printAvailableImage);
ctx.unavailableImageHandler(SamplesUtil::printUnavailableImage);
}
final IdleStrategy idleStrategy = new BusySpinIdleStrategy();
System.out.println("Subscribing Ping at " + PING_CHANNEL + " on stream id " + PING_STREAM_ID);
System.out.println("Publishing Pong at " + PONG_CHANNEL + " on stream id " + PONG_STREAM_ID);
System.out.println("Using exclusive publications " + EXCLUSIVE_PUBLICATIONS);
final AtomicBoolean running = new AtomicBoolean(true);
SigInt.register(() -> running.set(false));
try (Aeron aeron = Aeron.connect(ctx);
Subscription subscription = aeron.addSubscription(PING_CHANNEL, PING_STREAM_ID);
Publication publication = EXCLUSIVE_PUBLICATIONS ? aeron.addExclusivePublication(PONG_CHANNEL, PONG_STREAM_ID) : aeron.addPublication(PONG_CHANNEL, PONG_STREAM_ID)) {
final BufferClaim bufferClaim = new BufferClaim();
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> pingHandler(bufferClaim, publication, buffer, offset, length, header);
while (running.get()) {
idleStrategy.idle(subscription.poll(fragmentHandler, FRAME_COUNT_LIMIT));
}
System.out.println("Shutting down...");
}
CloseHelper.close(driver);
}
use of io.aeron.Publication in project Aeron by real-logic.
the class BasicPublisher method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
* @throws InterruptedException if the thread sleep delay is interrupted.
*/
public static void main(final String[] args) throws InterruptedException {
System.out.println("Publishing to " + CHANNEL + " on stream id " + STREAM_ID);
// If configured to do so, create an embedded media driver within this application rather
// than relying on an external one.
final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
final Aeron.Context ctx = new Aeron.Context();
if (EMBEDDED_MEDIA_DRIVER) {
ctx.aeronDirectoryName(driver.aeronDirectoryName());
}
// clean up resources when this try block is finished
try (Aeron aeron = Aeron.connect(ctx);
Publication publication = aeron.addPublication(CHANNEL, STREAM_ID)) {
final UnsafeBuffer buffer = new UnsafeBuffer(BufferUtil.allocateDirectAligned(256, 64));
for (long i = 0; i < NUMBER_OF_MESSAGES; i++) {
System.out.print("Offering " + i + "/" + NUMBER_OF_MESSAGES + " - ");
final int length = buffer.putStringWithoutLengthAscii(0, "Hello World! " + i);
final long result = publication.offer(buffer, 0, length);
if (result > 0) {
System.out.println("yay!");
} else if (result == Publication.BACK_PRESSURED) {
System.out.println("Offer failed due to back pressure");
} else if (result == Publication.NOT_CONNECTED) {
System.out.println("Offer failed because publisher is not connected to a subscriber");
} else if (result == Publication.ADMIN_ACTION) {
System.out.println("Offer failed because of an administration action in the system");
} else if (result == Publication.CLOSED) {
System.out.println("Offer failed because publication is closed");
break;
} else if (result == Publication.MAX_POSITION_EXCEEDED) {
System.out.println("Offer failed due to publication reaching its max position");
break;
} else {
System.out.println("Offer failed due to unknown reason: " + result);
}
if (!publication.isConnected()) {
System.out.println("No active subscribers detected");
}
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
}
System.out.println("Done sending.");
if (LINGER_TIMEOUT_MS > 0) {
System.out.println("Lingering for " + LINGER_TIMEOUT_MS + " milliseconds...");
Thread.sleep(LINGER_TIMEOUT_MS);
}
}
CloseHelper.close(driver);
}
use of io.aeron.Publication in project Aeron by real-logic.
the class EmbeddedPingPong method runPing.
private static void runPing(final Aeron aeron) throws InterruptedException {
System.out.println("Publishing Ping at " + PING_CHANNEL + " on stream id " + PING_STREAM_ID);
System.out.println("Subscribing Pong at " + PONG_CHANNEL + " on stream id " + PONG_STREAM_ID);
System.out.println("Message payload length of " + MESSAGE_LENGTH + " bytes");
System.out.println("Using exclusive publications: " + EXCLUSIVE_PUBLICATIONS);
final FragmentAssembler dataHandler = new FragmentAssembler(EmbeddedPingPong::pongHandler);
try (Subscription pongSubscription = aeron.addSubscription(PONG_CHANNEL, PONG_STREAM_ID, EmbeddedPingPong::availablePongImageHandler, null);
Publication pingPublication = EXCLUSIVE_PUBLICATIONS ? aeron.addExclusivePublication(PING_CHANNEL, PING_STREAM_ID) : aeron.addPublication(PING_CHANNEL, PING_STREAM_ID)) {
System.out.println("Waiting for new image from Pong...");
PONG_IMAGE_LATCH.await();
System.out.format("Warming up... %d iterations of %,d messages%n", WARMUP_NUMBER_OF_ITERATIONS, WARMUP_NUMBER_OF_MESSAGES);
for (int i = 0; i < WARMUP_NUMBER_OF_ITERATIONS; i++) {
roundTripMessages(dataHandler, pingPublication, pongSubscription, WARMUP_NUMBER_OF_MESSAGES);
Thread.yield();
}
Thread.sleep(100);
final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
do {
HISTOGRAM.reset();
System.out.format("Pinging %,d messages%n", NUMBER_OF_MESSAGES);
roundTripMessages(dataHandler, pingPublication, pongSubscription, NUMBER_OF_MESSAGES);
System.out.println("Histogram of RTT latencies in microseconds.");
HISTOGRAM.outputPercentileDistribution(System.out, 1000.0);
} while (barrier.await());
}
}
use of io.aeron.Publication in project Aeron by real-logic.
the class ManageRecordingHistoryTest method shouldDetachThenAttachFullSegments.
@Test
@InterruptAfter(10)
void shouldDetachThenAttachFullSegments() {
final String messagePrefix = "Message-Prefix-";
final long targetPosition = (SEGMENT_LENGTH * 3L) + 1;
try (Publication publication = aeronArchive.addRecordedPublication(uriBuilder.build(), STREAM_ID)) {
final CountersReader counters = aeron.countersReader();
final int counterId = awaitRecordingCounterId(counters, publication.sessionId());
final long recordingId = RecordingPos.getRecordingId(counters, counterId);
offerToPosition(publication, messagePrefix, targetPosition);
awaitPosition(counters, counterId, publication.position());
aeronArchive.stopRecording(publication);
final long startPosition = 0L;
final long segmentFileBasePosition = AeronArchive.segmentFileBasePosition(startPosition, SEGMENT_LENGTH * 2L, TERM_LENGTH, SEGMENT_LENGTH);
aeronArchive.detachSegments(recordingId, segmentFileBasePosition);
assertEquals(segmentFileBasePosition, aeronArchive.getStartPosition(recordingId));
final long attachSegments = aeronArchive.attachSegments(recordingId);
assertEquals(2L, attachSegments);
assertEquals(startPosition, aeronArchive.getStartPosition(recordingId));
}
}
Aggregations