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(); }
@Test public void testNonMatchingMessagesFollowedByMatchingMessagesMany() throws Exception { for (int i = 0; i < QueueImpl.MAX_DELIVERIES_IN_LOOP * 2; i++) { ClientMessage message = session.createMessage(false); message.putStringProperty("animal", "hippo"); producer.send(message); } assertNull(consumer.receiveImmediate()); for (int i = 0; i < QueueImpl.MAX_DELIVERIES_IN_LOOP * 2; i++) { ClientMessage message = session.createMessage(false); message.putStringProperty("animal", "giraffe"); producer.send(message); } for (int i = 0; i < QueueImpl.MAX_DELIVERIES_IN_LOOP * 2; i++) { ClientMessage received = consumer.receiveImmediate(); assertNotNull(received); assertEquals("giraffe", received.getStringProperty("animal")); } assertNull(consumer.receiveImmediate()); session.close(); }
@Test public void testNonMatchingMessagesFollowedByMatchingMessages() throws Exception { ClientMessage message = session.createMessage(false); message.putStringProperty("animal", "hippo"); producer.send(message); assertNull(consumer.receiveImmediate()); message = session.createMessage(false); message.putStringProperty("animal", "giraffe"); log.info("sending second msg"); producer.send(message); ClientMessage received = consumer.receiveImmediate(); assertNotNull(received); assertEquals("giraffe", received.getStringProperty("animal")); assertNull(consumer.receiveImmediate()); session.close(); }
/** * @param session * @param producer * @throws Exception */ private void sendMessage(ClientSession session, ClientProducer producer, String color, String msg) throws Exception { ClientMessage anyMessage = session.createMessage(true); anyMessage.putStringProperty("color", color); anyMessage.putStringProperty("value", msg); producer.send(anyMessage); session.commit(); }
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(); }
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(); }
private void doTestBasicGrouping() throws Exception { ClientProducer clientProducer = clientSession.createProducer(qName); ClientConsumer consumer = clientSession.createConsumer(qName); ClientConsumer consumer2 = clientSession.createConsumer(qName); clientSession.start(); SimpleString groupId = new SimpleString("grp1"); int numMessages = 100; for (int i = 0; i < numMessages; i++) { ClientMessage message = createTextMessage(clientSession, "m" + i); message.putStringProperty(Message.HDR_GROUP_ID, groupId); 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)); Assert.assertEquals(100, dummyMessageHandler.list.size()); Assert.assertEquals(0, dummyMessageHandler2.list.size()); consumer.close(); consumer2.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(); }
public void sendMessages(String queueName, int num) throws ActiveMQException { ClientSession session = sessionFactory.createSession(); ClientProducer producer = session.createProducer("jms.queue." + queueName); for (int i = 0; i < num; i++) { ClientMessage m = session.createMessage(true); m.putStringProperty("bridge-message", "hello " + index); index++; producer.send(m); } session.close(); }
@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 testLoadBalanceGroups() throws Exception { Assume.assumeFalse( "only makes sense withOUT auto-group", clientSessionFactory.getServerLocator().isAutoGroup()); ClientProducer clientProducer = clientSession.createProducer(qName); ClientConsumer consumer1 = clientSession.createConsumer(qName); ClientConsumer consumer2 = clientSession.createConsumer(qName); ClientConsumer consumer3 = clientSession.createConsumer(qName); ClientConsumer[] consumers = new ClientConsumer[] {consumer1, consumer2, consumer3}; int[] counts = new int[consumers.length]; clientSession.start(); try { // Add all messages for a particular group before moving onto the next for (int group = 0; group < 10; group++) { for (int messageId = 0; messageId < 3; messageId++) { ClientMessage message = clientSession.createMessage(false); message.putStringProperty("_AMQ_GROUP_ID", "" + group); clientProducer.send(message); } } for (int c = 0; c < consumers.length; c++) { while (true) { ClientMessage msg = consumers[c].receiveImmediate(); if (msg == null) { break; } counts[c]++; } } for (int count : counts) { Assert.assertNotEquals( "You shouldn't have all messages bound to a single consumer", 30, count); Assert.assertNotEquals( "But you shouldn't have also a single consumer bound to none", 0, count); } } finally { consumer1.close(); consumer2.close(); consumer3.close(); } }
@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"))); } }
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; } }
@Test public void testTwoConsumers() throws Exception { ClientConsumer consumer2 = session.createConsumer("foo", "animal='elephant'"); // Create and consume message that matches the first consumer's filter ClientMessage message = session.createMessage(false); message.putStringProperty("animal", "giraffe"); producer.send(message); ClientMessage received = consumer.receive(10000); assertNotNull(received); assertEquals("giraffe", received.getStringProperty("animal")); assertNull(consumer.receiveImmediate()); assertNull(consumer2.receiveImmediate()); // Create and consume another message that matches the first consumer's filter message = session.createMessage(false); message.putStringProperty("animal", "giraffe"); producer.send(message); received = consumer.receive(10000); assertNotNull(received); assertEquals("giraffe", received.getStringProperty("animal")); assertNull(consumer.receiveImmediate()); assertNull(consumer2.receiveImmediate()); // Create and consume a message that matches the second consumer's filter message = session.createMessage(false); message.putStringProperty("animal", "elephant"); producer.send(message); received = consumer2.receive(10000); assertNotNull(received); assertEquals("elephant", received.getStringProperty("animal")); assertNull(consumer.receiveImmediate()); assertNull(consumer2.receiveImmediate()); // Create and consume another message that matches the second consumer's filter message = session.createMessage(false); message.putStringProperty("animal", "elephant"); producer.send(message); received = consumer2.receive(1000); assertNotNull(received); assertEquals("elephant", received.getStringProperty("animal")); assertNull(consumer.receiveImmediate()); assertNull(consumer2.receiveImmediate()); session.close(); }
@Test public void testDeployWithFilter() throws Exception { final String testAddress = "testAddress"; final String queueName1 = "queue1"; final String filter = "cheese='camembert'"; CoreQueueConfiguration queue1 = new CoreQueueConfiguration() .setAddress(testAddress) .setName(queueName1) .setFilterString(filter) .setDurable(false); configuration.addQueueConfiguration(queue1); ActiveMQServer server = addServer(ActiveMQServers.newActiveMQServer(configuration, false)); server.start(); ServerLocator locator = createInVMNonHALocator(); ClientSessionFactory sf = createSessionFactory(locator); ClientSession session = addClientSession(sf.createSession(false, true, true)); ClientProducer producer = session.createProducer(new SimpleString(testAddress)); final SimpleString propKey = new SimpleString("testkey"); final int numMessages = 1; PredefinedQueueTest.log.info("sending messages"); for (int i = 0; i < numMessages; i++) { ClientMessage message = session.createMessage(true); message.putStringProperty(new SimpleString("cheese"), new SimpleString("camembert")); message.putIntProperty(propKey, i); producer.send(message); } session.start(); ClientConsumer consumer1 = session.createConsumer(queueName1); for (int i = 0; i < numMessages; i++) { ClientMessage message = consumer1.receive(200); Assert.assertNotNull(message); Assert.assertEquals(i, message.getObjectProperty(propKey)); message.acknowledge(); } Assert.assertNull(consumer1.receiveImmediate()); for (int i = 0; i < numMessages; i++) { ClientMessage message = session.createMessage(true); message.putStringProperty(new SimpleString("cheese"), new SimpleString("roquefort")); message.putIntProperty(propKey, i); producer.send(message); } Assert.assertNull(consumer1.receiveImmediate()); }