Class MemoryMappedFileManager
- java.lang.Object
-
- org.apache.logging.log4j.core.appender.AbstractManager
-
- org.apache.logging.log4j.core.appender.OutputStreamManager
-
- org.apache.logging.log4j.core.appender.MemoryMappedFileManager
-
- All Implemented Interfaces:
java.lang.AutoCloseable
,ByteBufferDestination
public class MemoryMappedFileManager extends OutputStreamManager
Extends OutputStreamManager but instead of using a buffered output stream, this class maps a region of a file into memory and writes to this memory region.- Since:
- 2.1
- See Also:
- http://www.codeproject.com/Tips/683614/Things-to-Know-about-Memory-Mapped-File-in-Java, http://bugs.java.com/view_bug.do?bug_id=6893654, http://bugs.java.com/view_bug.do?bug_id=4724038, http://stackoverflow.com/questions/9261316/memory-mapped-mappedbytebuffer-or-direct-bytebuffer-for-db-implementation
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
MemoryMappedFileManager.FactoryData
Factory Data.private static class
MemoryMappedFileManager.MemoryMappedFileManagerFactory
Factory to create a MemoryMappedFileManager.-
Nested classes/interfaces inherited from class org.apache.logging.log4j.core.appender.AbstractManager
AbstractManager.AbstractFactoryData
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
advertiseURI
(package private) static int
DEFAULT_REGION_LENGTH
Default length of region to map.private static MemoryMappedFileManager.MemoryMappedFileManagerFactory
FACTORY
private boolean
immediateFlush
private java.nio.MappedByteBuffer
mappedBuffer
private long
mappingOffset
private static int
MAX_REMAP_COUNT
private static double
NANOS_PER_MILLISEC
private java.io.RandomAccessFile
randomAccessFile
private int
regionLength
-
Fields inherited from class org.apache.logging.log4j.core.appender.OutputStreamManager
byteBuffer, layout
-
Fields inherited from class org.apache.logging.log4j.core.appender.AbstractManager
count, LOGGER
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
MemoryMappedFileManager(java.io.RandomAccessFile file, java.lang.String fileName, java.io.OutputStream os, boolean immediateFlush, long position, int regionLength, java.lang.String advertiseURI, Layout<? extends java.io.Serializable> layout, boolean writeHeader)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description boolean
closeOutputStream()
java.nio.ByteBuffer
drain(java.nio.ByteBuffer buf)
Drains the ByteBufferDestination's buffer into the destination.void
flush()
Flushes any buffers.protected void
flushBuffer(java.nio.ByteBuffer buffer)
Drains the ByteBufferDestination's buffer into the destination.java.nio.ByteBuffer
getByteBuffer()
Returns thisByteBufferDestination
's buffer.java.util.Map<java.lang.String,java.lang.String>
getContentFormat()
Gets this FileManager's content format specified by:static MemoryMappedFileManager
getFileManager(java.lang.String fileName, boolean append, boolean immediateFlush, int regionLength, java.lang.String advertiseURI, Layout<? extends java.io.Serializable> layout)
Returns the MemoryMappedFileManager.java.lang.String
getFileName()
Returns the name of the File being managed.int
getRegionLength()
Returns the length of the memory mapped region.java.lang.Boolean
isEndOfBatch()
Deprecated.end-of-batch on the event is used instead.boolean
isImmediateFlush()
Returnstrue
if the content of the buffer should be forced to the storage device on every write,false
otherwise.static java.nio.MappedByteBuffer
mmap(java.nio.channels.FileChannel fileChannel, java.lang.String fileName, long start, int size)
private void
remap()
void
setEndOfBatch(boolean endOfBatch)
Deprecated.end-of-batch on the event is used instead.private static void
unsafeUnmap(java.nio.MappedByteBuffer mbb)
protected void
write(byte[] bytes, int offset, int length, boolean immediateFlush)
Some output streams synchronize writes while others do not.-
Methods inherited from class org.apache.logging.log4j.core.appender.OutputStreamManager
createOutputStream, flushDestination, getManager, getOutputStream, hasOutputStream, isOpen, releaseSub, setOutputStream, skipFooter, write, write, write, writeBytes, writeBytes, writeFooter, writeHeader, writeToDestination
-
Methods inherited from class org.apache.logging.log4j.core.appender.AbstractManager
close, getCount, getLoggerContext, getManager, getManagerCount, getName, getStrSubstitutor, hasManager, log, logDebug, logError, logger, logWarn, narrow, release, stop, updateData
-
-
-
-
Field Detail
-
DEFAULT_REGION_LENGTH
static final int DEFAULT_REGION_LENGTH
Default length of region to map.- See Also:
- Constant Field Values
-
MAX_REMAP_COUNT
private static final int MAX_REMAP_COUNT
- See Also:
- Constant Field Values
-
FACTORY
private static final MemoryMappedFileManager.MemoryMappedFileManagerFactory FACTORY
-
NANOS_PER_MILLISEC
private static final double NANOS_PER_MILLISEC
- See Also:
- Constant Field Values
-
immediateFlush
private final boolean immediateFlush
-
regionLength
private final int regionLength
-
advertiseURI
private final java.lang.String advertiseURI
-
randomAccessFile
private final java.io.RandomAccessFile randomAccessFile
-
mappedBuffer
private java.nio.MappedByteBuffer mappedBuffer
-
mappingOffset
private long mappingOffset
-
-
Constructor Detail
-
MemoryMappedFileManager
protected MemoryMappedFileManager(java.io.RandomAccessFile file, java.lang.String fileName, java.io.OutputStream os, boolean immediateFlush, long position, int regionLength, java.lang.String advertiseURI, Layout<? extends java.io.Serializable> layout, boolean writeHeader) throws java.io.IOException
- Throws:
java.io.IOException
-
-
Method Detail
-
getFileManager
public static MemoryMappedFileManager getFileManager(java.lang.String fileName, boolean append, boolean immediateFlush, int regionLength, java.lang.String advertiseURI, Layout<? extends java.io.Serializable> layout)
Returns the MemoryMappedFileManager.- Parameters:
fileName
- The name of the file to manage.append
- true if the file should be appended to, false if it should be overwritten.immediateFlush
- true if the contents should be flushed to disk on every writeregionLength
- The mapped region length.advertiseURI
- the URI to use when advertising the filelayout
- The layout.- Returns:
- A MemoryMappedFileManager for the File.
-
isEndOfBatch
@Deprecated public java.lang.Boolean isEndOfBatch()
Deprecated.end-of-batch on the event is used instead.No longer used, theLogEvent.isEndOfBatch()
attribute is used instead.- Returns:
Boolean.FALSE
.
-
setEndOfBatch
@Deprecated public void setEndOfBatch(boolean endOfBatch)
Deprecated.end-of-batch on the event is used instead.No longer used, theLogEvent.isEndOfBatch()
attribute is used instead. This method is a no-op.
-
write
protected void write(byte[] bytes, int offset, int length, boolean immediateFlush)
Description copied from class:OutputStreamManager
Some output streams synchronize writes while others do not. Synchronizing here insures that log events won't be intertwined.- Overrides:
write
in classOutputStreamManager
- Parameters:
bytes
- The serialized Log event.offset
- The offset into the byte array.length
- The number of bytes to write.immediateFlush
- flushes immediately after writing.
-
remap
private void remap()
-
flush
public void flush()
Description copied from class:OutputStreamManager
Flushes any buffers.- Overrides:
flush
in classOutputStreamManager
-
closeOutputStream
public boolean closeOutputStream()
- Overrides:
closeOutputStream
in classOutputStreamManager
-
mmap
public static java.nio.MappedByteBuffer mmap(java.nio.channels.FileChannel fileChannel, java.lang.String fileName, long start, int size) throws java.io.IOException
- Throws:
java.io.IOException
-
unsafeUnmap
private static void unsafeUnmap(java.nio.MappedByteBuffer mbb) throws java.security.PrivilegedActionException
- Throws:
java.security.PrivilegedActionException
-
getFileName
public java.lang.String getFileName()
Returns the name of the File being managed.- Returns:
- The name of the File being managed.
-
getRegionLength
public int getRegionLength()
Returns the length of the memory mapped region.- Returns:
- the length of the mapped region
-
isImmediateFlush
public boolean isImmediateFlush()
Returnstrue
if the content of the buffer should be forced to the storage device on every write,false
otherwise.- Returns:
- whether each write should be force-sync'ed
-
getContentFormat
public java.util.Map<java.lang.String,java.lang.String> getContentFormat()
Gets this FileManager's content format specified by:Key: "fileURI" Value: provided "advertiseURI" param.
- Overrides:
getContentFormat
in classAbstractManager
- Returns:
- Map of content format keys supporting FileManager
-
flushBuffer
protected void flushBuffer(java.nio.ByteBuffer buffer)
Description copied from class:OutputStreamManager
Drains the ByteBufferDestination's buffer into the destination. By default this callsOutputStreamManager.write(byte[], int, int, boolean)
with the buffer contents. The underlying stream is not flushed.- Overrides:
flushBuffer
in classOutputStreamManager
- See Also:
OutputStreamManager.flushDestination()
-
getByteBuffer
public java.nio.ByteBuffer getByteBuffer()
Description copied from class:OutputStreamManager
Returns thisByteBufferDestination
's buffer.- Specified by:
getByteBuffer
in interfaceByteBufferDestination
- Overrides:
getByteBuffer
in classOutputStreamManager
- Returns:
- the buffer
-
drain
public java.nio.ByteBuffer drain(java.nio.ByteBuffer buf)
Description copied from class:OutputStreamManager
Drains the ByteBufferDestination's buffer into the destination. By default this callsOutputStreamManager.flushBuffer(ByteBuffer)
with the specified buffer. Subclasses may override.Do not call this method lightly! For some subclasses this is a very expensive operation. For example,
MemoryMappedFileManager
will assume this method was called because the end of the mapped region was reached during a text encoding operation and will remap its buffer.To just flush the buffered contents to the underlying stream, call
OutputStreamManager.flushBuffer(ByteBuffer)
directly instead.- Specified by:
drain
in interfaceByteBufferDestination
- Overrides:
drain
in classOutputStreamManager
- Parameters:
buf
- the buffer whose contents to write the destination- Returns:
- the specified buffer
-
-