use of io.aeron.Subscription in project aeron by real-logic.
the class ReplayedBasicSubscriber method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
*/
public static void main(final String[] args) {
System.out.println("Subscribing to " + CHANNEL + " on stream id " + STREAM_ID);
final FragmentHandler fragmentHandler = SamplesUtil.printAsciiMessage(STREAM_ID);
final AtomicBoolean running = new AtomicBoolean(true);
SigInt.register(() -> running.set(false));
// Create a unique response stream id so not to clash with other archive clients.
final AeronArchive.Context archiveCtx = new AeronArchive.Context().controlResponseStreamId(AeronArchive.Configuration.controlResponseStreamId() + 2);
try (AeronArchive archive = AeronArchive.connect(archiveCtx)) {
final long recordingId = findLatestRecording(archive);
final long position = 0L;
final long length = Long.MAX_VALUE;
final long sessionId = archive.startReplay(recordingId, position, length, CHANNEL, REPLAY_STREAM_ID);
final String channel = ChannelUri.addSessionId(CHANNEL, (int) sessionId);
try (Subscription subscription = archive.context().aeron().addSubscription(channel, REPLAY_STREAM_ID)) {
SamplesUtil.subscriberLoop(fragmentHandler, FRAGMENT_COUNT_LIMIT, running).accept(subscription);
System.out.println("Shutting down...");
}
}
}
use of io.aeron.Subscription in project aeron by real-logic.
the class SimpleSubscriber method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
*/
public static void main(final String[] args) {
// Maximum number of message fragments to receive during a single 'poll' operation
final int fragmentLimitCount = 10;
// The channel (an endpoint identifier) to receive messages from
final String channel = "aeron:udp?endpoint=localhost:40123";
// A unique identifier for a stream within a channel. Stream ID 0 is reserved
// for internal use and should not be used by applications.
final int streamId = 10;
System.out.println("Subscribing to " + channel + " on stream id " + streamId);
final AtomicBoolean running = new AtomicBoolean(true);
// Register a SIGINT handler for graceful shutdown.
SigInt.register(() -> running.set(false));
// dataHandler method is called for every new datagram received
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
final byte[] data = new byte[length];
buffer.getBytes(offset, data);
System.out.printf("Received message (%s) to stream %d from session %x term id %x term offset %d (%d@%d)%n", new String(data), streamId, header.sessionId(), header.termId(), header.termOffset(), length, offset);
// Received the intended message, time to exit the program
running.set(false);
};
// Create a context, needed for client connection to media driver
// A separate media driver process need to run prior to running this application
final Aeron.Context ctx = new Aeron.Context();
// clean up resources when this try block is finished.
try (Aeron aeron = Aeron.connect(ctx);
Subscription subscription = aeron.addSubscription(channel, streamId)) {
final IdleStrategy idleStrategy = new BackoffIdleStrategy(100, 10, TimeUnit.MICROSECONDS.toNanos(1), TimeUnit.MICROSECONDS.toNanos(100));
// Try to read the data from subscriber
while (running.get()) {
// poll delivers messages to the dataHandler as they arrive
// and returns number of fragments read, or 0
// if no data is available.
final int fragmentsRead = subscription.poll(fragmentHandler, fragmentLimitCount);
// Give the IdleStrategy a chance to spin/yield/sleep to reduce CPU
// use if no messages were received.
idleStrategy.idle(fragmentsRead);
}
System.out.println("Shutting down...");
}
}
use of io.aeron.Subscription in project aeron by real-logic.
the class FileReceiver method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
*/
public static void main(final String[] args) {
final File storageDir;
if (args.length > 1) {
storageDir = new File(args[0]);
if (!storageDir.isDirectory()) {
System.out.println(args[0] + " is not a directory");
System.exit(1);
}
} else {
storageDir = new File(SystemUtil.tmpDirName());
}
System.out.println("Files stored to " + storageDir.getAbsolutePath());
final IdleStrategy idleStrategy = new SleepingMillisIdleStrategy(1);
final AtomicBoolean running = new AtomicBoolean(true);
SigInt.register(() -> running.set(false));
try (MediaDriver mediaDriver = MediaDriver.launch();
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
System.out.println("Receiving from " + CHANNEL + " on stream id " + STREAM_ID);
final FileReceiver fileReceiver = new FileReceiver(storageDir, subscription);
while (running.get()) {
idleStrategy.idle(fileReceiver.doWork());
}
}
}
use of io.aeron.Subscription in project aeron by real-logic.
the class BasicSubscriber method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
*/
public static void main(final String[] args) {
System.out.println("Subscribing to " + CHANNEL + " on stream id " + STREAM_ID);
final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
final Aeron.Context ctx = new Aeron.Context().availableImageHandler(SamplesUtil::printAvailableImage).unavailableImageHandler(SamplesUtil::printUnavailableImage);
if (EMBEDDED_MEDIA_DRIVER) {
ctx.aeronDirectoryName(driver.aeronDirectoryName());
}
final FragmentHandler fragmentHandler = SamplesUtil.printAsciiMessage(STREAM_ID);
final AtomicBoolean running = new AtomicBoolean(true);
// Register a SIGINT handler for graceful shutdown.
SigInt.register(() -> running.set(false));
// clean up resources when this try block is finished
try (Aeron aeron = Aeron.connect(ctx);
Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
SamplesUtil.subscriberLoop(fragmentHandler, FRAGMENT_COUNT_LIMIT, running).accept(subscription);
System.out.println("Shutting down...");
}
CloseHelper.close(driver);
}
use of io.aeron.Subscription in project aeron by real-logic.
the class MultipleSubscribersWithFragmentAssembly method eventUnavailableImage.
/**
* This handler is called when image is unavailable.
*
* @param image that has gone inactive.
*/
public static void eventUnavailableImage(final Image image) {
final Subscription subscription = image.subscription();
System.out.format("inactive image on %s streamId %d sessionId %x%n", subscription.channel(), subscription.streamId(), image.sessionId());
}
Aggregations