use of javax.jms.QueueReceiver in project rabbitmq-jms-client by rabbitmq.
the class AsyncConsumerOnMessageHangIT method testSendAndAsyncReceiveTextMessageWithHang.
/**
* Basic send and receive with a Consumer. Uses serial send followed by async receive.
* @throws Exception if test error.
*/
@Test
public void testSendAndAsyncReceiveTextMessageWithHang() throws Exception {
try {
queueConn.start();
QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
Queue queue = queueSession.createQueue(QUEUE_NAME);
QueueSender queueSender = queueSession.createSender(queue);
queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
TextMessage message = queueSession.createTextMessage(MESSAGE);
queueSender.send(message);
} finally {
reconnect();
}
queueConn.start();
QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
Queue queue = queueSession.createQueue(QUEUE_NAME);
QueueReceiver queueReceiver = queueSession.createReceiver(queue);
MessageListener listener = new MessageListener();
queueReceiver.setMessageListener(listener);
Thread.sleep(4000);
TextMessage message = (TextMessage) listener.getLastMessage();
assertEquals(MESSAGE, message.getText());
assertEquals(1, listener.getMessageCount());
assertTrue(listener.isSuccess());
assertTrue(listener.isInterrupted());
}
use of javax.jms.QueueReceiver in project rabbitmq-jms-client by rabbitmq.
the class IndividualAcknowledgeQueueMessageIT method messageTestBase.
private void messageTestBase(MessageTestType mtt) throws Exception {
int noToSend = 10;
int noToAck = 5;
try {
queueConn.start();
QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
Queue queue = queueSession.createQueue(QUEUE_NAME);
drainQueue(queueSession, queue);
QueueSender queueSender = queueSession.createSender(queue);
queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
for (int i = 0; i < noToSend; ++i) {
queueSender.send(mtt.gen(queueSession, (Serializable) queue));
}
} finally {
reconnect();
}
try {
queueConn.start();
QueueSession queueSession = queueConn.createQueueSession(false, RMQSession.CLIENT_INDIVIDUAL_ACKNOWLEDGE);
Queue queue = queueSession.createQueue(QUEUE_NAME);
QueueReceiver queueReceiver = queueSession.createReceiver(queue);
int i = 0;
for (; i < noToSend - noToAck; ++i) {
Message msg = queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
mtt.check(msg, (Serializable) queue);
}
for (; i < noToSend; ++i) {
Message msg = queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
mtt.check(msg, (Serializable) queue);
msg.acknowledge();
}
} finally // requeues unacknowledged messages
{
reconnect();
}
{
queueConn.start();
QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
Queue queue = queueSession.createQueue(QUEUE_NAME);
QueueReceiver queueReceiver = queueSession.createReceiver(queue);
for (int i = noToSend - noToAck; i < noToSend; ++i) {
Message msg = queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
mtt.check(msg, (Serializable) queue);
}
}
}
use of javax.jms.QueueReceiver in project rabbitmq-jms-client by rabbitmq.
the class MixedConsumerCheckIT method testSyncThenAsyncReceive.
/**
* Receive on another thread. Try to set a Listener (asynchronous receive).
* @throws javax.jms.IllegalStateException if test succeeds.
*/
@Test
public void testSyncThenAsyncReceive() throws Exception {
assertThrows(javax.jms.IllegalStateException.class, () -> {
queueConn.start();
QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
Queue queue = queueSession.createQueue(QUEUE_NAME);
Queue queueTwo = queueSession.createQueue(QUEUE_NAME_TWO);
QueueReceiver queueReceiver = queueSession.createReceiver(queue);
final QueueReceiver queueReceiverTwo = queueSession.createReceiver(queueTwo);
new Thread() {
@Override
public void run() {
try {
// half a sec’
queueReceiverTwo.receive(500);
} catch (JMSException e) {
}
}
}.start();
// tenth of a sec’
Thread.sleep(100);
// should throw exception
queueReceiver.setMessageListener(new TestMessageListener());
});
}
use of javax.jms.QueueReceiver in project rabbitmq-jms-client by rabbitmq.
the class AbstractITQueue method drainQueue.
protected static void drainQueue(QueueSession session, Queue queue) throws Exception {
QueueReceiver receiver = session.createReceiver(queue);
int n = 0;
Message msg = receiver.receiveNoWait();
while (msg != null) {
++n;
msg = receiver.receiveNoWait();
}
if (n > 0) {
System.out.println(">> INFO >> Drained messages (n=" + n + ") from queue " + queue + " prior to test.");
}
}
use of javax.jms.QueueReceiver in project rabbitmq-jms-client by rabbitmq.
the class AsyncConsumerIT method testSendAndAsyncReceiveTextMessage.
/**
* Basic send and receive with a Consumer. Uses serial send followed by async receive.
* @throws Exception if test error.
*/
@Test
public void testSendAndAsyncReceiveTextMessage() throws Exception {
try {
queueConn.start();
QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
Queue queue = queueSession.createQueue(QUEUE_NAME);
QueueSender queueSender = queueSession.createSender(queue);
queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
TextMessage message = queueSession.createTextMessage(MESSAGE);
queueSender.send(message);
} finally {
reconnect();
}
queueConn.start();
QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
Queue queue = queueSession.createQueue(QUEUE_NAME);
QueueReceiver queueReceiver = queueSession.createReceiver(queue);
CountDownLatch latch = new CountDownLatch(1);
MessageListener listener = new MessageListener(latch);
queueReceiver.setMessageListener(listener);
latch.await(1000, TimeUnit.MILLISECONDS);
TextMessage message = (TextMessage) listener.getLastMessage();
assertEquals(MESSAGE, message.getText());
assertEquals(1, listener.getMessageCount());
assertTrue(listener.isSuccess());
}
Aggregations