@Test public void testConsumerMultipleBrowser() throws Exception { ClientSessionFactory sf = createSessionFactory(locator); ClientSession session = sf.createSession(false, true, true); session.createQueue(QUEUE, QUEUE, null, false); ClientProducer producer = session.createProducer(QUEUE); final int numMessages = 100; for (int i = 0; i < numMessages; i++) { ClientMessage message = createTextMessage(session, "m" + i); producer.send(message); } ClientConsumer consumer = session.createConsumer(QUEUE, null, true); ClientConsumer consumer2 = session.createConsumer(QUEUE, null, true); ClientConsumer consumer3 = session.createConsumer(QUEUE, null, true); for (int i = 0; i < numMessages; i++) { ClientMessage message2 = consumer.receive(1000); Assert.assertEquals("m" + i, message2.getBodyBuffer().readString()); message2 = consumer2.receive(1000); Assert.assertEquals("m" + i, message2.getBodyBuffer().readString()); message2 = consumer3.receive(1000); Assert.assertEquals("m" + i, message2.getBodyBuffer().readString()); } session.close(); }
@Test public void testBasicSend() throws Exception { SimpleString dla = new SimpleString("DLA"); SimpleString qName = new SimpleString("q1"); SimpleString adName = new SimpleString("ad1"); AddressSettings addressSettings = new AddressSettings().setMaxDeliveryAttempts(1).setDeadLetterAddress(dla); server.getAddressSettingsRepository().addMatch(adName.toString(), addressSettings); SimpleString dlq = new SimpleString("DLQ1"); clientSession.createQueue(dla, dlq, null, false); clientSession.createQueue(adName, qName, null, false); ClientProducer producer = clientSession.createProducer(adName); producer.send(createTextMessage(clientSession, "heyho!")); clientSession.start(); ClientConsumer clientConsumer = clientSession.createConsumer(qName); ClientMessage m = clientConsumer.receive(500); m.acknowledge(); Assert.assertNotNull(m); Assert.assertEquals(m.getBodyBuffer().readString(), "heyho!"); // force a cancel clientSession.rollback(); m = clientConsumer.receiveImmediate(); Assert.assertNull(m); clientConsumer.close(); clientConsumer = clientSession.createConsumer(dlq); m = clientConsumer.receive(500); Assert.assertNotNull(m); assertEquals("q1", m.getStringProperty(Message.HDR_ORIGINAL_QUEUE)); assertEquals("ad1", m.getStringProperty(Message.HDR_ORIGINAL_ADDRESS)); Assert.assertEquals(m.getBodyBuffer().readString(), "heyho!"); }
@Test public void testBody2() throws Exception { final String QUEUE_NAME = "A1"; ActiveMQServer server = createServer(true); server.start(); ServerLocator locator = createInVMNonHALocator(); ClientSessionFactory factory = locator.createSessionFactory(); ClientSession session = factory.createSession(false, true, true); session.createQueue(QUEUE_NAME, QUEUE_NAME, true); ClientProducer producer = session.createProducer(QUEUE_NAME); ClientMessage msg = session.createMessage(true); byte[] bodyTst = new byte[10]; for (int i = 0; i < 10; i++) { bodyTst[i] = (byte) (i + 1); } msg.getBodyBuffer().writeBytes(bodyTst); assertEquals(bodyTst.length, msg.getBodySize()); producer.send(msg); session.close(); locator.close(); server.stop(); ByteArrayOutputStream xmlOutputStream = new ByteArrayOutputStream(); XmlDataExporter xmlDataExporter = new XmlDataExporter(); xmlDataExporter.process( xmlOutputStream, server.getConfiguration().getBindingsDirectory(), server.getConfiguration().getJournalDirectory(), server.getConfiguration().getPagingDirectory(), server.getConfiguration().getLargeMessagesDirectory()); System.out.print(new String(xmlOutputStream.toByteArray())); clearDataRecreateServerDirs(); server.start(); locator = createInVMNonHALocator(); factory = locator.createSessionFactory(); session = factory.createSession(false, false, true); ClientSession managementSession = factory.createSession(false, true, true); ByteArrayInputStream xmlInputStream = new ByteArrayInputStream(xmlOutputStream.toByteArray()); XmlDataImporter xmlDataImporter = new XmlDataImporter(); xmlDataImporter.process(xmlInputStream, session, managementSession); ClientConsumer consumer = session.createConsumer(QUEUE_NAME); session.start(); msg = consumer.receive(CONSUMER_TIMEOUT); assertNotNull(msg); assertEquals(msg.getBodySize(), bodyTst.length); byte[] bodyRead = new byte[bodyTst.length]; msg.getBodyBuffer().readBytes(bodyRead); assertEqualsByteArrays(bodyTst, bodyRead); session.close(); locator.close(); server.stop(); }
private void dotestMultipleGroupingXACommit() throws Exception { ServerLocator locator = createInVMNonHALocator(); ClientSessionFactory sessionFactory = createSessionFactory(locator); ClientSession clientSession = sessionFactory.createSession(true, false, false); ClientProducer clientProducer = this.clientSession.createProducer(qName); ClientConsumer consumer = clientSession.createConsumer(qName); ClientConsumer consumer2 = clientSession.createConsumer(qName); clientSession.start(); Xid xid = new XidImpl("bq".getBytes(), 4, "gtid".getBytes()); clientSession.start(xid, XAResource.TMNOFLAGS); SimpleString groupId = new SimpleString("grp1"); SimpleString groupId2 = new SimpleString("grp2"); int numMessages = 100; for (int i = 0; i < numMessages; i++) { ClientMessage message = createTextMessage(clientSession, "m" + i); if (i % 2 == 0 || i == 0) { message.putStringProperty(Message.HDR_GROUP_ID, groupId); } else { message.putStringProperty(Message.HDR_GROUP_ID, groupId2); } clientProducer.send(message); } CountDownLatch latch = new CountDownLatch(numMessages); DummyMessageHandler dummyMessageHandler = new DummyMessageHandler(latch, true); consumer.setMessageHandler(dummyMessageHandler); DummyMessageHandler dummyMessageHandler2 = new DummyMessageHandler(latch, true); consumer2.setMessageHandler(dummyMessageHandler2); Assert.assertTrue(latch.await(10, TimeUnit.SECONDS)); clientSession.end(xid, XAResource.TMSUCCESS); clientSession.prepare(xid); clientSession.commit(xid, false); Assert.assertEquals(dummyMessageHandler.list.size(), 50); int i = 0; for (ClientMessage message : dummyMessageHandler.list) { Assert.assertEquals(message.getBodyBuffer().readString(), "m" + i); i += 2; } Assert.assertEquals(dummyMessageHandler2.list.size(), 50); i = 1; for (ClientMessage message : dummyMessageHandler2.list) { Assert.assertEquals(message.getBodyBuffer().readString(), "m" + i); i += 2; } consumer.close(); consumer2.close(); consumer = this.clientSession.createConsumer(qName); Assert.assertNull(consumer.receiveImmediate()); clientSession.close(); locator.close(); }
@Test public void testBody() throws Exception { final String QUEUE_NAME = "A1"; ActiveMQServer server = createServer(true); server.start(); ServerLocator locator = createInVMNonHALocator(); ClientSessionFactory factory = locator.createSessionFactory(); ClientSession session = factory.createSession(false, true, true); session.createQueue(QUEUE_NAME, QUEUE_NAME, true); ClientProducer producer = session.createProducer(QUEUE_NAME); ClientMessage msg = session.createMessage(Message.TEXT_TYPE, true); msg.getBodyBuffer().writeString("bob123"); producer.send(msg); session.close(); locator.close(); server.stop(); ByteArrayOutputStream xmlOutputStream = new ByteArrayOutputStream(); XmlDataExporter xmlDataExporter = new XmlDataExporter(); xmlDataExporter.process( xmlOutputStream, server.getConfiguration().getBindingsDirectory(), server.getConfiguration().getJournalDirectory(), server.getConfiguration().getPagingDirectory(), server.getConfiguration().getLargeMessagesDirectory()); System.out.print(new String(xmlOutputStream.toByteArray())); clearDataRecreateServerDirs(); server.start(); locator = createInVMNonHALocator(); factory = locator.createSessionFactory(); session = factory.createSession(false, false, true); ClientSession managementSession = factory.createSession(false, true, true); ByteArrayInputStream xmlInputStream = new ByteArrayInputStream(xmlOutputStream.toByteArray()); XmlDataImporter xmlDataImporter = new XmlDataImporter(); xmlDataImporter.process(xmlInputStream, session, managementSession); ClientConsumer consumer = session.createConsumer(QUEUE_NAME); session.start(); msg = consumer.receive(CONSUMER_TIMEOUT); assertNotNull(msg); assertEquals("bob123", msg.getBodyBuffer().readString()); session.close(); locator.close(); server.stop(); }
private void doTestMultipleGroupingTXCommit() throws Exception { ServerLocator locator = createInVMNonHALocator(); ClientSessionFactory sessionFactory = createSessionFactory(locator); ClientSession clientSession = sessionFactory.createSession(false, false, false); ClientProducer clientProducer = this.clientSession.createProducer(qName); clientSession.start(); ClientConsumer consumer = clientSession.createConsumer(qName); ClientConsumer consumer2 = clientSession.createConsumer(qName); // Wait a bit otherwise consumers might be busy Thread.sleep(200); SimpleString groupId = new SimpleString("grp1"); SimpleString groupId2 = new SimpleString("grp2"); int numMessages = 100; for (int i = 0; i < numMessages; i++) { ClientMessage message = createTextMessage(clientSession, "m" + i); if (i % 2 == 0 || i == 0) { message.putStringProperty(Message.HDR_GROUP_ID, groupId); } else { message.putStringProperty(Message.HDR_GROUP_ID, groupId2); } clientProducer.send(message); } CountDownLatch latch = new CountDownLatch(numMessages); DummyMessageHandler dummyMessageHandler = new DummyMessageHandler(latch, true); consumer.setMessageHandler(dummyMessageHandler); DummyMessageHandler dummyMessageHandler2 = new DummyMessageHandler(latch, true); consumer2.setMessageHandler(dummyMessageHandler2); Assert.assertTrue(latch.await(10, TimeUnit.SECONDS)); clientSession.commit(); Assert.assertEquals(dummyMessageHandler.list.size(), 50); int i = 0; for (ClientMessage message : dummyMessageHandler.list) { Assert.assertEquals(message.getBodyBuffer().readString(), "m" + i); i += 2; } Assert.assertEquals(dummyMessageHandler2.list.size(), 50); i = 1; for (ClientMessage message : dummyMessageHandler2.list) { Assert.assertEquals(message.getBodyBuffer().readString(), "m" + i); i += 2; } consumer.close(); consumer2.close(); consumer = this.clientSession.createConsumer(qName); Assert.assertNull(consumer.receiveImmediate()); clientSession.close(); locator.close(); }
@Test public void testConsumerBrowserMessageAckDoesNothing() throws Exception { ClientSessionFactory sf = createSessionFactory(locator); ClientSession session = sf.createSession(false, true, true); session.createQueue(QUEUE, QUEUE, null, false); ClientProducer producer = session.createProducer(QUEUE); final int numMessages = 100; for (int i = 0; i < numMessages; i++) { ClientMessage message = createTextMessage(session, "m" + i); producer.send(message); } ClientConsumer consumer = session.createConsumer(QUEUE, null, true); for (int i = 0; i < numMessages; i++) { ClientMessage message2 = consumer.receive(1000); message2.acknowledge(); Assert.assertEquals("m" + i, message2.getBodyBuffer().readString()); } // assert that all the messages are there and none have been acked Assert.assertEquals( 0, ((Queue) server.getPostOffice().getBinding(QUEUE).getBindable()).getDeliveringCount()); Assert.assertEquals( 100, getMessageCount(((Queue) server.getPostOffice().getBinding(QUEUE).getBindable()))); session.close(); }
@Test public void testConsumerBrowserWithStringSelector() throws Exception { ClientSessionFactory sf = createSessionFactory(locator); ClientSession session = sf.createSession(false, true, true); session.createQueue(QUEUE, QUEUE, null, false); ClientProducer producer = session.createProducer(QUEUE); final int numMessages = 100; for (int i = 0; i < numMessages; i++) { ClientMessage message = createTextMessage(session, "m" + i); if (i % 2 == 0) { message.putStringProperty(new SimpleString("color"), new SimpleString("RED")); } producer.send(message); } ClientConsumer consumer = session.createConsumer(QUEUE, new SimpleString("color = 'RED'"), true); for (int i = 0; i < numMessages; i += 2) { ClientMessage message2 = consumer.receive(1000); Assert.assertEquals("m" + i, message2.getBodyBuffer().readString()); } session.close(); }
private void doTestMultipleGroupingSingleConsumer(final boolean directDelivery) throws Exception { ClientProducer clientProducer = clientSession.createProducer(qName); ClientConsumer consumer = clientSession.createConsumer(qName); if (directDelivery) { clientSession.start(); } SimpleString groupId = new SimpleString("grp1"); SimpleString groupId2 = new SimpleString("grp2"); int numMessages = 100; for (int i = 0; i < numMessages; i++) { ClientMessage message = createTextMessage(clientSession, "m" + i); if (i % 2 == 0 || i == 0) { message.putStringProperty(Message.HDR_GROUP_ID, groupId); } else { message.putStringProperty(Message.HDR_GROUP_ID, groupId2); } clientProducer.send(message); } if (!directDelivery) { clientSession.start(); } CountDownLatch latch = new CountDownLatch(numMessages); DummyMessageHandler dummyMessageHandler = new DummyMessageHandler(latch, true); consumer.setMessageHandler(dummyMessageHandler); Assert.assertTrue(latch.await(10, TimeUnit.SECONDS)); Assert.assertEquals(dummyMessageHandler.list.size(), 100); int i = 0; for (ClientMessage message : dummyMessageHandler.list) { Assert.assertEquals(message.getBodyBuffer().readString(), "m" + i); i += 1; } consumer.close(); }
// HORNETQ- 1084 @Test public void testBasicSendWithDLAButNoBinding() throws Exception { SimpleString dla = new SimpleString("DLA"); SimpleString qName = new SimpleString("q1"); AddressSettings addressSettings = new AddressSettings().setMaxDeliveryAttempts(1).setDeadLetterAddress(dla); server.getAddressSettingsRepository().addMatch(qName.toString(), addressSettings); // SimpleString dlq = new SimpleString("DLQ1"); // clientSession.createQueue(dla, dlq, null, false); clientSession.createQueue(qName, qName, null, false); ClientProducer producer = clientSession.createProducer(qName); producer.send(createTextMessage(clientSession, "heyho!")); clientSession.start(); ClientConsumer clientConsumer = clientSession.createConsumer(qName); ClientMessage m = clientConsumer.receive(500); m.acknowledge(); Assert.assertNotNull(m); Assert.assertEquals(m.getBodyBuffer().readString(), "heyho!"); // force a cancel clientSession.rollback(); m = clientConsumer.receiveImmediate(); Assert.assertNull(m); clientConsumer.close(); Queue q = (Queue) server.getPostOffice().getBinding(qName).getBindable(); Assert.assertEquals(0, q.getDeliveringCount()); }
/** * Large message version of {@link #assertMessageBody(int, ClientMessage)}. * * @param i * @param message */ protected static void assertLargeMessageBody(final int i, final ClientMessage message) { ActiveMQBuffer buffer = message.getBodyBuffer(); for (int j = 0; j < LARGE_MESSAGE_SIZE; j++) { Assert.assertTrue( "msg " + i + ", expecting " + LARGE_MESSAGE_SIZE + " bytes, got " + j, buffer.readable()); Assert.assertEquals("equal at " + j, ActiveMQTestBase.getSamplebyte(j), buffer.readByte()); } }
private ClientSession sendAndConsume(final ClientSessionFactory sf, final boolean createQueue) throws Exception { ClientSession session = sf.createSession(false, true, true); if (createQueue) { session.createQueue(ADDRESS, ADDRESS, null, false); } ClientProducer producer = session.createProducer(ADDRESS); final int numMessages = 1000; for (int i = 0; i < numMessages; i++) { ClientMessage message = session.createMessage( ActiveMQTextMessage.TYPE, false, 0, System.currentTimeMillis(), (byte) 1); message.putIntProperty(new SimpleString("count"), i); message.getBodyBuffer().writeString("aardvarks"); producer.send(message); } ClientConsumer consumer = session.createConsumer(ADDRESS); session.start(); for (int i = 0; i < numMessages; i++) { ClientMessage message2 = consumer.receive(); assertEquals("aardvarks", message2.getBodyBuffer().readString()); assertEquals(i, message2.getObjectProperty(new SimpleString("count"))); message2.acknowledge(); } ClientMessage message3 = consumer.receiveImmediate(); assertNull(message3); return session; }
@Test public void testMultipleGroupingConsumeHalf() throws Exception { ClientProducer clientProducer = clientSession.createProducer(qName); ClientConsumer consumer = clientSession.createConsumer(qName); ClientConsumer consumer2 = clientSession.createConsumer(qName); clientSession.start(); // need to wait a bit or consumers might be busy Thread.sleep(200); SimpleString groupId = new SimpleString("grp1"); SimpleString groupId2 = new SimpleString("grp2"); int numMessages = 100; for (int i = 0; i < numMessages; i++) { ClientMessage message = createTextMessage(clientSession, "m" + i); if (i % 2 == 0 || i == 0) { message.putStringProperty(Message.HDR_GROUP_ID, groupId); } else { message.putStringProperty(Message.HDR_GROUP_ID, groupId2); } clientProducer.send(message); } for (int i = 0; i < numMessages / 2; i++) { ClientMessage cm = consumer.receive(500); Assert.assertNotNull(cm); Assert.assertEquals(cm.getBodyBuffer().readString(), "m" + i); i++; cm = consumer2.receive(500); Assert.assertNotNull(cm); Assert.assertEquals(cm.getBodyBuffer().readString(), "m" + i); } MessageGroupingTest.log.info("closing consumer2"); consumer2.close(); consumer.close(); }
@Test public void testBasicSendToNoQueue() throws Exception { SimpleString qName = new SimpleString("q1"); AddressSettings addressSettings = new AddressSettings().setMaxDeliveryAttempts(1); server.getAddressSettingsRepository().addMatch(qName.toString(), addressSettings); clientSession.createQueue(qName, qName, null, false); ClientProducer producer = clientSession.createProducer(qName); producer.send(createTextMessage(clientSession, "heyho!")); clientSession.start(); ClientConsumer clientConsumer = clientSession.createConsumer(qName); ClientMessage m = clientConsumer.receive(500); m.acknowledge(); Assert.assertNotNull(m); Assert.assertEquals(m.getBodyBuffer().readString(), "heyho!"); // force a cancel clientSession.rollback(); m = clientConsumer.receiveImmediate(); Assert.assertNull(m); clientConsumer.close(); }
@Test public void testDeadlLetterAddressWithDefaultAddressSettings() throws Exception { int deliveryAttempt = 3; SimpleString address = RandomUtil.randomSimpleString(); SimpleString queue = RandomUtil.randomSimpleString(); SimpleString deadLetterAddress = RandomUtil.randomSimpleString(); SimpleString deadLetterQueue = RandomUtil.randomSimpleString(); AddressSettings addressSettings = new AddressSettings() .setMaxDeliveryAttempts(deliveryAttempt) .setDeadLetterAddress(deadLetterAddress); server.getAddressSettingsRepository().setDefault(addressSettings); clientSession.createQueue(address, queue, false); clientSession.createQueue(deadLetterAddress, deadLetterQueue, false); ClientProducer producer = clientSession.createProducer(address); ClientMessage clientMessage = createTextMessage(clientSession, "heyho!"); producer.send(clientMessage); clientSession.start(); ClientConsumer clientConsumer = clientSession.createConsumer(queue); for (int i = 0; i < deliveryAttempt; i++) { ClientMessage m = clientConsumer.receive(500); Assert.assertNotNull(m); DeadLetterAddressTest.log.info("i is " + i); DeadLetterAddressTest.log.info("delivery cout is " + m.getDeliveryCount()); Assert.assertEquals(i + 1, m.getDeliveryCount()); m.acknowledge(); clientSession.rollback(); } ClientMessage m = clientConsumer.receive(500); Assert.assertNull("not expecting a message", m); clientConsumer.close(); clientConsumer = clientSession.createConsumer(deadLetterQueue); m = clientConsumer.receive(500); Assert.assertNotNull(m); Assert.assertEquals(m.getBodyBuffer().readString(), "heyho!"); }
@Test public void testBrowseWithZeroConsumerWindowSize() throws Exception { locator.setConsumerWindowSize(0); ClientSessionFactory sf = createSessionFactory(locator); ClientSession session = sf.createSession(false, true, true); session.createQueue(QUEUE, QUEUE, null, false); ClientProducer producer = session.createProducer(QUEUE); final int numMessages = 100; byte[] bytes = new byte[240]; for (int i = 0; i < numMessages; i++) { ClientMessage message = session.createMessage(false); message.getBodyBuffer().writeBytes(bytes); message.putIntProperty("foo", i); producer.send(message); } // Create a normal non browsing consumer session.createConsumer(QUEUE); session.start(); ClientConsumer browser = session.createConsumer(QUEUE, true); for (int i = 0; i < numMessages; i++) { ClientMessage message2 = browser.receive(1000); assertEquals(i, message2.getIntProperty("foo").intValue()); } session.close(); }
public void sendMessages(final int start, final int end) throws Exception { try (ClientSession session = factory.createSession(false, false)) { try { session.createQueue(QUEUE, QUEUE, true); } catch (Exception ignored) { } ClientProducer prod = session.createProducer(QUEUE); for (int i = start; i < end; i++) { ClientMessage msg = session.createMessage(true); msg.putIntProperty(new SimpleString("key"), i); msg.getBodyBuffer().writeUTF("message " + i); prod.send(msg); } session.commit(); session.close(); // server.stop(); -- this test was not supposed to stop the server, it should crash } }
/** * @param i * @param message * @throws Exception */ @Override protected void setBody(final int i, final ClientMessage message) { message.getBodyBuffer().writeString("message" + i); }
@Test public void testHeadersSet() throws Exception { final int MAX_DELIVERIES = 16; final int NUM_MESSAGES = 5; SimpleString dla = new SimpleString("DLA"); SimpleString qName = new SimpleString("q1"); AddressSettings addressSettings = new AddressSettings().setMaxDeliveryAttempts(MAX_DELIVERIES).setDeadLetterAddress(dla); server.getAddressSettingsRepository().addMatch(qName.toString(), addressSettings); SimpleString dlq = new SimpleString("DLQ1"); clientSession.createQueue(dla, dlq, null, false); clientSession.createQueue(qName, qName, null, false); ServerLocator locator = createInVMNonHALocator(); ClientSessionFactory sessionFactory = createSessionFactory(locator); ClientSession sendSession = sessionFactory.createSession(false, true, true); ClientProducer producer = sendSession.createProducer(qName); Map<String, Long> origIds = new HashMap<>(); for (int i = 0; i < NUM_MESSAGES; i++) { ClientMessage tm = createTextMessage(clientSession, "Message:" + i); producer.send(tm); } ClientConsumer clientConsumer = clientSession.createConsumer(qName); clientSession.start(); for (int i = 0; i < MAX_DELIVERIES; i++) { for (int j = 0; j < NUM_MESSAGES; j++) { ClientMessage tm = clientConsumer.receive(1000); Assert.assertNotNull(tm); tm.acknowledge(); if (i == 0) { origIds.put("Message:" + j, tm.getMessageID()); } Assert.assertEquals("Message:" + j, tm.getBodyBuffer().readString()); } clientSession.rollback(); } long timeout = System.currentTimeMillis() + 5000; // DLA transfer is asynchronous fired on the rollback while (System.currentTimeMillis() < timeout && getMessageCount(((Queue) server.getPostOffice().getBinding(qName).getBindable())) != 0) { Thread.sleep(1); } Assert.assertEquals( 0, getMessageCount(((Queue) server.getPostOffice().getBinding(qName).getBindable()))); ClientMessage m = clientConsumer.receiveImmediate(); Assert.assertNull(m); // All the messages should now be in the DLQ ClientConsumer cc3 = clientSession.createConsumer(dlq); for (int i = 0; i < NUM_MESSAGES; i++) { ClientMessage tm = cc3.receive(1000); Assert.assertNotNull(tm); String text = tm.getBodyBuffer().readString(); Assert.assertEquals("Message:" + i, text); // Check the headers SimpleString origDest = (SimpleString) tm.getObjectProperty(Message.HDR_ORIGINAL_ADDRESS); Long origMessageId = (Long) tm.getObjectProperty(Message.HDR_ORIG_MESSAGE_ID); Assert.assertEquals(qName, origDest); Long origId = origIds.get(text); Assert.assertEquals(origId, origMessageId); } sendSession.close(); }
@Test public void testPagedLargeMessage() throws Exception { final String MY_ADDRESS = "myAddress"; final String MY_QUEUE = "myQueue"; ActiveMQServer server = createServer(true); AddressSettings defaultSetting = new AddressSettings().setPageSizeBytes(10 * 1024).setMaxSizeBytes(20 * 1024); server.getAddressSettingsRepository().addMatch("#", defaultSetting); server.start(); ServerLocator locator = createInVMNonHALocator() // Making it synchronous, just because we want to stop sending messages as soon as the // page-store becomes in // page mode and we could only guarantee that by setting it to synchronous .setBlockOnNonDurableSend(true) .setBlockOnDurableSend(true) .setBlockOnAcknowledge(true); ClientSessionFactory factory = locator.createSessionFactory(); ClientSession session = factory.createSession(false, true, true); session.createQueue(MY_ADDRESS, MY_QUEUE, true); ClientProducer producer = session.createProducer(MY_ADDRESS); ClientMessage message = session.createMessage(true); message.getBodyBuffer().writeBytes(new byte[1024]); for (int i = 0; i < 200; i++) { producer.send(message); } LargeServerMessageImpl fileMessage = new LargeServerMessageImpl((JournalStorageManager) server.getStorageManager()); fileMessage.setMessageID(1005); fileMessage.setDurable(true); for (int i = 0; i < 2 * ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE; i++) { fileMessage.addBytes(new byte[] {getSamplebyte(i)}); } fileMessage.putLongProperty( Message.HDR_LARGE_BODY_SIZE, 2 * ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE); fileMessage.releaseResources(); producer.send(fileMessage); fileMessage.deleteFile(); session.close(); locator.close(); server.stop(); ByteArrayOutputStream xmlOutputStream = new ByteArrayOutputStream(); XmlDataExporter xmlDataExporter = new XmlDataExporter(); xmlDataExporter.process( xmlOutputStream, server.getConfiguration().getBindingsDirectory(), server.getConfiguration().getJournalDirectory(), server.getConfiguration().getPagingDirectory(), server.getConfiguration().getLargeMessagesDirectory()); // System.out.print(new String(xmlOutputStream.toByteArray())); clearDataRecreateServerDirs(); server.start(); locator = createInVMNonHALocator(); factory = locator.createSessionFactory(); session = factory.createSession(false, true, true); ByteArrayInputStream xmlInputStream = new ByteArrayInputStream(xmlOutputStream.toByteArray()); XmlDataImporter xmlDataImporter = new XmlDataImporter(); xmlDataImporter.process(xmlInputStream, session); ClientConsumer consumer = session.createConsumer(MY_QUEUE); session.start(); for (int i = 0; i < 200; i++) { message = consumer.receive(CONSUMER_TIMEOUT); assertNotNull(message); } ClientMessage msg = consumer.receive(CONSUMER_TIMEOUT); assertNotNull(msg); assertEquals(2 * ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE, msg.getBodySize()); for (int i = 0; i < 2 * ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE; i++) { assertEquals(getSamplebyte(i), msg.getBodyBuffer().readByte()); } session.close(); locator.close(); server.stop(); }
@Test public void testPaging() throws Exception { final String MY_ADDRESS = "myAddress"; final String MY_QUEUE = "myQueue"; server = createServer(true); AddressSettings defaultSetting = new AddressSettings().setPageSizeBytes(10 * 1024).setMaxSizeBytes(20 * 1024); server.getAddressSettingsRepository().addMatch("#", defaultSetting); server.start(); locator = createInVMNonHALocator() // Making it synchronous, just because we want to stop sending messages as soon as the // page-store becomes in // page mode and we could only guarantee that by setting it to synchronous .setBlockOnNonDurableSend(true) .setBlockOnDurableSend(true) .setBlockOnAcknowledge(true); factory = createSessionFactory(locator); ClientSession session = factory.createSession(false, true, true); session.createQueue(MY_ADDRESS, MY_QUEUE, true); ClientProducer producer = session.createProducer(MY_ADDRESS); ClientMessage message = session.createMessage(true); message.getBodyBuffer().writeBytes(new byte[1024]); for (int i = 0; i < 200; i++) { producer.send(message); } session.close(); locator.close(); server.stop(); ByteArrayOutputStream xmlOutputStream = new ByteArrayOutputStream(); XmlDataExporter xmlDataExporter = new XmlDataExporter(); xmlDataExporter.process( xmlOutputStream, server.getConfiguration().getBindingsDirectory(), server.getConfiguration().getJournalDirectory(), server.getConfiguration().getPagingDirectory(), server.getConfiguration().getLargeMessagesDirectory()); System.out.print(new String(xmlOutputStream.toByteArray())); clearDataRecreateServerDirs(); server.start(); locator = createInVMNonHALocator(); factory = createSessionFactory(locator); session = factory.createSession(false, true, true); ByteArrayInputStream xmlInputStream = new ByteArrayInputStream(xmlOutputStream.toByteArray()); XmlDataImporter xmlDataImporter = new XmlDataImporter(); xmlDataImporter.process(xmlInputStream, session); ClientConsumer consumer = session.createConsumer(MY_QUEUE); session.start(); for (int i = 0; i < 200; i++) { message = consumer.receive(CONSUMER_TIMEOUT); assertNotNull(message); } }
@Test public void testMessageProperties() throws Exception { ClientSession session = basicSetUp(); session.createQueue(QUEUE_NAME, QUEUE_NAME, true); ClientProducer producer = session.createProducer(QUEUE_NAME); StringBuilder international = new StringBuilder(); for (char x = 800; x < 1200; x++) { international.append(x); } String special = "\"<>'&"; for (int i = 0; i < 5; i++) { ClientMessage msg = session.createMessage(true); msg.getBodyBuffer().writeString("Bob the giant pig " + i); msg.putBooleanProperty("myBooleanProperty", Boolean.TRUE); msg.putByteProperty("myByteProperty", new Byte("0")); msg.putBytesProperty("myBytesProperty", new byte[] {0, 1, 2, 3, 4}); msg.putDoubleProperty("myDoubleProperty", i * 1.6); msg.putFloatProperty("myFloatProperty", i * 2.5F); msg.putIntProperty("myIntProperty", i); msg.putLongProperty("myLongProperty", Long.MAX_VALUE - i); msg.putObjectProperty("myObjectProperty", i); msg.putObjectProperty("myNullObjectProperty", null); msg.putShortProperty("myShortProperty", new Integer(i).shortValue()); msg.putStringProperty("myStringProperty", "myStringPropertyValue_" + i); msg.putStringProperty("myNullStringProperty", null); msg.putStringProperty("myNonAsciiStringProperty", international.toString()); msg.putStringProperty("mySpecialCharacters", special); msg.putStringProperty( new SimpleString("mySimpleStringProperty"), new SimpleString("mySimpleStringPropertyValue_" + i)); msg.putStringProperty(new SimpleString("myNullSimpleStringProperty"), null); producer.send(msg); } session.close(); locator.close(); server.stop(); ByteArrayOutputStream xmlOutputStream = new ByteArrayOutputStream(); XmlDataExporter xmlDataExporter = new XmlDataExporter(); xmlDataExporter.process( xmlOutputStream, server.getConfiguration().getBindingsDirectory(), server.getConfiguration().getJournalDirectory(), server.getConfiguration().getPagingDirectory(), server.getConfiguration().getLargeMessagesDirectory()); System.out.print(new String(xmlOutputStream.toByteArray())); clearDataRecreateServerDirs(); server.start(); locator = createInVMNonHALocator(); factory = createSessionFactory(locator); session = factory.createSession(false, true, true); ByteArrayInputStream xmlInputStream = new ByteArrayInputStream(xmlOutputStream.toByteArray()); XmlDataImporter xmlDataImporter = new XmlDataImporter(); xmlDataImporter.process(xmlInputStream, session); ClientConsumer consumer = session.createConsumer(QUEUE_NAME); session.start(); for (int i = 0; i < 5; i++) { ClientMessage msg = consumer.receive(CONSUMER_TIMEOUT); byte[] body = new byte[msg.getBodySize()]; msg.getBodyBuffer().readBytes(body); assertTrue(new String(body).contains("Bob the giant pig " + i)); assertEquals(msg.getBooleanProperty("myBooleanProperty"), Boolean.TRUE); assertEquals(msg.getByteProperty("myByteProperty"), new Byte("0")); byte[] bytes = msg.getBytesProperty("myBytesProperty"); for (int j = 0; j < 5; j++) { assertEquals(j, bytes[j]); } assertEquals(i * 1.6, msg.getDoubleProperty("myDoubleProperty"), 0.000001); assertEquals(i * 2.5F, msg.getFloatProperty("myFloatProperty"), 0.000001); assertEquals(i, msg.getIntProperty("myIntProperty").intValue()); assertEquals(Long.MAX_VALUE - i, msg.getLongProperty("myLongProperty").longValue()); assertEquals(i, msg.getObjectProperty("myObjectProperty")); assertEquals(null, msg.getObjectProperty("myNullObjectProperty")); assertEquals( new Integer(i).shortValue(), msg.getShortProperty("myShortProperty").shortValue()); assertEquals("myStringPropertyValue_" + i, msg.getStringProperty("myStringProperty")); assertEquals(null, msg.getStringProperty("myNullStringProperty")); assertEquals(international.toString(), msg.getStringProperty("myNonAsciiStringProperty")); assertEquals(special, msg.getStringProperty("mySpecialCharacters")); assertEquals( new SimpleString("mySimpleStringPropertyValue_" + i), msg.getSimpleStringProperty(new SimpleString("mySimpleStringProperty"))); assertEquals( null, msg.getSimpleStringProperty(new SimpleString("myNullSimpleStringProperty"))); } }
@Test public void testLargeMessage() throws Exception { server = createServer(true); server.start(); locator = createInVMNonHALocator(); factory = createSessionFactory(locator); ClientSession session = factory.createSession(false, false); LargeServerMessageImpl fileMessage = new LargeServerMessageImpl((JournalStorageManager) server.getStorageManager()); fileMessage.setMessageID(1005); fileMessage.setDurable(true); for (int i = 0; i < 2 * ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE; i++) { fileMessage.addBytes(new byte[] {getSamplebyte(i)}); } fileMessage.putLongProperty( Message.HDR_LARGE_BODY_SIZE, 2 * ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE); fileMessage.releaseResources(); session.createQueue("A", "A", true); ClientProducer prod = session.createProducer("A"); prod.send(fileMessage); fileMessage.deleteFile(); session.commit(); session.close(); locator.close(); server.stop(); ByteArrayOutputStream xmlOutputStream = new ByteArrayOutputStream(); XmlDataExporter xmlDataExporter = new XmlDataExporter(); xmlDataExporter.process( xmlOutputStream, server.getConfiguration().getBindingsDirectory(), server.getConfiguration().getJournalDirectory(), server.getConfiguration().getPagingDirectory(), server.getConfiguration().getLargeMessagesDirectory()); System.out.print(new String(xmlOutputStream.toByteArray())); clearDataRecreateServerDirs(); server.start(); locator = createInVMNonHALocator(); factory = createSessionFactory(locator); session = factory.createSession(false, true, true); ByteArrayInputStream xmlInputStream = new ByteArrayInputStream(xmlOutputStream.toByteArray()); XmlDataImporter xmlDataImporter = new XmlDataImporter(); xmlDataImporter.process(xmlInputStream, session); session.close(); session = factory.createSession(false, false); session.start(); ClientConsumer cons = session.createConsumer("A"); ClientMessage msg = cons.receive(CONSUMER_TIMEOUT); assertNotNull(msg); assertEquals(2 * ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE, msg.getBodySize()); for (int i = 0; i < 2 * ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE; i++) { assertEquals(getSamplebyte(i), msg.getBodyBuffer().readByte()); } msg.acknowledge(); session.commit(); }
private void doTestTransactional(final TestRunner runner) throws Throwable { // For duplication detection int executionId = 0; while (!runner.isFailed()) { ClientSession session = null; executionId++; log.info("#test doTestTransactional starting now. Execution " + executionId); try { boolean retry = false; final int numMessages = 1000; session = sf.createSession(false, false); listener = new CountDownSessionFailureListener(session); session.addFailureListener(listener); do { try { ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS); for (int i = 0; i < numMessages; i++) { ClientMessage message = session.createMessage(true); message.getBodyBuffer().writeString("message" + i); message.putIntProperty("counter", i); message.putStringProperty( Message.HDR_DUPLICATE_DETECTION_ID, new SimpleString("id:" + i + ",exec:" + executionId)); addPayload(message); if (log.isDebugEnabled()) { log.debug("Sending message " + message); } producer.send(message); } log.debug("Sending commit"); session.commit(); retry = false; } catch (ActiveMQDuplicateIdException die) { logAndSystemOut("#test duplicate id rejected on sending"); break; } catch (ActiveMQTransactionRolledBackException trbe) { log.info("#test transaction rollback retrying on sending"); // OK retry = true; } catch (ActiveMQUnBlockedException ube) { log.info("#test transaction rollback retrying on sending"); // OK retry = true; } catch (ActiveMQTransactionOutcomeUnknownException toue) { log.info("#test transaction rollback retrying on sending"); // OK retry = true; } catch (ActiveMQException e) { log.info("#test Exception " + e, e); throw e; } } while (retry); logAndSystemOut("#test Finished sending, starting consumption now"); boolean blocked = false; retry = false; ClientConsumer consumer = null; do { ArrayList<Integer> msgs = new ArrayList<>(); try { if (consumer == null) { consumer = session.createConsumer(FailoverTestBase.ADDRESS); session.start(); } for (int i = 0; i < numMessages; i++) { if (log.isDebugEnabled()) { log.debug("Consumer receiving message " + i); } ClientMessage message = consumer.receive(10000); if (message == null) { break; } if (log.isDebugEnabled()) { log.debug("Received message " + message); } int count = message.getIntProperty("counter"); if (count != i) { log.warn("count was received out of order, " + count + "!=" + i); } msgs.add(count); message.acknowledge(); } log.info("#test commit"); try { session.commit(); } catch (ActiveMQTransactionRolledBackException trbe) { // we know the tx has been rolled back so we just consume again retry = true; continue; } catch (ActiveMQException e) { // This could eventually happen // We will get rid of this when we implement 2 phase commit on failover log.warn("exception during commit, it will be ignored for now" + e.getMessage(), e); } try { if (blocked) { assertTrue( "msgs.size is expected to be 0 or " + numMessages + " but it was " + msgs.size(), msgs.size() == 0 || msgs.size() == numMessages); } else { assertTrue( "msgs.size is expected to be " + numMessages + " but it was " + msgs.size(), msgs.size() == numMessages); } } catch (Throwable e) { log.info(threadDump("Thread dump, messagesReceived = " + msgs.size())); logAndSystemOut(e.getMessage() + " messages received"); for (Integer msg : msgs) { logAndSystemOut(msg.toString()); } throw e; } int i = 0; for (Integer msg : msgs) { assertEquals(i++, (int) msg); } retry = false; blocked = false; } catch (ActiveMQTransactionRolledBackException trbe) { logAndSystemOut("Transaction rolled back with " + msgs.size(), trbe); // TODO: https://jira.jboss.org/jira/browse/HORNETQ-369 // ATM RolledBack exception is being called with the transaction is committed. // the test will fail if you remove this next line blocked = true; retry = true; } catch (ActiveMQTransactionOutcomeUnknownException tou) { logAndSystemOut("Transaction rolled back with " + msgs.size(), tou); // TODO: https://jira.jboss.org/jira/browse/HORNETQ-369 // ATM RolledBack exception is being called with the transaction is committed. // the test will fail if you remove this next line blocked = true; retry = true; } catch (ActiveMQUnBlockedException ube) { logAndSystemOut("Unblocked with " + msgs.size(), ube); // TODO: https://jira.jboss.org/jira/browse/HORNETQ-369 // This part of the test is never being called. blocked = true; retry = true; } catch (ActiveMQException e) { logAndSystemOut(e.getMessage(), e); throw e; } } while (retry); } finally { if (session != null) { session.close(); } } listener = null; } }
private void doTestNonTransactional(final TestRunner runner) throws Exception { while (!runner.isFailed()) { AsynchronousFailoverTest.log.info("looping"); ClientSession session = sf.createSession(true, true, 0); listener = new CountDownSessionFailureListener(session); session.addFailureListener(listener); ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS); final int numMessages = 1000; for (int i = 0; i < numMessages; i++) { boolean retry = false; do { try { ClientMessage message = session.createMessage(true); message.getBodyBuffer().writeString("message" + i); message.putIntProperty("counter", i); addPayload(message); producer.send(message); retry = false; } catch (ActiveMQUnBlockedException ube) { AsynchronousFailoverTest.log.info("exception when sending message with counter " + i); ube.printStackTrace(); retry = true; } catch (ActiveMQException e) { fail("Invalid Exception type:" + e.getType()); } } while (retry); } // create the consumer with retry if failover occurs during createConsumer call ClientConsumer consumer = null; boolean retry = false; do { try { consumer = session.createConsumer(FailoverTestBase.ADDRESS); retry = false; } catch (ActiveMQUnBlockedException ube) { AsynchronousFailoverTest.log.info("exception when creating consumer"); retry = true; } catch (ActiveMQException e) { fail("Invalid Exception type:" + e.getType()); } } while (retry); session.start(); List<Integer> counts = new ArrayList<>(1000); int lastCount = -1; boolean counterGap = false; while (true) { ClientMessage message = consumer.receive(500); if (message == null) { break; } // messages must remain ordered but there could be a "jump" if messages // are missing or duplicated int count = message.getIntProperty("counter"); counts.add(count); if (count != lastCount + 1) { if (counterGap) { Assert.fail("got another counter gap at " + count + ": " + counts); } else { if (lastCount != -1) { AsynchronousFailoverTest.log.info("got first counter gap at " + count); counterGap = true; } } } lastCount = count; message.acknowledge(); } session.close(); this.listener = null; } }
/** * @param sf * @param NMSGS * @throws ActiveMQException * @throws InterruptedException * @throws Throwable */ private void produceMessages(final ClientSessionFactory sf, final int NMSGS) throws Throwable { final int TIMEOUT = 5000; System.out.println("sending " + NMSGS + " messages"); final ClientSession sessionSend = sf.createSession(true, true); ClientProducer prod2 = sessionSend.createProducer("slow-queue"); try { sessionSend.createQueue("Queue", "Queue", true); } catch (Exception ignored) { } final ClientSession sessionReceive = sf.createSession(true, true); sessionReceive.start(); final ArrayList<Throwable> errors = new ArrayList<>(); Thread tReceive = new Thread() { @Override public void run() { try { ClientConsumer consumer = sessionReceive.createConsumer("Queue"); for (int i = 0; i < NMSGS; i++) { if (i % 500 == 0) { double percent = (double) i / (double) NMSGS; System.out.println( "msgs " + i + " of " + NMSGS + ", " + (int) (percent * 100) + "%"); Thread.sleep(100); } ClientMessage msg = consumer.receive(TIMEOUT); if (msg == null) { errors.add(new Exception("Didn't receive msgs")); break; } msg.acknowledge(); } } catch (Exception e) { errors.add(e); } } }; tReceive.start(); ClientProducer prod = sessionSend.createProducer("Queue"); Random random = new Random(); for (int i = 0; i < NMSGS; i++) { ClientMessage msg = sessionSend.createMessage(true); int size = RandomUtil.randomPositiveInt() % 10024; if (size == 0) { size = 10 * 1024; } byte[] buffer = new byte[size]; random.nextBytes(buffer); msg.getBodyBuffer().writeBytes(buffer); prod.send(msg); if (i % 5000 == 0) { prod2.send(msg); System.out.println("Sending slow message"); } } tReceive.join(); sessionReceive.close(); sessionSend.close(); sf.close(); for (Throwable e : errors) { throw e; } }