OrderedExecutorFactory factory = new OrderedExecutorFactory(executor);
final ExecutorService deleteExecutor = Executors.newCachedThreadPool();
final JournalStorageManager storage = new JournalStorageManager(config, factory);
storage.start();
storage.loadInternalOnly();
((JournalImpl)storage.getMessageJournal()).setAutoReclaim(false);
final LinkedList<Long> survivingMsgs = new LinkedList<Long>();
Runnable producerRunnable = new Runnable()
{
public void run()
{
try
{
while (running.get())
{
final long[] values = new long[100];
long tx = seqGenerator.incrementAndGet();
OperationContextImpl ctx = new OperationContextImpl(executor);
storage.setContext(ctx);
for (int i = 0; i < 100; i++)
{
long id = seqGenerator.incrementAndGet();
values[i] = id;
ServerMessageImpl message = new ServerMessageImpl(id, 100);
message.getBodyBuffer().writeBytes(new byte[1024]);
storage.storeMessageTransactional(tx, message);
}
ServerMessageImpl message = new ServerMessageImpl(seqGenerator.incrementAndGet(), 100);
survivingMsgs.add(message.getMessageID());
// This one will stay here forever
storage.storeMessage(message);
storage.commit(tx);
ctx.executeOnCompletion(new IOAsyncTask()
{
public void onError(int errorCode, String errorMessage)
{
}
public void done()
{
deleteExecutor.execute(new Runnable()
{
public void run()
{
try
{
for (long messageID : values)
{
storage.deleteMessage(messageID);
}
}
catch (Exception e)
{
e.printStackTrace();
errors.incrementAndGet();
}
}
});
}
});
}
}
catch (Throwable e)
{
e.printStackTrace();
errors.incrementAndGet();
}
}
};
Runnable compressRunnable = new Runnable()
{
public void run()
{
try
{
int i = 0;
while (running.get())
{
Thread.sleep(500);
System.out.println("Compacting");
((JournalImpl)storage.getMessageJournal()).testCompact();
((JournalImpl)storage.getMessageJournal()).checkReclaimStatus();
}
}
catch (Throwable e)
{
e.printStackTrace();
errors.incrementAndGet();
}
}
};
Thread producerThread = new Thread(producerRunnable);
producerThread.start();
Thread compactorThread = new Thread(compressRunnable);
compactorThread.start();
Thread.sleep(10000);
running.set(false);
producerThread.join();
compactorThread.join();
executor.shutdown();
assertTrue(executor.awaitTermination(10, TimeUnit.SECONDS));
deleteExecutor.shutdown();
assertTrue(deleteExecutor.awaitTermination(30, TimeUnit.SECONDS));
storage.stop();
}