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"); }
/** * 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); }
@Override public void run() { try { if (shareConnectionFactory) { session = sharedSf.createSession(false, false); } else { locator = createInVMNonHALocator(); sf = createSessionFactory(locator); session = sf.createSession(false, false); } ClientProducer prod = session.createProducer(MultipleConsumersPageStressTest.ADDRESS); int count = 0; while (enabled()) { int numberOfMessages = getNumberOfMessages(); for (int i = 0; i < numberOfMessages; i++) { ClientMessage msg = session.createMessage(true); msg.putStringProperty("Test", "This is a simple test"); msg.putIntProperty("count", count++); prod.send(msg); } messagesAvailable.addAndGet(numberOfMessages); session.commit(); } } catch (Throwable e) { exceptionHappened(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 testProducerWithSmallWindowSizeAndLargeMessage() throws Exception { final CountDownLatch latch = new CountDownLatch(1); server .getRemotingService() .addInterceptor( new Interceptor() { public boolean intercept(final Packet packet, final RemotingConnection connection) throws HornetQException { if (packet.getType() == PacketImpl.SESS_SEND) { latch.countDown(); } return true; } }); ServerLocator locator = createInVMNonHALocator(); locator.setConfirmationWindowSize(100); ClientSessionFactory cf = locator.createSessionFactory(); ClientSession session = cf.createSession(false, true, true); ClientProducer producer = session.createProducer(QUEUE); ClientMessage message = session.createMessage(true); byte[] body = new byte[1000]; message.getBodyBuffer().writeBytes(body); producer.send(message); Assert.assertTrue(latch.await(5, TimeUnit.SECONDS)); session.close(); locator.close(); }
// 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(); }
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()); }
@Test public void testXACommit() throws Exception { HornetQResourceAdapter qResourceAdapter = newResourceAdapter(); MyBootstrapContext ctx = new MyBootstrapContext(); qResourceAdapter.start(ctx); HornetQActivationSpec spec = new HornetQActivationSpec(); spec.setResourceAdapter(qResourceAdapter); spec.setUseJNDI(false); spec.setDestinationType("javax.jms.Queue"); spec.setDestination(MDBQUEUE); qResourceAdapter.setConnectorClassName(INVM_CONNECTOR_FACTORY); CountDownLatch latch = new CountDownLatch(1); XADummyEndpoint endpoint = new XADummyEndpoint(latch); DummyMessageEndpointFactory endpointFactory = new DummyMessageEndpointFactory(endpoint, true); qResourceAdapter.endpointActivation(endpointFactory, spec); ClientSession session = locator.createSessionFactory().createSession(); ClientProducer clientProducer = session.createProducer(MDBQUEUEPREFIXED); ClientMessage message = session.createMessage(true); message.getBodyBuffer().writeString("teststring"); clientProducer.send(message); session.close(); latch.await(5, TimeUnit.SECONDS); assertNotNull(endpoint.lastMessage); assertEquals(endpoint.lastMessage.getCoreMessage().getBodyBuffer().readString(), "teststring"); endpoint.prepare(); endpoint.commit(); qResourceAdapter.endpointDeactivation(endpointFactory, spec); qResourceAdapter.stop(); }
@Test public void testSimpleMessageReceivedOnQueueManyMessages() throws Exception { HornetQResourceAdapter qResourceAdapter = newResourceAdapter(); MyBootstrapContext ctx = new MyBootstrapContext(); qResourceAdapter.start(ctx); HornetQActivationSpec spec = new HornetQActivationSpec(); spec.setResourceAdapter(qResourceAdapter); spec.setUseJNDI(false); spec.setDestinationType("javax.jms.Queue"); spec.setDestination(MDBQUEUE); qResourceAdapter.setConnectorClassName(INVM_CONNECTOR_FACTORY); CountDownLatch latch = new CountDownLatch(15); MultipleEndpoints endpoint = new MultipleEndpoints(latch, false); DummyMessageEndpointFactory endpointFactory = new DummyMessageEndpointFactory(endpoint, false); qResourceAdapter.endpointActivation(endpointFactory, spec); ClientSession session = locator.createSessionFactory().createSession(); ClientProducer clientProducer = session.createProducer(MDBQUEUEPREFIXED); for (int i = 0; i < 15; i++) { ClientMessage message = session.createMessage(true); message.getBodyBuffer().writeString("teststring" + i); clientProducer.send(message); } session.close(); latch.await(5, TimeUnit.SECONDS); qResourceAdapter.endpointDeactivation(endpointFactory, spec); qResourceAdapter.stop(); }
/** * This would force a journal duplication on bindings even with the scenario that generated fixed, * the server shouldn't hold of from starting * * @throws Exception */ @Test public void testForceDuplicationOnBindings() throws Exception { queue = server.createQueue(QUEUE, QUEUE, null, true, false); ClientSessionFactory factory = locator.createSessionFactory(); ClientSession session = factory.createSession(false, false, false); ClientProducer producer = session.createProducer(QUEUE); producer.send(session.createMessage(true)); session.commit(); long queueID = server.getStorageManager().generateUniqueID(); long txID = server.getStorageManager().generateUniqueID(); // Forcing a situation where the server would unexpectedly create a duplicated queue. The server // should still start normally LocalQueueBinding newBinding = new LocalQueueBinding( QUEUE, new QueueImpl(queueID, QUEUE, QUEUE, null, true, false, null, null, null, null, null), server.getNodeID()); server.getStorageManager().addQueueBinding(txID, newBinding); server.getStorageManager().commitBindings(txID); server.stop(); // a duplicate binding would impede the server from starting server.start(); waitForServer(server); server.stop(); }
public void sendMessages(final int start, final int end) throws Exception { ClientSession session = null; try { 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 } finally { session.close(); } }
/** * Set up the {@link ClientSession} to throw an exception when {@link * ClientSession#createProducer(String)} is called. Make sure, we throw up our hands saying "I am * not dealing with this". * * @throws Exception */ @Test(expected = RuntimeException.class) public void eventSinkShouldThrowExceptionWhenProducerCreationFailsInConstructor() throws Exception { doThrow(new HornetQException()).when(mockClientSession.createProducer(anyString())); createEventSink(mockSessionFactory); fail("Runtime exception should have been thrown."); }
@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 testDurableSubscription() throws Exception { HornetQResourceAdapter qResourceAdapter = newResourceAdapter(); MyBootstrapContext ctx = new MyBootstrapContext(); qResourceAdapter.start(ctx); HornetQActivationSpec spec = new HornetQActivationSpec(); spec.setResourceAdapter(qResourceAdapter); spec.setUseJNDI(false); spec.setDestinationType("javax.jms.Topic"); spec.setDestination("mdbTopic"); spec.setSubscriptionDurability("Durable"); spec.setSubscriptionName("durable-mdb"); spec.setClientID("id-1"); qResourceAdapter.setConnectorClassName(INVM_CONNECTOR_FACTORY); CountDownLatch latch = new CountDownLatch(1); DummyMessageEndpoint endpoint = new DummyMessageEndpoint(latch); DummyMessageEndpointFactory endpointFactory = new DummyMessageEndpointFactory(endpoint, false); qResourceAdapter.endpointActivation(endpointFactory, spec); ClientSession session = locator.createSessionFactory().createSession(); ClientProducer clientProducer = session.createProducer("jms.topic.mdbTopic"); ClientMessage message = session.createMessage(true); message.getBodyBuffer().writeString("1"); clientProducer.send(message); latch.await(5, TimeUnit.SECONDS); assertNotNull(endpoint.lastMessage); assertEquals(endpoint.lastMessage.getCoreMessage().getBodyBuffer().readString(), "1"); qResourceAdapter.endpointDeactivation(endpointFactory, spec); message = session.createMessage(true); message.getBodyBuffer().writeString("2"); clientProducer.send(message); latch = new CountDownLatch(1); endpoint = new DummyMessageEndpoint(latch); endpointFactory = new DummyMessageEndpointFactory(endpoint, false); qResourceAdapter.endpointActivation(endpointFactory, spec); latch.await(5, TimeUnit.SECONDS); assertNotNull(endpoint.lastMessage); assertEquals(endpoint.lastMessage.getCoreMessage().getBodyBuffer().readString(), "2"); latch = new CountDownLatch(1); endpoint.reset(latch); message = session.createMessage(true); message.getBodyBuffer().writeString("3"); clientProducer.send(message); latch.await(5, TimeUnit.SECONDS); assertNotNull(endpoint.lastMessage); assertEquals(endpoint.lastMessage.getCoreMessage().getBodyBuffer().readString(), "3"); qResourceAdapter.endpointDeactivation(endpointFactory, spec); qResourceAdapter.stop(); }
@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 testAutoFailbackThenFailover() throws Exception { createSessionFactory(); ClientSession session = sendAndConsume(sf, true); CountDownSessionFailureListener listener = new CountDownSessionFailureListener(); session.addFailureListener(listener); liveServer.crash(session); ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS); ClientMessage message = session.createMessage(true); setBody(0, message); producer.send(message); session.removeFailureListener(listener); listener = new CountDownSessionFailureListener(); session.addFailureListener(listener); log.info("restarting live node now"); liveServer.start(); assertTrue("expected a session failure", listener.getLatch().await(5, TimeUnit.SECONDS)); message = session.createMessage(true); setBody(1, message); producer.send(message); session.removeFailureListener(listener); listener = new CountDownSessionFailureListener(); session.addFailureListener(listener); waitForBackup(sf, 10); liveServer.crash(); assertTrue("expected a session failure", listener.getLatch().await(5, TimeUnit.SECONDS)); session.close(); wrapUpSessionFactory(); }
@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(); }
@Override public void run() { ClientSession session = null; ClientSession sessionSlow = null; latchReady.countDown(); try { UnitTestCase.waitForLatch(latchStart); session = sf.createSession(true, true); sessionSlow = sf.createSession(false, false); ClientProducer prod = session.createProducer(CompactingStressTest.AD2); ClientProducer slowProd = sessionSlow.createProducer(CompactingStressTest.AD1); for (int i = 0; i < NUMBER_OF_FAST_MESSAGES; i++) { if (i % SLOW_INTERVAL == 0) { if (numberOfMessages.incrementAndGet() % 5 == 0) { sessionSlow.commit(); } slowProd.send(session.createMessage(true)); } ClientMessage msg = session.createMessage(true); prod.send(msg); } sessionSlow.commit(); } catch (Throwable e) { this.e = e; } finally { try { session.close(); } catch (Throwable e) { this.e = e; } try { sessionSlow.close(); } catch (Throwable e) { this.e = e; } } }
@Before public void init() throws Exception { this.factory = new EventFactory(mockPrincipalProvider); this.principal = TestUtil.createOwnerPrincipal(); eventFilter = new EventFilter(new CandlepinCommonTestConfig()); when(mockPrincipalProvider.get()).thenReturn(this.principal); when(mockSessionFactory.createTransactedSession()).thenReturn(mockClientSession); when(mockClientSession.createProducer(anyString())).thenReturn(mockClientProducer); when(mockClientSession.createMessage(anyBoolean())).thenReturn(mockClientMessage); when(mockClientMessage.getBodyBuffer()).thenReturn(HornetQBuffers.fixedBuffer(2000)); when(mockSessionFactory.getServerLocator()).thenReturn(mockLocator); this.mapper = spy(new ObjectMapper()); this.eventSinkImpl = createEventSink(mockSessionFactory); o = new Owner("test owner"); }
// 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(); }
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.*")); }
// 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(); } }
// https://issues.jboss.org/browse/JBPAPP-8017 @Test public void testNonDurableSubscriptionDeleteAfterCrash() throws Exception { HornetQResourceAdapter qResourceAdapter = newResourceAdapter(); qResourceAdapter.setTransactionManagerLocatorClass(""); qResourceAdapter.setTransactionManagerLocatorMethod(""); MyBootstrapContext ctx = new MyBootstrapContext(); qResourceAdapter.start(ctx); HornetQActivationSpec spec = new HornetQActivationSpec(); spec.setResourceAdapter(qResourceAdapter); spec.setUseJNDI(false); spec.setDestinationType("javax.jms.Topic"); spec.setDestination("mdbTopic"); qResourceAdapter.setConnectorClassName(INVM_CONNECTOR_FACTORY); CountDownLatch latch = new CountDownLatch(1); DummyMessageEndpoint endpoint = new DummyMessageEndpoint(latch); DummyMessageEndpointFactory endpointFactory = new DummyMessageEndpointFactory(endpoint, false); qResourceAdapter.endpointActivation(endpointFactory, spec); ClientSession session = locator.createSessionFactory().createSession(); ClientProducer clientProducer = session.createProducer("jms.topic.mdbTopic"); ClientMessage message = session.createMessage(true); message.getBodyBuffer().writeString("1"); clientProducer.send(message); latch.await(5, TimeUnit.SECONDS); assertNotNull(endpoint.lastMessage); assertEquals(endpoint.lastMessage.getCoreMessage().getBodyBuffer().readString(), "1"); HornetQActivation activation = lookupActivation(qResourceAdapter); SimpleString tempQueueName = activation.getTopicTemporaryQueue(); QueueQuery query = session.queueQuery(tempQueueName); assertTrue(query.isExists()); // this should be enough to simulate the crash qResourceAdapter.getDefaultHornetQConnectionFactory().close(); qResourceAdapter.stop(); query = session.queueQuery(tempQueueName); assertFalse(query.isExists()); }
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; }
private void sendMessages() throws Exception { ClientSession session = sf.createSession(true, true); session.createQueue(ADDRESS, ADDRESS, null, true); ClientProducer producer = session.createProducer(ADDRESS); for (int i = 0; i < numMessages; i++) { ClientMessage message = session.createMessage(true); setBody(i, message); message.putIntProperty("int", i); message.putShortProperty("short", (short) i); message.putByteProperty("byte", (byte) i); message.putFloatProperty("float", floatValue(i)); message.putStringProperty(SIMPLE_STRING_KEY, new SimpleString(Integer.toString(i))); message.putBytesProperty("byte[]", byteArray(i)); message.putObjectProperty("null-value", null); producer.send(message); } session.commit(); }
@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(); }
@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(); } }
@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"); }