protected Connection startJmsConnection(String url) { try { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); connectionFactory.setAlwaysSyncSend(false); Connection connect = connectionFactory.createConnection(); connect.start(); return connect; } catch (JMSException e) { throw new AssertionError("Failed to establish the JMS-Connection!", e); } } }
protected Connection startJmsConnection(String url) { try { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); connectionFactory.setAlwaysSyncSend(false); Connection connect = connectionFactory.createConnection(); connect.start(); return connect; } catch (JMSException e) { throw new AssertionError("Failed to establish the JMS-Connection!", e); } } }
public synchronized ConnectionFactory configureCachingConnectionFactory(ActiveMQConnectionFactory connectionFactory) { // for non-persistent messages setting "AlwaysSyncSend" to true makes it slow, but ensures that a producer is immediately informed // about the memory issues on broker (is blocked or gets exception depending on <systemUsage> config) // see more info http://activemq.apache.org/producer-flow-control.html connectionFactory.setAlwaysSyncSend(false); // disable timestamps by default so that ttl of messages is not checked connectionFactory.setDisableTimeStampsByDefault(true); CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(connectionFactory); cachingConnectionFactory.setCacheConsumers(true); cachingConnectionFactory.setCacheProducers(true); return cachingConnectionFactory; }
@Test public void testSimpleSendReceive() throws Exception { factory.setAlwaysSyncSend(true); flowControlConnection = (ActiveMQConnection) factory.createConnection(); flowControlConnection.start(); Session session = flowControlConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(queueA); // Test sending to Queue B it should not block. CountDownLatch pubishDoneToQeueuA = asyncSendTo(queueA, "Message 1"); assertTrue(pubishDoneToQeueuA.await(2, TimeUnit.SECONDS)); TextMessage msg = (TextMessage) consumer.receive(); assertEquals("Message 1", msg.getText()); msg.acknowledge(); pubishDoneToQeueuA = asyncSendTo(queueA, "Message 2"); assertTrue(pubishDoneToQeueuA.await(2, TimeUnit.SECONDS)); msg = (TextMessage) consumer.receive(); assertEquals("Message 2", msg.getText()); msg.acknowledge(); consumer.close(); }
@Test public void test2ndPublisherWithSyncSendConnectionThatIsBlocked() throws Exception { factory.setAlwaysSyncSend(true); flowControlConnection = (ActiveMQConnection) factory.createConnection(); flowControlConnection.start(); Session session = flowControlConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(queueB); // Test sending to Queue A // 1st send should not block. But the rest will. fillQueue(queueA); // Test sending to Queue B it should not block. CountDownLatch pubishDoneToQeueuB = asyncSendTo(queueB, "Message 1"); assertTrue(pubishDoneToQeueuB.await(2, TimeUnit.SECONDS)); TextMessage msg = (TextMessage) consumer.receive(); assertEquals("Message 1", msg.getText()); msg.acknowledge(); pubishDoneToQeueuB = asyncSendTo(queueB, "Message 2"); assertTrue(pubishDoneToQeueuB.await(2, TimeUnit.SECONDS)); msg = (TextMessage) consumer.receive(); assertEquals("Message 2", msg.getText()); msg.acknowledge(); consumer.close(); }