use of org.apache.activemq.artemis.core.buffers.impl.ChannelBufferWrapper in project activemq-artemis by apache.
the class LargeServerMessageImpl method getReadOnlyBodyBuffer.
// Even though not recommended, in certain instances
// we may need to convert a large message back to a whole buffer
// in a way you can convert
@Override
public ActiveMQBuffer getReadOnlyBodyBuffer() {
try {
validateFile();
file.open();
int fileSize = (int) file.size();
ByteBuffer buffer = this.storageManager.largeMessagesFactory.newBuffer(fileSize);
file.read(buffer);
return new ChannelBufferWrapper(Unpooled.wrappedBuffer(buffer));
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
file.close();
} catch (Exception ignored) {
}
}
}
use of org.apache.activemq.artemis.core.buffers.impl.ChannelBufferWrapper in project activemq-artemis by apache.
the class Page method readFromMapped.
private int readFromMapped(StorageManager storage, List<PagedMessage> messages) throws IOException {
file.position(0);
// use a readonly mapped view of the file
final int mappedSize = size.get();
final MappedByteBuffer mappedByteBuffer = mapFileForRead(this.file.getJavaFile(), mappedSize);
ChannelBufferWrapper activeMQBuffer = wrapBuffer(mappedSize, mappedByteBuffer);
try {
return read(storage, activeMQBuffer, messages);
} finally {
// unmap the file after read it to avoid GC to take care of it
PlatformDependent.freeDirectBuffer(mappedByteBuffer);
}
}
use of org.apache.activemq.artemis.core.buffers.impl.ChannelBufferWrapper in project activemq-artemis by apache.
the class Page method readFromSequentialFile.
private void readFromSequentialFile(StorageManager storage, List<PagedMessage> messages) throws Exception {
final int fileSize = (int) file.size();
// doesn't need to be a direct buffer: that case is covered using the MMAP read
final ByteBuffer buffer = this.fileFactory.newBuffer(fileSize);
try {
file.position(0);
file.read(buffer);
buffer.rewind();
assert (buffer.limit() == fileSize) : "buffer doesn't contains the whole file";
ChannelBufferWrapper activeMQBuffer = wrapBuffer(fileSize, buffer);
read(storage, activeMQBuffer, messages);
} finally {
this.fileFactory.releaseBuffer(buffer);
}
}
use of org.apache.activemq.artemis.core.buffers.impl.ChannelBufferWrapper in project activemq-artemis by apache.
the class CoreMessage method getLargeMessageBuffer.
private ActiveMQBuffer getLargeMessageBuffer() throws ActiveMQException {
ActiveMQBuffer buffer;
LargeBodyEncoder encoder = getBodyEncoder();
encoder.open();
int bodySize = (int) encoder.getLargeBodySize();
buffer = new ChannelBufferWrapper(UnpooledByteBufAllocator.DEFAULT.heapBuffer(bodySize));
encoder.encode(buffer, bodySize);
encoder.close();
return buffer;
}
use of org.apache.activemq.artemis.core.buffers.impl.ChannelBufferWrapper in project activemq-artemis by apache.
the class Page method write.
public synchronized void write(final PagedMessage message) throws Exception {
if (!file.isOpen()) {
return;
}
final int messageEncodedSize = message.getEncodeSize();
final int bufferSize = messageEncodedSize + Page.SIZE_RECORD;
final ByteBuffer buffer = fileFactory.newBuffer(bufferSize);
ChannelBufferWrapper activeMQBuffer = wrapBuffer(bufferSize, buffer);
activeMQBuffer.clear();
activeMQBuffer.writeByte(Page.START_BYTE);
activeMQBuffer.writeInt(messageEncodedSize);
message.encode(activeMQBuffer);
activeMQBuffer.writeByte(Page.END_BYTE);
assert (activeMQBuffer.readableBytes() == bufferSize) : "messageEncodedSize is different from expected";
// buffer limit and position are the same
assert (buffer.remaining() == bufferSize) : "buffer position or limit are changed";
file.writeDirect(buffer, false);
if (pageCache != null) {
pageCache.addLiveMessage(message);
}
// lighter than addAndGet when single writer
numberOfMessages.lazySet(numberOfMessages.get() + 1);
size.lazySet(size.get() + bufferSize);
storageManager.pageWrite(message, pageId);
}
Aggregations