Search in sources :

Example 1 with ChannelBufferWrapper

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) {
        }
    }
}
Also used : ChannelBufferWrapper(org.apache.activemq.artemis.core.buffers.impl.ChannelBufferWrapper) ByteBuffer(java.nio.ByteBuffer) ActiveMQIOErrorException(org.apache.activemq.artemis.api.core.ActiveMQIOErrorException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQInternalErrorException(org.apache.activemq.artemis.api.core.ActiveMQInternalErrorException)

Example 2 with ChannelBufferWrapper

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);
    }
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) ChannelBufferWrapper(org.apache.activemq.artemis.core.buffers.impl.ChannelBufferWrapper)

Example 3 with ChannelBufferWrapper

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);
    }
}
Also used : ChannelBufferWrapper(org.apache.activemq.artemis.core.buffers.impl.ChannelBufferWrapper) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 4 with ChannelBufferWrapper

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;
}
Also used : ChannelBufferWrapper(org.apache.activemq.artemis.core.buffers.impl.ChannelBufferWrapper) LargeBodyEncoder(org.apache.activemq.artemis.core.message.LargeBodyEncoder) ResetLimitWrappedActiveMQBuffer(org.apache.activemq.artemis.core.buffers.impl.ResetLimitWrappedActiveMQBuffer) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer)

Example 5 with ChannelBufferWrapper

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);
}
Also used : ChannelBufferWrapper(org.apache.activemq.artemis.core.buffers.impl.ChannelBufferWrapper) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Aggregations

ChannelBufferWrapper (org.apache.activemq.artemis.core.buffers.impl.ChannelBufferWrapper)5 ByteBuffer (java.nio.ByteBuffer)3 MappedByteBuffer (java.nio.MappedByteBuffer)3 ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)1 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)1 ActiveMQIOErrorException (org.apache.activemq.artemis.api.core.ActiveMQIOErrorException)1 ActiveMQInternalErrorException (org.apache.activemq.artemis.api.core.ActiveMQInternalErrorException)1 ResetLimitWrappedActiveMQBuffer (org.apache.activemq.artemis.core.buffers.impl.ResetLimitWrappedActiveMQBuffer)1 LargeBodyEncoder (org.apache.activemq.artemis.core.message.LargeBodyEncoder)1