Package net.morimekta.providence.mio
Class QueuedMessageWriter
- java.lang.Object
-
- net.morimekta.providence.mio.QueuedMessageWriter
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,MessageWriter
public class QueuedMessageWriter extends java.lang.Object implements MessageWriter
A queued message writer that takes in messages onto a queue, and let a single thread handle all the writes to the contained writer. This writer is thread safe, and should be much faster than having multiple threads fight over the file IO.Note that the writer will continue to accept messages after it has been closed.
-
-
Constructor Summary
Constructors Constructor Description QueuedMessageWriter(MessageWriter writer)
Create a queued message writer.QueuedMessageWriter(MessageWriter writer, java.util.concurrent.ExecutorService executor)
Create a queued message writer using the given executor service.QueuedMessageWriter(MessageWriter writer, java.util.concurrent.ExecutorService executor, int maxQueueLength)
Create a queued message writer using the given executor service.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
int
separator()
Write an entry separator to the writer.int
size()
protected void
sleep(long ms)
<Message extends PMessage<Message,Field>,Field extends PField>
intwrite(Message message)
Write a providence message to the writer.<Message extends PMessage<Message,Field>,Field extends PField>
intwrite(PServiceCall<Message,Field> call)
Write a providence service call to the writer.
-
-
-
Constructor Detail
-
QueuedMessageWriter
public QueuedMessageWriter(MessageWriter writer)
Create a queued message writer.- Parameters:
writer
- The message writer to write to.
-
QueuedMessageWriter
public QueuedMessageWriter(MessageWriter writer, java.util.concurrent.ExecutorService executor)
Create a queued message writer using the given executor service. Note that the executor service will be shut down with the message queue.- Parameters:
writer
- The message writer to write to.executor
- The executor service running the write loop thread.
-
QueuedMessageWriter
public QueuedMessageWriter(MessageWriter writer, java.util.concurrent.ExecutorService executor, int maxQueueLength)
Create a queued message writer using the given executor service. Note that the executor service will be shut down with the message queue.- Parameters:
writer
- The message writer to write to.executor
- The executor service running the write loop thread.maxQueueLength
- The max queue length. If 0 or less, no limit is enforced. Default is 65536 (64k).
-
-
Method Detail
-
size
public int size()
- Returns:
- The current size of the in-memory queue.
-
write
public <Message extends PMessage<Message,Field>,Field extends PField> int write(Message message) throws java.io.IOException
Description copied from interface:MessageWriter
Write a providence message to the writer.- Specified by:
write
in interfaceMessageWriter
- Type Parameters:
Message
- The message type.Field
- The field type.- Parameters:
message
- The message to write.- Returns:
- The number of bytes written.
- Throws:
java.io.IOException
- If write failed.
-
write
public <Message extends PMessage<Message,Field>,Field extends PField> int write(PServiceCall<Message,Field> call) throws java.io.IOException
Description copied from interface:MessageWriter
Write a providence service call to the writer.- Specified by:
write
in interfaceMessageWriter
- Type Parameters:
Message
- The message type embedded in the call.Field
- The field type embedded in the call.- Parameters:
call
- The service call to write.- Returns:
- The number of bytes written.
- Throws:
java.io.IOException
- If write failed.
-
separator
public int separator() throws java.io.IOException
Description copied from interface:MessageWriter
Write an entry separator to the writer.- Specified by:
separator
in interfaceMessageWriter
- Returns:
- The number of bytes written.
- Throws:
java.io.IOException
- If write failed.
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
sleep
protected void sleep(long ms) throws java.lang.InterruptedException
- Throws:
java.lang.InterruptedException
-
-