use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.
the class QueueControlTest method testRemoveAllWithPagingMode.
@Test
public void testRemoveAllWithPagingMode() throws Exception {
// 3k
final int MESSAGE_SIZE = 1024 * 3;
// reset maxSize for Paging mode
Field maxSizField = PagingManagerImpl.class.getDeclaredField("maxSize");
maxSizField.setAccessible(true);
maxSizField.setLong(server.getPagingManager(), 10240);
clearDataRecreateServerDirs();
SimpleString address = RandomUtil.randomSimpleString();
SimpleString queueName = RandomUtil.randomSimpleString();
session.createQueue(address, RoutingType.MULTICAST, queueName, null, durable);
Queue queue = server.locateQueue(queueName);
Assert.assertEquals(false, queue.getPageSubscription().isPaging());
ClientProducer producer = session.createProducer(address);
byte[] body = new byte[MESSAGE_SIZE];
ByteBuffer bb = ByteBuffer.wrap(body);
for (int j = 1; j <= MESSAGE_SIZE; j++) {
bb.put(getSamplebyte(j));
}
final int numberOfMessages = 8000;
ClientMessage message;
for (int i = 0; i < numberOfMessages; i++) {
message = session.createMessage(true);
ActiveMQBuffer bodyLocal = message.getBodyBuffer();
bodyLocal.writeBytes(body);
producer.send(message);
}
Assert.assertEquals(true, queue.getPageSubscription().isPaging());
QueueControl queueControl = createManagementControl(address, queueName);
assertMessageMetrics(queueControl, numberOfMessages, durable);
int removedMatchedMessagesCount = queueControl.removeAllMessages();
Assert.assertEquals(numberOfMessages, removedMatchedMessagesCount);
assertMessageMetrics(queueControl, 0, durable);
Field queueMemoprySizeField = QueueImpl.class.getDeclaredField("queueMemorySize");
queueMemoprySizeField.setAccessible(true);
AtomicInteger queueMemorySize = (AtomicInteger) queueMemoprySizeField.get(queue);
Assert.assertEquals(0, queueMemorySize.get());
session.deleteQueue(queueName);
}
use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.
the class NIOSequentialFileFactoryTest method testInterrupts.
@Test
public void testInterrupts() throws Throwable {
final EncodingSupport fakeEncoding = new EncodingSupport() {
@Override
public int getEncodeSize() {
return 10;
}
@Override
public void encode(ActiveMQBuffer buffer) {
buffer.writeBytes(new byte[10]);
}
@Override
public void decode(ActiveMQBuffer buffer) {
}
};
final AtomicInteger calls = new AtomicInteger(0);
final NIOSequentialFileFactory factory = new NIOSequentialFileFactory(new File(getTestDir()), new IOCriticalErrorListener() {
@Override
public void onIOException(Throwable code, String message, SequentialFile file) {
new Exception("shutdown").printStackTrace();
calls.incrementAndGet();
}
}, 1);
Thread threadOpen = new Thread() {
@Override
public void run() {
try {
Thread.currentThread().interrupt();
SequentialFile file = factory.createSequentialFile("file.txt");
file.open();
} catch (Exception e) {
e.printStackTrace();
}
}
};
threadOpen.start();
threadOpen.join();
Thread threadClose = new Thread() {
@Override
public void run() {
try {
SequentialFile file = factory.createSequentialFile("file.txt");
file.open();
file.write(fakeEncoding, true);
Thread.currentThread().interrupt();
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
};
threadClose.start();
threadClose.join();
Thread threadWrite = new Thread() {
@Override
public void run() {
try {
SequentialFile file = factory.createSequentialFile("file.txt");
file.open();
Thread.currentThread().interrupt();
file.write(fakeEncoding, true);
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
};
threadWrite.start();
threadWrite.join();
Thread threadFill = new Thread() {
@Override
public void run() {
try {
SequentialFile file = factory.createSequentialFile("file.txt");
file.open();
Thread.currentThread().interrupt();
file.fill(1024);
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
};
threadFill.start();
threadFill.join();
Thread threadWriteDirect = new Thread() {
@Override
public void run() {
try {
SequentialFile file = factory.createSequentialFile("file.txt");
file.open();
ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.put(new byte[10]);
Thread.currentThread().interrupt();
file.writeDirect(buffer, true);
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
};
threadWriteDirect.start();
threadWriteDirect.join();
Thread threadRead = new Thread() {
@Override
public void run() {
try {
SequentialFile file = factory.createSequentialFile("file.txt");
file.open();
file.write(fakeEncoding, true);
file.position(0);
ByteBuffer readBytes = ByteBuffer.allocate(fakeEncoding.getEncodeSize());
Thread.currentThread().interrupt();
file.read(readBytes);
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
};
threadRead.start();
threadRead.join();
// An interrupt exception shouldn't issue a shutdown
Assert.assertEquals(0, calls.get());
}
use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.
the class ActiveMQServerControlTest method sendMessagesWithPredefinedSize.
private void sendMessagesWithPredefinedSize(int numberOfMessages, ClientSession session, ClientProducer producer, int messageSize) throws Exception {
ClientMessage message;
final byte[] body = new byte[messageSize];
ByteBuffer bb = ByteBuffer.wrap(body);
for (int i = 1; i <= messageSize; i++) {
bb.put(getSamplebyte(i));
}
for (int i = 0; i < numberOfMessages; i++) {
message = session.createMessage(true);
ActiveMQBuffer bodyLocal = message.getBodyBuffer();
bodyLocal.writeBytes(body);
producer.send(message);
if (i % 1000 == 0) {
session.commit();
}
}
session.commit();
}
use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.
the class SelfExpandingBufferTest method testSelfExpandingBuffer.
private void testSelfExpandingBuffer(final boolean netty, final boolean persistent) throws Exception {
setUpService(netty, persistent);
ClientSessionFactory factory;
ServerLocator locator = createFactory(netty);
factory = createSessionFactory(locator);
ClientSession session = factory.createSession(false, true, true);
try {
session.createQueue(ADDRESS, ADDRESS, true);
ClientMessage msg = session.createMessage(true);
ActiveMQBuffer buffer = msg.getBodyBuffer();
SelfExpandingBufferTest.log.info("buffer is " + buffer);
byte[] bytes = RandomUtil.randomBytes(10 * buffer.capacity());
buffer.writeBytes(bytes);
ClientProducer prod = session.createProducer(ADDRESS);
prod.send(msg);
// Send same message again
prod.send(msg);
ClientConsumer cons = session.createConsumer(ADDRESS);
session.start();
ClientMessage msg2 = cons.receive(3000);
Assert.assertNotNull(msg2);
byte[] receivedBytes = new byte[bytes.length];
// log.info("buffer start pos should be at " + PacketImpl.PACKET_HEADERS_SIZE + DataConstants.SIZE_INT);
//
// log.info("buffer pos at " + msg2.getBodyBuffer().readerIndex());
//
// log.info("buffer length should be " + msg2.getBodyBuffer().readInt(PacketImpl.PACKET_HEADERS_SIZE));
msg2.getBodyBuffer().readBytes(receivedBytes);
ActiveMQTestBase.assertEqualsByteArrays(bytes, receivedBytes);
msg2 = cons.receive(3000);
Assert.assertNotNull(msg2);
msg2.getBodyBuffer().readBytes(receivedBytes);
ActiveMQTestBase.assertEqualsByteArrays(bytes, receivedBytes);
} finally {
session.close();
}
}
use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.
the class BridgeTest method testBridgeWithLargeMessage.
@Test
public void testBridgeWithLargeMessage() throws Exception {
ActiveMQServer server0 = null;
ActiveMQServer server1 = null;
final int PAGE_MAX = 1024 * 1024;
final int PAGE_SIZE = 10 * 1024;
ServerLocator locator = null;
try {
Map<String, Object> server0Params = new HashMap<>();
server0 = createClusteredServerWithParams(isNetty(), 0, true, PAGE_SIZE, PAGE_MAX, server0Params);
Map<String, Object> server1Params = new HashMap<>();
addTargetParameters(server1Params);
server1 = createClusteredServerWithParams(isNetty(), 1, true, server1Params);
final String testAddress = "testAddress";
final String queueName0 = "queue0";
final String forwardAddress = "forwardAddress";
final String queueName1 = "queue1";
Map<String, TransportConfiguration> connectors = new HashMap<>();
TransportConfiguration server0tc = new TransportConfiguration(getConnector(), server0Params);
TransportConfiguration server1tc = new TransportConfiguration(getConnector(), server1Params);
connectors.put(server1tc.getName(), server1tc);
server0.getConfiguration().setConnectorConfigurations(connectors);
ArrayList<String> staticConnectors = new ArrayList<>();
staticConnectors.add(server1tc.getName());
BridgeConfiguration bridgeConfiguration = new BridgeConfiguration().setName("bridge1").setQueueName(queueName0).setForwardingAddress(forwardAddress).setRetryInterval(1000).setReconnectAttemptsOnSameNode(-1).setUseDuplicateDetection(false).setConfirmationWindowSize(1024).setStaticConnectors(staticConnectors);
List<BridgeConfiguration> bridgeConfigs = new ArrayList<>();
bridgeConfigs.add(bridgeConfiguration);
server0.getConfiguration().setBridgeConfigurations(bridgeConfigs);
CoreQueueConfiguration queueConfig0 = new CoreQueueConfiguration().setAddress(testAddress).setName(queueName0);
List<CoreQueueConfiguration> queueConfigs0 = new ArrayList<>();
queueConfigs0.add(queueConfig0);
server0.getConfiguration().setQueueConfigurations(queueConfigs0);
CoreQueueConfiguration queueConfig1 = new CoreQueueConfiguration().setAddress(forwardAddress).setName(queueName1);
List<CoreQueueConfiguration> queueConfigs1 = new ArrayList<>();
queueConfigs1.add(queueConfig1);
server1.getConfiguration().setQueueConfigurations(queueConfigs1);
server1.start();
server0.start();
locator = addServerLocator(ActiveMQClient.createServerLocatorWithoutHA(server0tc, server1tc));
ClientSessionFactory sf0 = locator.createSessionFactory(server0tc);
ClientSessionFactory sf1 = locator.createSessionFactory(server1tc);
ClientSession session0 = sf0.createSession(false, true, true);
ClientSession session1 = sf1.createSession(false, true, true);
ClientProducer producer0 = session0.createProducer(new SimpleString(testAddress));
ClientConsumer consumer1 = session1.createConsumer(queueName1);
session1.start();
final int numMessages = 50;
final SimpleString propKey = new SimpleString("testkey");
final int LARGE_MESSAGE_SIZE = 1024;
for (int i = 0; i < numMessages; i++) {
ClientMessage message = session0.createMessage(true);
message.setBodyInputStream(createFakeLargeStream(LARGE_MESSAGE_SIZE));
message.putIntProperty(propKey, i);
producer0.send(message);
}
session0.commit();
for (int i = 0; i < numMessages; i++) {
ClientMessage message = consumer1.receive(5000);
Assert.assertNotNull(message);
Assert.assertEquals(i, message.getObjectProperty(propKey));
ActiveMQBuffer buff = message.getBodyBuffer();
for (int posMsg = 0; posMsg < LARGE_MESSAGE_SIZE; posMsg++) {
assertEquals(getSamplebyte(posMsg), buff.readByte());
}
message.acknowledge();
}
session1.commit();
Assert.assertNull(consumer1.receiveImmediate());
session0.close();
session1.close();
sf0.close();
sf1.close();
} finally {
if (locator != null) {
locator.close();
}
try {
server0.stop();
} catch (Throwable ignored) {
}
try {
server1.stop();
} catch (Throwable ignored) {
}
}
assertEquals(0, loadQueues(server0).size());
}
Aggregations