/** * Basic fail-back test. * * @throws Exception */ @Test public void testFailBack() throws Exception { createSessionFactory(); ClientSession session = sendAndConsume(sf, true); ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS); sendMessages(session, producer, NUM_MESSAGES); session.commit(); crash(session); session.start(); ClientConsumer consumer = session.createConsumer(FailoverTestBase.ADDRESS); receiveMessages(consumer, 0, NUM_MESSAGES, true); producer = session.createProducer(FailoverTestBase.ADDRESS); sendMessages(session, producer, 2 * NUM_MESSAGES); session.commit(); assertFalse("must NOT be a backup", liveServer.getServer().getConfiguration().isBackup()); adaptLiveConfigForReplicatedFailBack(liveServer.getServer().getConfiguration()); CountDownSessionFailureListener listener = new CountDownSessionFailureListener(); session.addFailureListener(listener); liveServer.start(); assertTrue(listener.getLatch().await(5, TimeUnit.SECONDS)); assertTrue( "live initialized after restart", liveServer.getServer().waitForActivation(15, TimeUnit.SECONDS)); session.start(); receiveMessages(consumer, 0, NUM_MESSAGES, true); }
public void testCommitPreparedTransactions() throws Exception { SimpleString recQueue = new SimpleString("BasicXaTestqRec"); SimpleString sendQueue = new SimpleString("BasicXaTestqSend"); byte[] globalTransactionId = UUIDGenerator.getInstance().generateStringUUID().getBytes(); Xid xid = new XidImpl("xa1".getBytes(), 1, globalTransactionId); Xid xid2 = new XidImpl("xa2".getBytes(), 1, globalTransactionId); ServerLocator locator = HornetQClient.createServerLocatorWithoutHA( new TransportConfiguration(UnitTestCase.INVM_CONNECTOR_FACTORY)); ClientSessionFactory csf = locator.createSessionFactory(); ClientSession clientSession = csf.createSession(true, false, false); clientSession.createQueue(recQueue, recQueue, null, true); clientSession.createQueue(sendQueue, sendQueue, null, true); ClientMessage m1 = createTextMessage(clientSession, ""); m1.putStringProperty("m1", "m1"); ClientProducer clientProducer = clientSession.createProducer(recQueue); clientProducer.send(m1); locator.close(); ServerLocator receiveLocator = HornetQClient.createServerLocatorWithoutHA( new TransportConfiguration(UnitTestCase.INVM_CONNECTOR_FACTORY)); ClientSessionFactory receiveCsf = receiveLocator.createSessionFactory(); ClientSession receiveClientSession = receiveCsf.createSession(true, false, false); ClientConsumer consumer = receiveClientSession.createConsumer(recQueue); ServerLocator sendLocator = HornetQClient.createServerLocatorWithoutHA( new TransportConfiguration(UnitTestCase.INVM_CONNECTOR_FACTORY)); ClientSessionFactory sendCsf = sendLocator.createSessionFactory(); ClientSession sendClientSession = sendCsf.createSession(true, false, false); ClientProducer producer = sendClientSession.createProducer(sendQueue); receiveClientSession.start(xid, XAResource.TMNOFLAGS); receiveClientSession.start(); sendClientSession.start(xid2, XAResource.TMNOFLAGS); ClientMessage m = consumer.receive(5000); assertNotNull(m); producer.send(m); receiveClientSession.end(xid, XAResource.TMSUCCESS); sendClientSession.end(xid2, XAResource.TMSUCCESS); receiveClientSession.prepare(xid); sendClientSession.prepare(xid2); HornetQServerControl serverControl = createManagementControl(); sendLocator.close(); receiveLocator.close(); boolean success = serverControl.commitPreparedTransaction(XidImpl.toBase64String(xid)); success = serverControl.commitPreparedTransaction(XidImpl.toBase64String(xid)); System.out.println("HornetQServerControlTest.testCommitPreparedTransactions"); }
/** * Connects to the yamcs server. This method blocks until a connection is established or some * error has occurred, thus this should be called in a separate thread. Hornetq will try * indefinitely to establish the connection and also provides automatic re-connection afterwards. * * @throws Exception if the hornetq session could not be established due to some error */ public void connect() throws Exception { for (ConnectionListener cl : connectionListeners) { cl.connecting(connParams.getUrl()); } Map<String, Object> tcpConfig = new HashMap<String, Object>(); tcpConfig.put(TransportConstants.HOST_PROP_NAME, connParams.host); tcpConfig.put(TransportConstants.PORT_PROP_NAME, connParams.port); locator = HornetQClient.createServerLocatorWithoutHA( new TransportConfiguration(NettyConnectorFactory.class.getName(), tcpConfig)); locator.setInitialConnectAttempts(initialConnectAttempts); locator.setReconnectAttempts(reconnectAttempts); locator.setRetryInterval(retryInterval); locator.setRetryIntervalMultiplier(retryIntervalMultiplier); locator.setMaxRetryInterval(maxRetryInterval); locator.setAckBatchSize(ackBatchSize); sessionFactory = locator.createSessionFactory(); // TODO Use hornetq auth (like YamcsConnector), or keep anonymous connection? session = sessionFactory.createSession(false, true, true, preAcknowledge); session.addFailureListener(YamcsAckConnector.this); session.start(); for (ConnectionListener cl : connectionListeners) { cl.connected(connParams.getUrl()); } }
private void receiveMessages() throws Exception { ClientSession session = sf.createSession(true, true); session.start(); ClientConsumer consumer = session.createConsumer(ADDRESS); for (int i = 0; i < numMessages; i++) { ClientMessage message = consumer.receive(100); assertNotNull("Expecting a message " + i, message); assertMessageBody(i, message); assertEquals(i, message.getIntProperty("int").intValue()); assertEquals((short) i, message.getShortProperty("short").shortValue()); assertEquals((byte) i, message.getByteProperty("byte").byteValue()); assertEquals(floatValue(i), message.getFloatProperty("float").floatValue(), 0.001); assertEquals( new SimpleString(Integer.toString(i)), message.getSimpleStringProperty(SIMPLE_STRING_KEY.toString())); assertEqualsByteArrays(byteArray(i), message.getBytesProperty("byte[]")); assertTrue(message.containsProperty("null-value")); assertEquals(message.getObjectProperty("null-value"), null); message.acknowledge(); } assertNull("no more messages", consumer.receive(50)); consumer.close(); session.commit(); }
@Override public void run() { ClientSession session = null; latchReady.countDown(); try { UnitTestCase.waitForLatch(latchStart); session = sf.createSession(true, true); session.start(); ClientConsumer cons = session.createConsumer(CompactingStressTest.Q2); for (int i = 0; i < NUMBER_OF_FAST_MESSAGES; i++) { ClientMessage msg = cons.receive(60 * 1000); msg.acknowledge(); } Assert.assertNull(cons.receiveImmediate()); } catch (Throwable e) { this.e = e; } finally { try { session.close(); } catch (Throwable e) { this.e = e; } } }
/** * Remove any old message queues that have a 0 message count in them. This lets us not worry about * changing around the registered listeners. */ private void cleanupOldQueues() { log.debug("Cleaning old message queues"); try { String[] queues = hornetqServer.getHornetQServer().getHornetQServerControl().getQueueNames(); ServerLocator locator = HornetQClient.createServerLocatorWithoutHA( new TransportConfiguration(InVMConnectorFactory.class.getName())); ClientSessionFactory factory = locator.createSessionFactory(); ClientSession session = factory.createSession(true, true); session.start(); for (int i = 0; i < queues.length; i++) { long msgCount = session.queueQuery(new SimpleString(queues[i])).getMessageCount(); if (msgCount == 0) { log.debug(String.format("found queue '%s' with 0 messages. deleting", queues[i])); session.deleteQueue(queues[i]); } else { log.debug(String.format("found queue '%s' with %d messages. kept", queues[i], msgCount)); } } session.stop(); session.close(); } catch (HornetQException e) { log.error("Problem cleaning old message queues:", e); throw new RuntimeException(e); } catch (Exception e) { log.error("Problem cleaning old message queues:", e); throw new RuntimeException(e); } }
public void testConsumerAckImmediateAutoCommitFalse() throws Exception { ClientSessionFactory sf = createSessionFactory(locator); ClientSession session = sf.createSession(false, true, false, 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); session.start(); for (int i = 0; i < numMessages; i++) { ClientMessage message2 = consumer.receive(1000); 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( 0, ((Queue) server.getPostOffice().getBinding(QUEUE).getBindable()).getMessageCount()); session.close(); }
public void testRestartJournal() throws Throwable { runExternalProcess(0, JournalCrashTest.FIRST_RUN); runExternalProcess(JournalCrashTest.FIRST_RUN, JournalCrashTest.SECOND_RUN); runExternalProcess(JournalCrashTest.SECOND_RUN, JournalCrashTest.THIRD_RUN); runExternalProcess(JournalCrashTest.THIRD_RUN, JournalCrashTest.FOURTH_RUN); printJournal(); ClientSession session = null; try { startServer(); session = factory.createSession(true, true); ClientConsumer consumer = session.createConsumer(QUEUE); session.start(); for (int i = 0; i < JournalCrashTest.FOURTH_RUN; i++) { ClientMessage msg = consumer.receive(5000); Assert.assertNotNull("Msg at " + i, msg); msg.acknowledge(); Assert.assertEquals(i, msg.getObjectProperty(new SimpleString("key"))); } session.close(); } finally { try { session.close(); } catch (Throwable ignored) { } } }
// https://jira.jboss.org/browse/HORNETQ-410 public void testConsumeWithNoConsumerFlowControl() throws Exception { ServerLocator locator = createInVMNonHALocator(); locator.setConsumerWindowSize(-1); ClientSessionFactory sf = createSessionFactory(locator); ClientSession session = sf.createSession(false, true, true); session.createQueue(QUEUE, QUEUE, null, false); session.start(); 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); for (int i = 0; i < numMessages; i++) { ClientMessage message = consumer.receive(10000); assertNotNull(message); message.acknowledge(); } session.close(); sf.close(); locator.close(); }
private void drainQueue() throws Exception { PerfBase.log.info("Draining queue"); ClientSession session = null; try { session = factory.createSession(); ClientConsumer consumer = session.createConsumer(perfParams.getQueueName()); session.start(); ClientMessage message = null; int count = 0; do { message = consumer.receive(3000); if (message != null) { message.acknowledge(); count++; } } while (message != null); PerfBase.log.info("Drained " + count + " messages"); } finally { if (session != null) { session.close(); } } }
public void testSSL() throws Exception { String text = RandomUtil.randomString(); tc.getParams().put(TransportConstants.SSL_ENABLED_PROP_NAME, true); tc.getParams() .put(TransportConstants.KEYSTORE_PATH_PROP_NAME, TransportConstants.DEFAULT_KEYSTORE_PATH); tc.getParams() .put( TransportConstants.KEYSTORE_PASSWORD_PROP_NAME, TransportConstants.DEFAULT_KEYSTORE_PASSWORD); ServerLocator locator = addServerLocator(HornetQClient.createServerLocatorWithoutHA(tc)); ClientSessionFactory sf = createSessionFactory(locator); ClientSession session = sf.createSession(false, true, true); session.createQueue(CoreClientOverSSLTest.QUEUE, CoreClientOverSSLTest.QUEUE, false); ClientProducer producer = session.createProducer(CoreClientOverSSLTest.QUEUE); ClientMessage message = createTextMessage(session, text); producer.send(message); ClientConsumer consumer = session.createConsumer(CoreClientOverSSLTest.QUEUE); session.start(); Message m = consumer.receive(1000); Assert.assertNotNull(m); Assert.assertEquals(text, m.getBodyBuffer().readString()); }
/** * Setup the activation * * @throws Exception Thrown if an error occurs */ protected synchronized void setup() throws Exception { HornetQRALogger.LOGGER.debug("Setting up " + spec); setupCF(); setupDestination(); for (int i = 0; i < spec.getMaxSession(); i++) { ClientSession session = null; try { ClientSessionFactory cf = factory.getServerLocator().createSessionFactory(); session = setupSession(cf); HornetQMessageHandler handler = new HornetQMessageHandler(this, ra.getTM(), (ClientSessionInternal) session, cf, i); handler.setup(); session.start(); handlers.add(handler); } catch (Exception e) { if (session != null) { session.close(); } throw e; } } resourceRecovery = ra.getRecoveryManager().register(factory, spec.getUser(), spec.getPassword()); HornetQRALogger.LOGGER.debug("Setup complete " + this); }
// An exception during delivery shouldn't make the message disappear @Test public void testExceptionWhileDelivering() throws Exception { queue = server.createQueue(QUEUE, QUEUE, null, true, false); HangInterceptor hangInt = new HangInterceptor(); try { locator.addInterceptor(hangInt); ClientSessionFactory factory = locator.createSessionFactory(); ClientSession session = factory.createSession(false, false, false); ClientProducer producer = session.createProducer(QUEUE); ClientConsumer consumer = session.createConsumer(QUEUE); producer.send(session.createMessage(true)); session.commit(); hangInt.close(); session.start(); Assert.assertTrue(hangInt.reusableLatch.await(10, TimeUnit.SECONDS)); hangInt.pendingException = new HornetQException(); hangInt.open(); session.close(); session = factory.createSession(false, false); session.start(); consumer = session.createConsumer(QUEUE); ClientMessage msg = consumer.receive(5000); Assert.assertNotNull(msg); msg.acknowledge(); session.commit(); } finally { hangInt.open(); } }
@Test public void testLargeMessageCompression2() throws Exception { final int messageSize = (int) (3.5 * HornetQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE); HornetQServer server = createServer(true, isNetty()); server.start(); ClientSessionFactory sf = createSessionFactory(locator); ClientSession session = addClientSession(sf.createSession(false, false, false)); session.createTemporaryQueue(ADDRESS, ADDRESS); ClientProducer producer = session.createProducer(ADDRESS); Message clientFile = createLargeClientMessageStreaming(session, messageSize, true); producer.send(clientFile); session.commit(); session.start(); ClientConsumer consumer = session.createConsumer(ADDRESS); ClientMessage msg1 = consumer.receive(1000); Assert.assertNotNull(msg1); String testDir = getTestDir(); File testFile = new File(testDir, "async_large_message"); FileOutputStream output = new FileOutputStream(testFile); msg1.setOutputStream(output); msg1.waitOutputStreamCompletion(0); msg1.acknowledge(); output.close(); session.commit(); consumer.close(); session.close(); // verify FileInputStream input = new FileInputStream(testFile); for (int i = 0; i < messageSize; i++) { byte b = (byte) input.read(); assertEquals("position = " + i, getSamplebyte(i), b); } input.close(); testFile.delete(); validateNoFilesOnLargeDir(); }
public MessageProducer(String addressName, ClientSessionFactory factory) { this.addressName = addressName; try { producerSession = factory.createSession(true, true); producer = producerSession.createProducer(addressName); producerSession.start(); } catch (HornetQException ex) { Logger.getLogger(MessageProducer.class.getName()).log(Level.SEVERE, null, ex); } }
@Test public void testDelayedRedeliveryDefaultOnRollback() throws Exception { ClientSessionFactory sessionFactory = createSessionFactory(locator); ClientSession session = sessionFactory.createSession(false, false, false); session.createQueue(qName, qName, null, true); session.close(); ClientSession session1 = sessionFactory.createSession(false, true, true); ClientProducer producer = session1.createProducer(qName); final int NUM_MESSAGES = 5; for (int i = 0; i < NUM_MESSAGES; i++) { ClientMessage tm = createDurableMessage(session1, "message" + i); producer.send(tm); } session1.close(); ClientSession session2 = sessionFactory.createSession(false, false, false); ClientConsumer consumer2 = session2.createConsumer(qName); session2.start(); for (int i = 0; i < NUM_MESSAGES; i++) { ClientMessage tm = consumer2.receive(500); Assert.assertNotNull(tm); Assert.assertEquals("message" + i, tm.getBodyBuffer().readString()); } // Now rollback long now = System.currentTimeMillis(); session2.rollback(); // This should redeliver with a delayed redelivery for (int i = 0; i < NUM_MESSAGES; i++) { ClientMessage tm = consumer2.receive(DelayedMessageTest.DELAY + 1000); Assert.assertNotNull(tm); long time = System.currentTimeMillis(); Assert.assertTrue(time - now >= DelayedMessageTest.DELAY); // Hudson can introduce a large degree of indeterminism Assert.assertTrue( time - now + ">" + (DelayedMessageTest.DELAY + 1000), time - now < DelayedMessageTest.DELAY + 1000); } session2.commit(); session2.close(); }
@Test public void testSendPacketsWithFailure() throws Exception { final int nMsg = 100; final int stop = 37; setupServer(true, TestInterceptor.class.getName()); manager = liveServer.getReplicationManager(); waitForComponent(manager); ClientSessionFactory sf = createSessionFactory(locator); final ClientSession session = sf.createSession(); final ClientSession session2 = sf.createSession(); session.createQueue(ADDRESS, ADDRESS, null, true); final ClientProducer producer = session.createProducer(ADDRESS); session.start(); session2.start(); try { final ClientConsumer consumer = session2.createConsumer(ADDRESS); for (int i = 0; i < nMsg; i++) { ClientMessage message = session.createMessage(true); setBody(i, message); message.putIntProperty("counter", i); producer.send(message); if (i == stop) { // Now we start intercepting the communication with the backup TestInterceptor.value.set(false); } ClientMessage msgRcvd = consumer.receive(1000); Assert.assertNotNull("Message should exist!", msgRcvd); assertMessageBody(i, msgRcvd); Assert.assertEquals(i, msgRcvd.getIntProperty("counter").intValue()); msgRcvd.acknowledge(); } } finally { TestInterceptor.value.set(false); if (!session.isClosed()) session.close(); if (!session2.isClosed()) session2.close(); } }
private void init() throws YamcsApiException { try { String username = null; String password = null; if (invm) { locator = HornetQClient.createServerLocatorWithoutHA( new TransportConfiguration(Protocol.IN_VM_FACTORY)); sessionFactory = locator.createSessionFactory(); username = hornetqInvmUser; password = hornetqInvmPass; } else { if (ycd.host != null) { Map<String, Object> tcpConfig = new HashMap<String, Object>(); tcpConfig.put(TransportConstants.HOST_PROP_NAME, ycd.host); tcpConfig.put(TransportConstants.PORT_PROP_NAME, ycd.port); locator = HornetQClient.createServerLocatorWithoutHA( new TransportConfiguration(NettyConnectorFactory.class.getName(), tcpConfig)); sessionFactory = locator.createSessionFactory(); } else { locator = HornetQClient.createServerLocatorWithoutHA( new TransportConfiguration(NettyConnectorFactory.class.getName())); sessionFactory = locator.createSessionFactory(); } username = ycd.username; password = ycd.password; } // All sessions are authenticated, a null username translates to // guest auth and authz (if allowed by server) session = sessionFactory.createSession(username, password, false, true, true, preAcknowledge, 1); session.start(); } catch (HornetQException e) { // Pass specific HornetQExceptions as our cause, helps identify // permissions problems try { close(); } catch (HornetQException e1) { } throw new YamcsApiException(e.getMessage(), e); } catch (Exception e) { // Pass Exception's cause as our cause. System.out.println(e); // close everything try { close(); } catch (HornetQException e1) { } throw new YamcsApiException(e.getMessage(), e.getCause()); } }
@Test public void testSessionClosedOnRemotingConnectionFailure() throws Exception { ClientSession session = addClientSession(sf.createSession()); session.createQueue("fooaddress", "fooqueue"); ClientProducer prod = session.createProducer("fooaddress"); ClientConsumer cons = session.createConsumer("fooqueue"); session.start(); prod.send(session.createMessage(false)); Assert.assertNotNull(cons.receive()); // Now fail the underlying connection RemotingConnection connection = ((ClientSessionInternal) session).getConnection(); connection.fail(new HornetQNotConnectedException()); Assert.assertTrue(session.isClosed()); Assert.assertTrue(prod.isClosed()); Assert.assertTrue(cons.isClosed()); // Now try and use the producer try { prod.send(session.createMessage(false)); Assert.fail("Should throw exception"); } catch (HornetQObjectClosedException oce) { // ok } catch (HornetQException e) { fail("Invalid Exception type:" + e.getType()); } try { cons.receive(); Assert.fail("Should throw exception"); } catch (HornetQObjectClosedException oce) { // ok } catch (HornetQException e) { fail("Invalid Exception type:" + e.getType()); } session.close(); }
public void testAcksWithSmallSendWindow() 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 = 10000; for (int i = 0; i < numMessages; i++) { ClientMessage message = createTextMessage(session, "m" + i); producer.send(message); } session.close(); sf.close(); final CountDownLatch latch = new CountDownLatch(numMessages); server .getRemotingService() .addInterceptor( new Interceptor() { public boolean intercept(final Packet packet, final RemotingConnection connection) throws HornetQException { if (packet.getType() == PacketImpl.SESS_ACKNOWLEDGE) { latch.countDown(); } return true; } }); ServerLocator locator = createInVMNonHALocator(); locator.setConfirmationWindowSize(100); locator.setAckBatchSize(-1); ClientSessionFactory sfReceive = createSessionFactory(locator); ClientSession sessionRec = sfReceive.createSession(false, true, true); ClientConsumer consumer = sessionRec.createConsumer(QUEUE); consumer.setMessageHandler( new MessageHandler() { public void onMessage(final ClientMessage message) { try { message.acknowledge(); } catch (HornetQException e) { e.printStackTrace(); } } }); sessionRec.start(); Assert.assertTrue(latch.await(5, TimeUnit.SECONDS)); sessionRec.close(); locator.close(); }
@Test public void testMultipleMessagesAfterRecovery() throws Exception { Xid xid = new XidImpl("bq1".getBytes(), 4, "gtid1".getBytes()); ClientProducer producer = clientSessionXa.createProducer(address); SimpleString messageId1 = new SimpleString("SMID1"); SimpleString messageId2 = new SimpleString("SMID2"); clientSessionXa.start(xid, XAResource.TMNOFLAGS); ClientMessage m1 = createTextMessage(clientSession, "m1"); m1.putStringProperty(Message.HDR_LAST_VALUE_NAME, messageId1); ClientMessage m2 = createTextMessage(clientSession, "m2"); m2.putStringProperty(Message.HDR_LAST_VALUE_NAME, messageId2); ClientMessage m3 = createTextMessage(clientSession, "m3"); m3.putStringProperty(Message.HDR_LAST_VALUE_NAME, messageId1); ClientMessage m4 = createTextMessage(clientSession, "m4"); m4.putStringProperty(Message.HDR_LAST_VALUE_NAME, messageId2); producer.send(m1); producer.send(m2); producer.send(m3); producer.send(m4); clientSessionXa.end(xid, XAResource.TMSUCCESS); clientSessionXa.prepare(xid); clientSession.close(); clientSessionXa.close(); restartServer(); clientSessionXa.commit(xid, false); ClientConsumer consumer = clientSession.createConsumer(qName1); clientSession.start(); ClientMessage m = consumer.receive(1000); Assert.assertNotNull(m); m.acknowledge(); Assert.assertEquals(m.getBodyBuffer().readString(), "m3"); m = consumer.receive(1000); Assert.assertNotNull(m); m.acknowledge(); Assert.assertEquals(m.getBodyBuffer().readString(), "m4"); }
// https://jira.jboss.org/jira/browse/HORNETQ-111 // Test that, on rollback credits are released for messages cleared in the buffer public void testConsumerCreditsOnRollbackLargeMessages() throws Exception { locator.setConsumerWindowSize(10000); locator.setMinLargeMessageSize(1000); ClientSessionFactory sf = createSessionFactory(locator); ClientSession session = sf.createTransactedSession(); session.createQueue(QUEUE, QUEUE, null, false); ClientProducer producer = session.createProducer(QUEUE); final int numMessages = 100; final byte[] bytes = new byte[10000]; for (int i = 0; i < numMessages; i++) { ClientMessage message = session.createMessage(false); message.getBodyBuffer().writeBytes(bytes); message.putIntProperty("count", i); producer.send(message); } session.commit(); ClientConsumer consumer = session.createConsumer(QUEUE); session.start(); for (int i = 0; i < 110; i++) { ClientMessage message = consumer.receive(); int count = message.getIntProperty("count"); boolean redelivered = message.getDeliveryCount() > 1; if (count % 2 == 0 && !redelivered) { session.rollback(); } else { session.commit(); } } session.close(); }
/** * @throws Exception * @throws HornetQException */ private void verifyMessageOnServer(final int server, final int numberOfMessages) throws Exception, HornetQException { ServerLocator backupLocator = createInVMLocator(server); ClientSessionFactory factorybkp = addSessionFactory(createSessionFactory(backupLocator)); ClientSession sessionbkp = factorybkp.createSession(false, false); sessionbkp.start(); ClientConsumer consumerbkp = sessionbkp.createConsumer(ADDRESS); for (int i = 0; i < numberOfMessages; i++) { ClientMessage msg = consumerbkp.receive(1000); assertNotNull(msg); msg.acknowledge(); sessionbkp.commit(); } sessionbkp.close(); factorybkp.close(); backupLocator.close(); }
public void testListPreparedTransactionDetails() throws Exception { SimpleString atestq = new SimpleString("BasicXaTestq"); Xid xid = newXID(); ServerLocator locator = HornetQClient.createServerLocatorWithoutHA( new TransportConfiguration(UnitTestCase.INVM_CONNECTOR_FACTORY)); ClientSessionFactory csf = locator.createSessionFactory(); ClientSession clientSession = csf.createSession(true, false, false); clientSession.createQueue(atestq, atestq, null, true); ClientMessage m1 = createTextMessage(clientSession, ""); ClientMessage m2 = createTextMessage(clientSession, ""); ClientMessage m3 = createTextMessage(clientSession, ""); ClientMessage m4 = createTextMessage(clientSession, ""); m1.putStringProperty("m1", "m1"); m2.putStringProperty("m2", "m2"); m3.putStringProperty("m3", "m3"); m4.putStringProperty("m4", "m4"); ClientProducer clientProducer = clientSession.createProducer(atestq); clientSession.start(xid, XAResource.TMNOFLAGS); clientProducer.send(m1); clientProducer.send(m2); clientProducer.send(m3); clientProducer.send(m4); clientSession.end(xid, XAResource.TMSUCCESS); clientSession.prepare(xid); HornetQServerControl serverControl = createManagementControl(); JSONArray jsonArray = new JSONArray(serverControl.listProducersInfoAsJSON()); assertEquals(1, jsonArray.length()); assertEquals(4, ((JSONObject) jsonArray.get(0)).getInt("msgSent")); clientSession.close(); locator.close(); String txDetails = serverControl.listPreparedTransactionDetailsAsJSON(); Assert.assertTrue(txDetails.matches(".*m1.*")); Assert.assertTrue(txDetails.matches(".*m2.*")); Assert.assertTrue(txDetails.matches(".*m3.*")); Assert.assertTrue(txDetails.matches(".*m4.*")); }
public void start() throws Exception { serverLocator = HornetQClient.createServerLocatorWithoutHA( new TransportConfiguration(NettyConnectorFactory.class.getName())); sf = serverLocator.createSessionFactory(); clientSession = sf.createSession( HornetQDefaultConfiguration.DEFAULT_CLUSTER_USER, HornetQDefaultConfiguration.DEFAULT_CLUSTER_PASSWORD, false, true, true, false, 1); requestor = new ClientRequestor(clientSession, HornetQDefaultConfiguration.DEFAULT_MANAGEMENT_ADDRESS); clientSession.start(); }
private ClientSession sendAndConsume(final ClientSessionFactory sf, final boolean createQueue) throws Exception { ClientSession session = sf.createSession(false, true, true); if (createQueue) { session.createQueue(FailoverTestBase.ADDRESS, FailoverTestBase.ADDRESS, null, true); } ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS); final int numMessages = 1000; for (int i = 0; i < numMessages; i++) { ClientMessage message = session.createMessage( HornetQTextMessage.TYPE, false, 0, System.currentTimeMillis(), (byte) 1); message.putIntProperty(new SimpleString("count"), i); message.getBodyBuffer().writeString("aardvarks"); producer.send(message); } ClientConsumer consumer = session.createConsumer(FailoverTestBase.ADDRESS); session.start(); for (int i = 0; i < numMessages; i++) { ClientMessage message2 = consumer.receive(); Assert.assertEquals("aardvarks", message2.getBodyBuffer().readString()); Assert.assertEquals(i, message2.getObjectProperty(new SimpleString("count"))); message2.acknowledge(); } ClientMessage message3 = consumer.receiveImmediate(); consumer.close(); Assert.assertNull(message3); return session; }
public void testReuseConsumersFlowControl0() throws Throwable { shareConnectionFactory = true; openConsumerOnEveryLoop = false; numberOfProducers = 1; numberOfConsumers = 1; sharedLocator = createInVMNonHALocator(); sharedLocator.setConsumerWindowSize(0); sharedSf = createSessionFactory(sharedLocator); try { internalMultipleConsumers(); } catch (Throwable e) { TestConsumer tstConsumer = consumers.get(0); System.out.println("first retry: " + tstConsumer.consumer.receive(1000)); System.out.println(pagedServerQueue.debug()); pagedServerQueue.forceDelivery(); System.out.println("Second retry: " + tstConsumer.consumer.receive(1000)); System.out.println(pagedServerQueue.debug()); tstConsumer.session.commit(); System.out.println("Third retry:" + tstConsumer.consumer.receive(1000)); tstConsumer.close(); ClientSession session = sharedSf.createSession(); session.start(); ClientConsumer consumer = session.createConsumer(ADDRESS); pagedServerQueue.forceDelivery(); System.out.println("Fourth retry: " + consumer.receive(1000)); System.out.println(pagedServerQueue.debug()); throw e; } }
@Test public void testLargeMessageCompression() throws Exception { final int messageSize = (int) (3.5 * HornetQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE); HornetQServer server = createServer(true, isNetty()); server.start(); ClientSessionFactory sf = createSessionFactory(locator); ClientSession session = addClientSession(sf.createSession(false, false, false)); session.createTemporaryQueue(ADDRESS, ADDRESS); ClientProducer producer = session.createProducer(ADDRESS); Message clientFile = createLargeClientMessageStreaming(session, messageSize, true); producer.send(clientFile); session.commit(); session.start(); ClientConsumer consumer = session.createConsumer(ADDRESS); ClientMessage msg1 = consumer.receive(1000); Assert.assertNotNull(msg1); for (int i = 0; i < messageSize; i++) { byte b = msg1.getBodyBuffer().readByte(); assertEquals("position = " + i, getSamplebyte(i), b); } msg1.acknowledge(); session.commit(); consumer.close(); session.close(); validateNoFilesOnLargeDir(); }
@Override @Before public void setUp() throws Exception { super.setUp(); Configuration conf = createBasicConfig(); conf.setSecurityEnabled(false); conf.setJMXManagementEnabled(true); conf.getAcceptorConfigurations() .add(new TransportConfiguration(InVMAcceptorFactory.class.getName())); server = createServer(false, conf, mbeanServer); server.start(); locator = createInVMNonHALocator(); locator.setBlockOnNonDurableSend(true); locator.setBlockOnNonDurableSend(true); sf = createSessionFactory(locator); session = sf.createSession(false, true, false); session.start(); addClientSession(session); }
protected void runListener() { ClientSession session = null; try { init(perfParams.isSessionTransacted(), perfParams.getQueueName()); session = factory.createSession( !perfParams.isSessionTransacted(), !perfParams.isSessionTransacted()); if (perfParams.isDrainQueue()) { drainQueue(); } ClientConsumer consumer = session.createConsumer(perfParams.getQueueName()); session.start(); PerfBase.log.info("READY!!!"); CountDownLatch countDownLatch = new CountDownLatch(1); consumer.setMessageHandler(new PerfListener(session, countDownLatch, perfParams)); countDownLatch.await(); long end = System.currentTimeMillis(); // start was set on the first received message displayAverage(perfParams.getNoOfMessagesToSend(), start, end); } catch (Exception e) { e.printStackTrace(); } finally { if (factory != null) { try { factory.close(); } catch (Exception e) { e.printStackTrace(); } } } }