本文整理汇总了Java中org.agrona.concurrent.UnsafeBuffer类的典型用法代码示例。如果您正苦于以下问题:Java UnsafeBuffer类的具体用法?Java UnsafeBuffer怎么用?Java UnsafeBuffer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
UnsafeBuffer类属于org.agrona.concurrent包,在下文中一共展示了UnsafeBuffer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import org.agrona.concurrent.UnsafeBuffer; //导入依赖的package包/类
@Override
public void run()
{
final IdleStrategy idleStrategy = new BusySpinIdleStrategy();
final UnsafeBuffer buffer = new UnsafeBuffer(DirectBufferAllocator.allocateCacheAligned(MESSAGE_LENGTH));
for (int messageNumber = 1; messageNumber <= numMessages; messageNumber++)
{
final long timestamp = System.nanoTime();
buffer.putInt(0, messageNumber);
buffer.putLong(4, timestamp);
while (!inputRingBuffer.write(MessageTypes.APPLICATION_MSG_ID, buffer, 0, MESSAGE_LENGTH))
{
idleStrategy.idle(0);
}
}
}
示例2: onMessage
import org.agrona.concurrent.UnsafeBuffer; //导入依赖的package包/类
@Override
public void onMessage(int msgTypeId, MutableDirectBuffer buffer, int index, int length)
{
if (msgTypeId == MessageTypes.APPLICATION_MSG_ID)
{
incomingDataMessage.wrap(buffer, index, length);
final UnsafeBuffer dataBuffer = incomingDataMessage.dataBuffer();
final int dataOffset = incomingDataMessage.dataBufferOffset();
/* ECHO GATEWAY message processing: store last echo timestamp */
final long echoTimestamp = dataBuffer.getLong(dataOffset);
//final long echoTimestamp = buffer.getLong(index);
//UNSAFE.putOrderedLong(this, TIMESTAMP_OFFSET, echoTimestamp);
//final long timestamp = timestampQueue.remove();
//System.out.println("RECEIVED echoTimestamp=" + echoTimestamp);
}
}
示例3: sendEcho
import org.agrona.concurrent.UnsafeBuffer; //导入依赖的package包/类
void sendEcho(final long echoTimestamp)
{
final RingBuffer outputBuffer = outputBufferPool.outputRingBuffers().iterator().next(); // FIXME: refactoring to avoid this API
final UnsafeBuffer dataBuffer = outgoingDataMessage.dataBuffer();
final int dataOffset = outgoingDataMessage.dataBufferOffset();
//echoBuffer.putLong(0, echoTimestamp);
dataBuffer.putLong(dataOffset, echoTimestamp);
/*while (!outputBuffer.write(MessageTypes.APPLICATION_MSG_ID, echoBuffer, 0, MESSAGE_LENGTH))
{
idleStrategy.idle(0);
}*/
while (!outputBuffer.write(MessageTypes.APPLICATION_MSG_ID, dataBuffer, 0, dataBuffer.capacity()))
{
idleStrategy.idle(0);
}
//System.out.println("SENT echoTimestamp=" + echoTimestamp);
//UNSAFE.putOrderedLong(this, TIMESTAMP_OFFSET, 0);
//timestampQueue.add(echoTimestamp);
}
示例4: addEventChannel
import org.agrona.concurrent.UnsafeBuffer; //导入依赖的package包/类
@Override
public Gateway<T> addEventChannel(final AeronStream eventStream)
{
Objects.requireNonNull(eventStream, "eventStream");
final IdleStrategy readIdleStrategy = helios.context().readIdleStrategy();
final ByteBuffer eventBuffer = DirectBufferAllocator.allocateCacheAligned((16 * 1024) + TRAILER_LENGTH); // TODO: configure
final RingBuffer eventRingBuffer = new OneToOneRingBuffer(new UnsafeBuffer(eventBuffer));
ringBufferPool.addEventRingBuffer(eventStream, eventRingBuffer);
final int heartbeatLiveness = helios.context().heartbeatLiveness();
final InputMessageProcessor eventProcessor =
new InputMessageProcessor(eventRingBuffer, readIdleStrategy, FRAME_COUNT_LIMIT,
heartbeatLiveness, eventStream, null, "eventProcessor"); // FIXME: eventProcessor name
eventProcessorList.add(eventProcessor);
return this;
}
示例5: addEventChannel
import org.agrona.concurrent.UnsafeBuffer; //导入依赖的package包/类
@Override
public Service<T> addEventChannel(final AeronStream eventStream)
{
Objects.requireNonNull(eventStream, "eventStream");
eventStream.componentType = ComponentType.Service;
eventStream.componentId = serviceId;
final IdleStrategy writeIdleStrategy = helios.context().writeIdleStrategy();
final ByteBuffer eventBuffer = DirectBufferAllocator.allocateCacheAligned((16 * 1024) + TRAILER_LENGTH); // TODO: configure
final RingBuffer eventRingBuffer = new OneToOneRingBuffer(new UnsafeBuffer(eventBuffer));
ringBufferPool.addEventRingBuffer(eventStream, eventRingBuffer);
final int heartbeatInterval = helios.context().heartbeatInterval();
final OutputMessageProcessor eventProcessor =
new OutputMessageProcessor(eventRingBuffer, eventStream, writeIdleStrategy, heartbeatInterval, "eventProcessor"); // FIXME: eventProcessor name
eventProcessorList.add(eventProcessor);
return this;
}
示例6: StartupMessage
import org.agrona.concurrent.UnsafeBuffer; //导入依赖的package包/类
public StartupMessage(final ComponentType componentType, final short componentId)
{
startupBuffer = new UnsafeBuffer(DirectBufferAllocator.allocateCacheAligned(MESSAGE_LENGTH));
final MessageHeaderEncoder messageHeaderEncoder = new MessageHeaderEncoder();
final StartupEncoder startupEncoder = new StartupEncoder();
// Encode the StartupMessage message once and forever
int bufferOffset = 0;
messageHeaderEncoder.wrap(startupBuffer, bufferOffset)
.blockLength(startupEncoder.sbeBlockLength())
.templateId(startupEncoder.sbeTemplateId())
.schemaId(startupEncoder.sbeSchemaId())
.version(startupEncoder.sbeSchemaVersion());
bufferOffset += messageHeaderEncoder.encodedLength();
startupEncoder.wrap(startupBuffer, bufferOffset)
.mmbHeader()
.nodeId((short)0)
.timestamp(System.nanoTime())
.component()
.componentId(componentId)
.componentType(componentType);
}
示例7: runLatencyTest
import org.agrona.concurrent.UnsafeBuffer; //导入依赖的package包/类
public void runLatencyTest() throws Exception {
//given
final long n = warmupCount + measuredCount;
System.out.println("\twarmup + measured : " + warmupCount + " + " + measuredCount + " = " + n);
System.out.println("\tchannel : " + channel);
System.out.println("\tstreamId : " + streamId);
System.out.println("\tmessagesPerSecond : " + messagesPerSecond);
System.out.println("\tmarketDataDepth : " + marketDataDepth);
System.out.println("\tmessageSize : " + encode(new UnsafeBuffer(new byte[1024]), givenMarketDataSnapshot(new ImmutableMarketDataSnapshot.Builder(), marketDataDepth, marketDataDepth)) + " bytes");
System.out.println();
//when
aeronSubscriber = new AeronSubscriber(aeronMediaDriver.getAeronDirectoryName(), channel, streamId, warmupCount, measuredCount);
aeronSubscriber.start();
aeronPublisher = new AeronPublisher(aeronMediaDriver.getAeronDirectoryName(), channel, streamId, n, messagesPerSecond, marketDataDepth);
aeronPublisher.start();
}
示例8: putCommitGetByteArray
import org.agrona.concurrent.UnsafeBuffer; //导入依赖的package包/类
@Test
public void putCommitGetByteArray() throws IOException {
final File path = tmp.newFile();
try (Env<byte[]> envBa = create(new ByteArrayProxy())
.setMapSize(MEBIBYTES.toBytes(64))
.setMaxReaders(1)
.setMaxDbs(2)
.open(path, MDB_NOSUBDIR)) {
final Dbi<byte[]> db = envBa.openDbi(DB_1, MDB_CREATE);
try (Txn<byte[]> txn = envBa.txnWrite()) {
db.put(txn, ba(5), ba(5));
txn.commit();
}
try (Txn<byte[]> txn = envBa.txnWrite()) {
final byte[] found = db.get(txn, ba(5));
assertNotNull(found);
assertThat(new UnsafeBuffer(txn.val()).getInt(0), is(5));
}
}
}
示例9: map
import org.agrona.concurrent.UnsafeBuffer; //导入依赖的package包/类
public static MappedFile map(final File bufferFile, final int size)
{
final FileChannel fileChannel;
try
{
if (bufferFile.exists())
{
// NB: closing RAF or FileChannel closes them both
fileChannel = new RandomAccessFile(bufferFile, "rw").getChannel();
}
else
{
fileChannel = IoUtil.createEmptyFile(bufferFile, (long)size);
}
final MappedByteBuffer mappedBuffer = fileChannel.map(READ_WRITE, 0, fileChannel.size());
return new MappedFile(bufferFile, fileChannel, new UnsafeBuffer(mappedBuffer));
}
catch (final IOException ex)
{
LangUtil.rethrowUnchecked(ex);
return null;
}
}
示例10: SessionIndex
import org.agrona.concurrent.UnsafeBuffer; //导入依赖的package包/类
private SessionIndex(final long fixSessionId)
{
final File logFile = logFile(logFileDir, fixSessionId, requiredStreamId);
final boolean exists = logFile.exists();
this.wrappedBuffer = bufferFactory.map(logFile, indexFileSize);
this.buffer = new UnsafeBuffer(wrappedBuffer);
capacity = recordCapacity(buffer.capacity());
if (!exists)
{
indexHeaderEncoder
.wrap(buffer, 0)
.blockLength(replayIndexRecord.sbeBlockLength())
.templateId(replayIndexRecord.sbeTemplateId())
.schemaId(replayIndexRecord.sbeSchemaId())
.version(replayIndexRecord.sbeSchemaVersion());
}
else
{
// Reset the positions in order to avoid wraps at the start.
final int resetPosition = offset(beginChange(buffer), capacity);
beginChangeOrdered(buffer, resetPosition);
endChangeOrdered(buffer, resetPosition);
}
}
示例11: forEachFragment
import org.agrona.concurrent.UnsafeBuffer; //导入依赖的package包/类
public void forEachFragment(
final StreamIdentifier streamId,
final FragmentHandler handler,
final ErrorHandler errorHandler)
{
final UnsafeBuffer termBuffer = new UnsafeBuffer(0, 0);
for (final File logFile : directoryDescriptor.listLogFiles(streamId))
{
final ByteBuffer byteBuffer = LoggerUtil.mapExistingFile(logFile);
if (byteBuffer.capacity() > 0)
{
termBuffer.wrap(byteBuffer);
final int initialTermId = LogBufferDescriptor.initialTermId(termBuffer);
final Header header = new Header(initialTermId, termBuffer.capacity());
TermReader.read(
termBuffer,
0,
handler,
Integer.MAX_VALUE,
header,
errorHandler);
}
}
}
示例12: make
import org.agrona.concurrent.UnsafeBuffer; //导入依赖的package包/类
static DirectBuffer make(final String libraryChannel)
{
final byte[] libraryChannelBytes = libraryChannel.getBytes(UTF_8);
final MessageHeaderEncoder messageHeader = new MessageHeaderEncoder();
final EngineDescriptorEncoder descriptor = new EngineDescriptorEncoder();
final int length = ENCODED_LENGTH + EngineDescriptorEncoder.BLOCK_LENGTH + libraryChannelHeaderLength() +
libraryChannelBytes.length;
final UnsafeBuffer buffer = new UnsafeBuffer(new byte[length]);
descriptor
.wrapAndApplyHeader(buffer, 0, messageHeader)
.putLibraryChannel(libraryChannelBytes, 0, libraryChannelBytes.length);
return buffer;
}
示例13: shouldValidateCompId
import org.agrona.concurrent.UnsafeBuffer; //导入依赖的package包/类
@Test
public void shouldValidateCompId()
{
final UnsafeBuffer buffer = bufferOf(
"8=FIX.4.2\0019=146\00135=D\00134=4\00149=WRONG\001" +
"52=20090323-15:40:29\00156=WRONG\001115=XYZ\00111=NF 0542/03232009\00154=1\00138=100\001" +
"55=CVS\00140=1\00159=0\00147=A\00160=20090323-15:40:29\00121=1\001207=N\00110=195\001");
when(mockSession.state()).thenReturn(SessionState.AWAITING_LOGOUT);
parser.onMessage(buffer, 0, buffer.capacity(), 'D', 1);
verify(mockSession).onInvalidMessage(
4,
TARGET_COMP_ID,
"D".toCharArray(),
"D".length(),
RejectReason.COMPID_PROBLEM.representation());
verify(mockSession).startLogout();
verify(mockSession, never()).onInvalidMessageType(anyInt(), any(), anyInt());
}
示例14: setupCapturingClaim
import org.agrona.concurrent.UnsafeBuffer; //导入依赖的package包/类
protected int setupCapturingClaim()
{
final int offset = offset();
when(publication.tryClaim(anyInt(), eq(claim))).then(inv ->
{
claimedLength = (int)inv.getArguments()[0];
resultBuffer = new UnsafeBuffer(new byte[offset + claimedLength]);
resultAsciiBuffer.wrap(resultBuffer);
when(claim.buffer()).thenReturn(resultBuffer);
when(claim.offset()).thenReturn(offset);
when(claim.length()).thenReturn(claimedLength);
return (long)offset;
});
return offset;
}
示例15: shouldInsertPaddingRecordPlusMessageOnBufferWrap
import org.agrona.concurrent.UnsafeBuffer; //导入依赖的package包/类
@Test
public void shouldInsertPaddingRecordPlusMessageOnBufferWrap()
{
final int length = 200;
final int recordLength = length + HEADER_LENGTH;
final int alignedRecordLength = align(recordLength, ALIGNMENT);
final long tail = CAPACITY - HEADER_LENGTH;
final long head = tail - (ALIGNMENT * 4);
when(buffer.getLongVolatile(HEAD_COUNTER_INDEX)).thenReturn(head);
when(buffer.getLong(TAIL_COUNTER_INDEX)).thenReturn(tail);
final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);
final int srcIndex = 0;
assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, srcIndex, length));
final InOrder inOrder = inOrder(buffer);
inOrder.verify(buffer).putLong(0, 0L);
inOrder.verify(buffer).putLongOrdered((int)tail, makeHeader(HEADER_LENGTH, PADDING_MSG_TYPE_ID));
inOrder.verify(buffer).putBytes(encodedMsgOffset(0), srcBuffer, srcIndex, length);
inOrder.verify(buffer).putLong(alignedRecordLength, 0L);
inOrder.verify(buffer).putLongOrdered(0, makeHeader(recordLength, MSG_TYPE_ID));
}