List of usage examples for io.netty.buffer ByteBuf readerIndex
public abstract int readerIndex();
From source file:org.apache.activemq.artemis.protocol.amqp.converter.message.JMSMappingOutboundTransformerTest.java
License:Apache License
public EncodedMessage transform(ServerJMSMessage message) throws Exception { // Useful for testing but not recommended for real life use. ByteBuf nettyBuffer = Unpooled.buffer(1024); NettyWritable buffer = new NettyWritable(nettyBuffer); long messageFormat = transformer.transform(message, buffer); EncodedMessage encoded = new EncodedMessage(messageFormat, nettyBuffer.array(), nettyBuffer.arrayOffset() + nettyBuffer.readerIndex(), nettyBuffer.readableBytes()); return encoded; }
From source file:org.apache.activemq.artemis.protocol.amqp.converter.ProtonMessageConverter.java
License:Apache License
@Override public Object outbound(ServerMessage messageOutbound, int deliveryCount) throws Exception { // Useful for testing but not recommended for real life use. ByteBuf nettyBuffer = Unpooled.buffer(1024); NettyWritable buffer = new NettyWritable(nettyBuffer); long messageFormat = (long) outbound(messageOutbound, deliveryCount, buffer); EncodedMessage encoded = new EncodedMessage(messageFormat, nettyBuffer.array(), nettyBuffer.arrayOffset() + nettyBuffer.readerIndex(), nettyBuffer.readableBytes()); return encoded; }
From source file:org.apache.activemq.artemis.protocol.amqp.proton.ProtonServerSenderContext.java
License:Apache License
/** * handle an out going message from ActiveMQ Artemis, send via the Proton Sender */// w ww . jav a2 s.c o m public int deliverMessage(MessageReference messageReference, int deliveryCount, Connection transportConnection) throws Exception { if (closed) { return 0; } AMQPMessage message = CoreAmqpConverter.checkAMQP(messageReference.getMessage()); sessionSPI.invokeOutgoing(message, (ActiveMQProtonRemotingConnection) transportConnection.getProtocolConnection()); // presettle means we can settle the message on the dealer side before we send it, i.e. // for browsers boolean preSettle = sender.getRemoteSenderSettleMode() == SenderSettleMode.SETTLED; // we only need a tag if we are going to settle later byte[] tag = preSettle ? new byte[0] : protonSession.getTag(); ByteBuf nettyBuffer = PooledByteBufAllocator.DEFAULT.heapBuffer(message.getEncodeSize()); try { message.sendBuffer(nettyBuffer, deliveryCount); int size = nettyBuffer.writerIndex(); while (!connection.tryLock(1, TimeUnit.SECONDS)) { if (closed || sender.getLocalState() == EndpointState.CLOSED) { // If we're waiting on the connection lock, the link might be in the process of closing. If this happens // we return. return 0; } else { if (log.isDebugEnabled()) { log.debug("Couldn't get lock on deliverMessage " + this); } } } try { final Delivery delivery; delivery = sender.delivery(tag, 0, tag.length); delivery.setMessageFormat((int) message.getMessageFormat()); delivery.setContext(messageReference); // this will avoid a copy.. patch provided by Norman using buffer.array() sender.send(nettyBuffer.array(), nettyBuffer.arrayOffset() + nettyBuffer.readerIndex(), nettyBuffer.readableBytes()); if (preSettle) { // Presettled means the client implicitly accepts any delivery we send it. sessionSPI.ack(null, brokerConsumer, messageReference.getMessage()); delivery.settle(); } else { sender.advance(); } connection.flush(); } finally { connection.unlock(); } return size; } finally { nettyBuffer.release(); } }
From source file:org.apache.activemq.artemis.protocol.amqp.util.DeliveryUtil.java
License:Apache License
public static MessageImpl decodeMessageImpl(ByteBuf buffer) { MessageImpl message = (MessageImpl) Message.Factory.create(); message.decode(buffer.array(), buffer.arrayOffset() + buffer.readerIndex(), buffer.readableBytes()); return message; }
From source file:org.apache.activemq.artemis.tests.integration.transports.netty.ActiveMQFrameDecoder2Test.java
License:Apache License
@Test public void testOrdinaryFragmentation() throws Exception { final EmbeddedChannel decoder = new EmbeddedChannel(new ActiveMQFrameDecoder2()); final byte[] data = new byte[ActiveMQFrameDecoder2Test.MSG_LEN]; ActiveMQFrameDecoder2Test.rand.nextBytes(data); ByteBuf src = Unpooled.buffer(ActiveMQFrameDecoder2Test.MSG_CNT * (ActiveMQFrameDecoder2Test.MSG_LEN + 4)); while (src.writerIndex() < src.capacity()) { src.writeInt(ActiveMQFrameDecoder2Test.MSG_LEN); src.writeBytes(data);//from w ww . j ava 2 s. co m } List<ByteBuf> packets = new ArrayList<ByteBuf>(); while (src.isReadable()) { int length = Math.min( ActiveMQFrameDecoder2Test.rand.nextInt(ActiveMQFrameDecoder2Test.FRAGMENT_MAX_LEN), src.readableBytes()); packets.add(src.readBytes(length)); } int cnt = 0; for (ByteBuf p : packets) { decoder.writeInbound(p); for (;;) { ByteBuf frame = (ByteBuf) decoder.readInbound(); if (frame == null) { break; } Assert.assertEquals(4, frame.readerIndex()); Assert.assertEquals(ActiveMQFrameDecoder2Test.MSG_LEN, frame.readableBytes()); Assert.assertEquals(Unpooled.wrappedBuffer(data), frame); cnt++; frame.release(); } } Assert.assertEquals(ActiveMQFrameDecoder2Test.MSG_CNT, cnt); }
From source file:org.apache.activemq.artemis.tests.integration.transports.netty.ActiveMQFrameDecoder2Test.java
License:Apache License
@Test public void testExtremeFragmentation() throws Exception { final EmbeddedChannel decoder = new EmbeddedChannel(new ActiveMQFrameDecoder2()); decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0 })); Assert.assertNull(decoder.readInbound()); decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0 })); Assert.assertNull(decoder.readInbound()); decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0 })); Assert.assertNull(decoder.readInbound()); decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 4 })); Assert.assertNull(decoder.readInbound()); decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 5 })); Assert.assertNull(decoder.readInbound()); decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 6 })); Assert.assertNull(decoder.readInbound()); decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 7 })); Assert.assertNull(decoder.readInbound()); decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 8 })); ByteBuf frame = (ByteBuf) decoder.readInbound(); Assert.assertEquals(4, frame.readerIndex()); Assert.assertEquals(4, frame.readableBytes()); Assert.assertEquals(5, frame.getByte(4)); Assert.assertEquals(6, frame.getByte(5)); Assert.assertEquals(7, frame.getByte(6)); Assert.assertEquals(8, frame.getByte(7)); frame.release();/*from w w w .ja v a2 s. c o m*/ }
From source file:org.apache.activemq.artemis.tests.unit.core.journal.impl.TimedBufferTest.java
License:Apache License
@Test public void testFillBuffer() { final ArrayList<ByteBuffer> buffers = new ArrayList<>(); final AtomicInteger flushTimes = new AtomicInteger(0); class TestObserver implements TimedBufferObserver { @Override/*w ww . j av a2 s. c o m*/ public void flushBuffer(final ByteBuf byteBuf, final boolean sync, final List<IOCallback> callbacks) { final ByteBuffer buffer = ByteBuffer.allocate(byteBuf.readableBytes()); buffer.limit(byteBuf.readableBytes()); byteBuf.getBytes(byteBuf.readerIndex(), buffer); buffer.flip(); buffers.add(buffer); flushTimes.incrementAndGet(); } @Override public int getRemainingBytes() { return 1024 * 1024; } } TimedBuffer timedBuffer = new TimedBuffer(null, 100, TimedBufferTest.ONE_SECOND_IN_NANOS, false); timedBuffer.start(); try { timedBuffer.setObserver(new TestObserver()); int x = 0; for (int i = 0; i < 10; i++) { byte[] bytes = new byte[10]; for (int j = 0; j < 10; j++) { bytes[j] = ActiveMQTestBase.getSamplebyte(x++); } ActiveMQBuffer buff = ActiveMQBuffers.wrappedBuffer(bytes); timedBuffer.checkSize(10); timedBuffer.addBytes(buff, false, dummyCallback); } timedBuffer.checkSize(1); Assert.assertEquals(1, flushTimes.get()); ByteBuffer flushedBuffer = buffers.get(0); Assert.assertEquals(100, flushedBuffer.limit()); Assert.assertEquals(100, flushedBuffer.capacity()); flushedBuffer.rewind(); for (int i = 0; i < 100; i++) { Assert.assertEquals(ActiveMQTestBase.getSamplebyte(i), flushedBuffer.get()); } } finally { timedBuffer.stop(); } }
From source file:org.apache.activemq.artemis.tests.unit.core.journal.impl.TimedBufferTest.java
License:Apache License
@Test public void testTimeOnTimedBuffer() throws Exception { final ReusableLatch latchFlushed = new ReusableLatch(0); final AtomicInteger flushes = new AtomicInteger(0); class TestObserver implements TimedBufferObserver { @Override//from w ww. j a v a 2s. co m public void flushBuffer(final ByteBuf byteBuf, final boolean sync, final List<IOCallback> callbacks) { final ByteBuffer buffer = ByteBuffer.allocate(byteBuf.readableBytes()); buffer.limit(byteBuf.readableBytes()); byteBuf.getBytes(byteBuf.readerIndex(), buffer); for (IOCallback callback : callbacks) { callback.done(); } } @Override public int getRemainingBytes() { return 1024 * 1024; } } TimedBuffer timedBuffer = new TimedBuffer(null, 100, TimedBufferTest.ONE_SECOND_IN_NANOS / 2, false); timedBuffer.start(); TestObserver observer = new TestObserver(); timedBuffer.setObserver(observer); int x = 0; byte[] bytes = new byte[10]; for (int j = 0; j < 10; j++) { bytes[j] = ActiveMQTestBase.getSamplebyte(x++); } ActiveMQBuffer buff = ActiveMQBuffers.wrappedBuffer(bytes); IOCallback callback = new IOCallback() { @Override public void done() { System.out.println("done"); latchFlushed.countDown(); } @Override public void onError(int errorCode, String errorMessage) { } }; try { latchFlushed.setCount(2); // simulating a low load period timedBuffer.addBytes(buff, true, callback); Thread.sleep(1000); timedBuffer.addBytes(buff, true, callback); Assert.assertTrue(latchFlushed.await(5, TimeUnit.SECONDS)); latchFlushed.setCount(5); flushes.set(0); // Sending like crazy... still some wait (1 millisecond) between each send.. long time = System.currentTimeMillis(); for (int i = 0; i < 5; i++) { timedBuffer.addBytes(buff, true, callback); Thread.sleep(1); } Assert.assertTrue(latchFlushed.await(5, TimeUnit.SECONDS)); // The purpose of the timed buffer is to batch writes up to a millisecond.. or up to the size of the buffer. Assert.assertTrue( "Timed Buffer is not batching accordingly, it was expected to take at least 500 seconds batching multiple writes while it took " + (System.currentTimeMillis() - time) + " milliseconds", System.currentTimeMillis() - time >= 450); // ^^ there are some discounts that can happen inside the timed buffer that are still considered valid (like discounting the time it took to perform the operation itself // for that reason the test has been failing (before this commit) at 499 or 480 milliseconds. So, I'm using a reasonable number close to 500 milliseconds that would still be valid for the test // it should be in fact only writing once.. // i will set for 3 just in case there's a GC or anything else happening on the test Assert.assertTrue("Too many writes were called", flushes.get() <= 3); } finally { timedBuffer.stop(); } }
From source file:org.apache.activemq.artemis.tests.unit.core.journal.impl.TimedBufferTest.java
License:Apache License
@Test public void testTimingAndFlush() throws Exception { final ArrayList<ByteBuffer> buffers = new ArrayList<>(); final AtomicInteger flushTimes = new AtomicInteger(0); class TestObserver implements TimedBufferObserver { @Override//from w ww .j ava 2 s . c o m public void flushBuffer(final ByteBuf byteBuf, final boolean sync, final List<IOCallback> callbacks) { final ByteBuffer buffer = ByteBuffer.allocate(byteBuf.readableBytes()); buffer.limit(byteBuf.readableBytes()); byteBuf.getBytes(byteBuf.readerIndex(), buffer); buffer.flip(); buffers.add(buffer); flushTimes.incrementAndGet(); } @Override public int getRemainingBytes() { return 1024 * 1024; } } TimedBuffer timedBuffer = new TimedBuffer(null, 100, TimedBufferTest.ONE_SECOND_IN_NANOS / 10, false); timedBuffer.start(); try { timedBuffer.setObserver(new TestObserver()); int x = 0; byte[] bytes = new byte[10]; for (int j = 0; j < 10; j++) { bytes[j] = ActiveMQTestBase.getSamplebyte(x++); } ActiveMQBuffer buff = ActiveMQBuffers.wrappedBuffer(bytes); timedBuffer.checkSize(10); timedBuffer.addBytes(buff, false, dummyCallback); Thread.sleep(200); Assert.assertEquals(0, flushTimes.get()); bytes = new byte[10]; for (int j = 0; j < 10; j++) { bytes[j] = ActiveMQTestBase.getSamplebyte(x++); } buff = ActiveMQBuffers.wrappedBuffer(bytes); timedBuffer.checkSize(10); timedBuffer.addBytes(buff, true, dummyCallback); Thread.sleep(500); Assert.assertEquals(1, flushTimes.get()); ByteBuffer flushedBuffer = buffers.get(0); Assert.assertEquals(20, flushedBuffer.limit()); Assert.assertEquals(20, flushedBuffer.capacity()); flushedBuffer.rewind(); for (int i = 0; i < 20; i++) { Assert.assertEquals(ActiveMQTestBase.getSamplebyte(i), flushedBuffer.get()); } } finally { timedBuffer.stop(); } }
From source file:org.apache.activemq.artemis.utils.AbstractByteBufPool.java
License:Apache License
/** * Returns a pooled entry if possible, a new one otherwise. * <p>/*ww w. jav a 2 s. co m*/ * The {@code byteBuf}'s {@link ByteBuf#readerIndex()} is incremented by {@code length} after it. */ public final T getOrCreate(final ByteBuf byteBuf) { final int length = byteBuf.readInt(); if (!canPool(byteBuf, length)) { return create(byteBuf, length); } else { if (!byteBuf.isReadable(length)) { throw new IndexOutOfBoundsException(); } final int bytesOffset = byteBuf.readerIndex(); final int hashCode = hashCode(byteBuf, bytesOffset, length); //fast % operation with power of 2 entries.length final int firstIndex = hashCode & mask; final T firstEntry = entries[firstIndex]; if (isEqual(firstEntry, byteBuf, bytesOffset, length)) { byteBuf.skipBytes(length); return firstEntry; } final int secondIndex = (hashCode >> shift) & mask; final T secondEntry = entries[secondIndex]; if (isEqual(secondEntry, byteBuf, bytesOffset, length)) { byteBuf.skipBytes(length); return secondEntry; } final T internedEntry = create(byteBuf, length); final int entryIndex = firstEntry == null ? firstIndex : secondIndex; entries[entryIndex] = internedEntry; return internedEntry; } }