use of org.agrona.concurrent.BackoffIdleStrategy in project Aeron by real-logic.
the class Configuration method agentIdleStrategy.
/**
* Get the {@link IdleStrategy} that should be applied to {@link org.agrona.concurrent.Agent}s.
*
* @param strategyName to be created.
* @param controllableStatus status indicator for what the strategy should do.
* @return the newly created IdleStrategy.
*/
public static IdleStrategy agentIdleStrategy(final String strategyName, final StatusIndicator controllableStatus) {
IdleStrategy idleStrategy = null;
switch(strategyName) {
case DEFAULT_IDLE_STRATEGY:
idleStrategy = new BackoffIdleStrategy(AGENT_IDLE_MAX_SPINS, AGENT_IDLE_MAX_YIELDS, AGENT_IDLE_MIN_PARK_NS, AGENT_IDLE_MAX_PARK_NS);
break;
case CONTROLLABLE_IDLE_STRATEGY:
idleStrategy = new ControllableIdleStrategy(controllableStatus);
controllableStatus.setOrdered(ControllableIdleStrategy.PARK);
break;
default:
try {
idleStrategy = (IdleStrategy) Class.forName(strategyName).newInstance();
} catch (final Exception ex) {
LangUtil.rethrowUnchecked(ex);
}
break;
}
return idleStrategy;
}
use of org.agrona.concurrent.BackoffIdleStrategy in project aeron by real-logic.
the class MultipleSubscribersWithFragmentAssembly method main.
public static void main(final String[] args) {
System.out.format("Subscribing to %s on stream ID %d and stream ID %d%n", CHANNEL, STREAM_ID_1, STREAM_ID_2);
final Aeron.Context ctx = new Aeron.Context().availableImageHandler(MultipleSubscribersWithFragmentAssembly::eventAvailableImage).unavailableImageHandler(MultipleSubscribersWithFragmentAssembly::eventUnavailableImage);
final FragmentAssembler dataHandler1 = new FragmentAssembler(reassembledStringMessage1(STREAM_ID_1));
final FragmentAssembler dataHandler2 = new FragmentAssembler(reassembledStringMessage2(STREAM_ID_2));
final AtomicBoolean running = new AtomicBoolean(true);
SigInt.register(() -> running.set(false));
try (Aeron aeron = Aeron.connect(ctx);
Subscription subscription1 = aeron.addSubscription(CHANNEL, STREAM_ID_1);
Subscription subscription2 = aeron.addSubscription(CHANNEL, STREAM_ID_2)) {
final IdleStrategy idleStrategy = new BackoffIdleStrategy(100, 10, TimeUnit.MICROSECONDS.toNanos(1), TimeUnit.MICROSECONDS.toNanos(100));
int idleCount = 0;
while (running.get()) {
final int fragmentsRead1 = subscription1.poll(dataHandler1, FRAGMENT_COUNT_LIMIT);
final int fragmentsRead2 = subscription2.poll(dataHandler2, FRAGMENT_COUNT_LIMIT);
if ((fragmentsRead1 + fragmentsRead2) == 0) {
idleStrategy.idle(idleCount++);
} else {
idleCount = 0;
}
}
System.out.println("Shutting down...");
}
}
use of org.agrona.concurrent.BackoffIdleStrategy in project aeron by real-logic.
the class EmbeddedRecordingThroughput method runRecordingEventPoller.
private void runRecordingEventPoller() {
try (Subscription subscription = aeron.addSubscription(AeronArchive.Configuration.recordingEventsChannel(), AeronArchive.Configuration.recordingEventsStreamId())) {
final IdleStrategy idleStrategy = new BackoffIdleStrategy(10, 100, 1, 1);
final RecordingEventsAdapter recordingEventsAdapter = new RecordingEventsAdapter(this, subscription, FRAGMENT_COUNT_LIMIT);
while (isRunning) {
idleStrategy.idle(recordingEventsAdapter.poll());
}
}
}
use of org.agrona.concurrent.BackoffIdleStrategy in project aeron by real-logic.
the class Configuration method agentIdleStrategy.
/**
* Get the {@link IdleStrategy} that should be applied to {@link org.agrona.concurrent.Agent}s.
*
* @param strategyName of the class to be created.
* @param controllableStatus status indicator for what the strategy should do.
* @return the newly created IdleStrategy.
*/
public static IdleStrategy agentIdleStrategy(final String strategyName, final StatusIndicator controllableStatus) {
IdleStrategy idleStrategy = null;
switch(strategyName) {
case DEFAULT_IDLE_STRATEGY:
idleStrategy = new BackoffIdleStrategy(IDLE_MAX_SPINS, IDLE_MAX_YIELDS, IDLE_MIN_PARK_NS, IDLE_MAX_PARK_NS);
break;
case CONTROLLABLE_IDLE_STRATEGY:
idleStrategy = new ControllableIdleStrategy(controllableStatus);
controllableStatus.setOrdered(ControllableIdleStrategy.PARK);
break;
default:
try {
idleStrategy = (IdleStrategy) Class.forName(strategyName).newInstance();
} catch (final Exception ex) {
LangUtil.rethrowUnchecked(ex);
}
break;
}
return idleStrategy;
}
use of org.agrona.concurrent.BackoffIdleStrategy in project Aeron by real-logic.
the class MultipleSubscribersWithFragmentAssembly method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
*/
public static void main(final String[] args) {
System.out.format("Subscribing to %s on stream ID %d and stream ID %d%n", CHANNEL, STREAM_ID_1, STREAM_ID_2);
final Aeron.Context ctx = new Aeron.Context().availableImageHandler(MultipleSubscribersWithFragmentAssembly::eventAvailableImage).unavailableImageHandler(MultipleSubscribersWithFragmentAssembly::eventUnavailableImage);
final FragmentAssembler assembler1 = new FragmentAssembler(reassembledMessage1(STREAM_ID_1));
final FragmentAssembler assembler2 = new FragmentAssembler(reassembledMessage2(STREAM_ID_2));
final AtomicBoolean running = new AtomicBoolean(true);
SigInt.register(() -> running.set(false));
try (Aeron aeron = Aeron.connect(ctx);
Subscription subscription1 = aeron.addSubscription(CHANNEL, STREAM_ID_1);
Subscription subscription2 = aeron.addSubscription(CHANNEL, STREAM_ID_2)) {
final IdleStrategy idleStrategy = new BackoffIdleStrategy(100, 10, TimeUnit.MICROSECONDS.toNanos(1), TimeUnit.MICROSECONDS.toNanos(100));
int idleCount = 0;
while (running.get()) {
final int fragmentsRead1 = subscription1.poll(assembler1, FRAGMENT_COUNT_LIMIT);
final int fragmentsRead2 = subscription2.poll(assembler2, FRAGMENT_COUNT_LIMIT);
if ((fragmentsRead1 + fragmentsRead2) == 0) {
idleStrategy.idle(idleCount++);
} else {
idleCount = 0;
}
}
System.out.println("Shutting down...");
}
}
Aggregations