public void createBody(final int initialMessageBufferSize) { buffer = HornetQBuffers.dynamicBuffer(initialMessageBufferSize); // There's a bug in netty which means a dynamic buffer won't resize until you write a byte buffer.writeByte((byte) 0); buffer.setIndex(BODY_OFFSET, BODY_OFFSET); }
public HornetQBuffer toHornetQBuffer() throws Exception { if (buffer == null) { if (bytesBody != null) { buffer = HornetQBuffers.dynamicBuffer(bytesBody.length + 512); } else { buffer = HornetQBuffers.dynamicBuffer(512); } if (isPing()) { buffer.writeByte((byte) 10); return buffer; } StringBuffer head = new StringBuffer(); head.append(command); head.append(Stomp.NEWLINE); // Output the headers. for (Map.Entry<String, String> header : headers.entrySet()) { head.append(header.getKey()); head.append(Stomp.Headers.SEPARATOR); head.append(header.getValue()); head.append(Stomp.NEWLINE); } // Add a newline to separate the headers from the content. head.append(Stomp.NEWLINE); buffer.writeBytes(head.toString().getBytes("UTF-8")); if (bytesBody != null) { buffer.writeBytes(bytesBody); } buffer.writeBytes(END_OF_FRAME); size = buffer.writerIndex(); } return buffer; }
@Test public void testSendPackets() throws Exception { setupServer(true); StorageManager storage = getStorage(); manager = liveServer.getReplicationManager(); waitForComponent(manager); Journal replicatedJournal = new ReplicatedJournal((byte) 1, new FakeJournal(), manager); replicatedJournal.appendPrepareRecord(1, new FakeData(), false); replicatedJournal.appendAddRecord(1, (byte) 1, new FakeData(), false); replicatedJournal.appendUpdateRecord(1, (byte) 2, new FakeData(), false); replicatedJournal.appendDeleteRecord(1, false); replicatedJournal.appendAddRecordTransactional(2, 2, (byte) 1, new FakeData()); replicatedJournal.appendUpdateRecordTransactional(2, 2, (byte) 2, new FakeData()); replicatedJournal.appendCommitRecord(2, false); replicatedJournal.appendDeleteRecordTransactional(3, 4, new FakeData()); replicatedJournal.appendPrepareRecord(3, new FakeData(), false); replicatedJournal.appendRollbackRecord(3, false); blockOnReplication(storage, manager); Assert.assertTrue( "Expecting no active tokens:" + manager.getActiveTokens(), manager.getActiveTokens().isEmpty()); ServerMessage msg = new ServerMessageImpl(1, 1024); SimpleString dummy = new SimpleString("dummy"); msg.setAddress(dummy); replicatedJournal.appendAddRecordTransactional(23, 24, (byte) 1, new FakeData()); PagedMessage pgmsg = new PagedMessageImpl(msg, new long[0]); manager.pageWrite(pgmsg, 1); manager.pageWrite(pgmsg, 2); manager.pageWrite(pgmsg, 3); manager.pageWrite(pgmsg, 4); blockOnReplication(storage, manager); PagingManager pagingManager = createPageManager( backupServer.getStorageManager(), backupServer.getConfiguration(), backupServer.getExecutorFactory(), backupServer.getAddressSettingsRepository()); PagingStore store = pagingManager.getPageStore(dummy); store.start(); Assert.assertEquals(4, store.getNumberOfPages()); store.stop(); manager.pageDeleted(dummy, 1); manager.pageDeleted(dummy, 2); manager.pageDeleted(dummy, 3); manager.pageDeleted(dummy, 4); manager.pageDeleted(dummy, 5); manager.pageDeleted(dummy, 6); blockOnReplication(storage, manager); ServerMessageImpl serverMsg = new ServerMessageImpl(); serverMsg.setMessageID(500); serverMsg.setAddress(new SimpleString("tttt")); HornetQBuffer buffer = HornetQBuffers.dynamicBuffer(100); serverMsg.encodeHeadersAndProperties(buffer); manager.largeMessageBegin(500); manager.largeMessageWrite(500, new byte[1024]); manager.largeMessageDelete(Long.valueOf(500)); blockOnReplication(storage, manager); store.start(); Assert.assertEquals(0, store.getNumberOfPages()); }
public static SequentialFile writeControlFile( final SequentialFileFactory fileFactory, final List<JournalFile> files, final List<JournalFile> newFiles, final List<Pair<String, String>> renames) throws Exception { SequentialFile controlFile = fileFactory.createSequentialFile(AbstractJournalUpdateTask.FILE_COMPACT_CONTROL, 1); try { controlFile.open(1, false); JournalImpl.initFileHeader(fileFactory, controlFile, 0, 0); HornetQBuffer filesToRename = HornetQBuffers.dynamicBuffer(1); // DataFiles first if (files == null) { filesToRename.writeInt(0); } else { filesToRename.writeInt(files.size()); for (JournalFile file : files) { filesToRename.writeUTF(file.getFile().getFileName()); } } // New Files second if (newFiles == null) { filesToRename.writeInt(0); } else { filesToRename.writeInt(newFiles.size()); for (JournalFile file : newFiles) { filesToRename.writeUTF(file.getFile().getFileName()); } } // Renames from clean up third if (renames == null) { filesToRename.writeInt(0); } else { filesToRename.writeInt(renames.size()); for (Pair<String, String> rename : renames) { filesToRename.writeUTF(rename.getA()); filesToRename.writeUTF(rename.getB()); } } JournalInternalRecord controlRecord = new JournalAddRecord( true, 1, (byte) 0, new ByteArrayEncoding(filesToRename.toByteBuffer().array())); HornetQBuffer renameBuffer = HornetQBuffers.dynamicBuffer(filesToRename.writerIndex()); controlRecord.setFileID(0); controlRecord.encode(renameBuffer); ByteBuffer writeBuffer = fileFactory.newBuffer(renameBuffer.writerIndex()); writeBuffer.put(renameBuffer.toByteBuffer().array(), 0, renameBuffer.writerIndex()); writeBuffer.rewind(); controlFile.writeDirect(writeBuffer, true); return controlFile; } finally { controlFile.close(); } }