use of javax.jms.MessageListener in project activemq-artemis by apache.
the class TempQueueMemoryTest method testLoadRequestReply.
public void testLoadRequestReply() throws Exception {
for (int i = 0; i < numConsumers; i++) {
serverSession.createConsumer(serverDestination).setMessageListener(new MessageListener() {
@Override
public void onMessage(Message msg) {
try {
Destination replyTo = msg.getJMSReplyTo();
MessageProducer producer = serverSession.createProducer(replyTo);
producer.send(replyTo, msg);
if (serverTransactional) {
serverSession.commit();
}
producer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
class Producer extends Thread {
private final int numToSend;
public Producer(int numToSend) {
this.numToSend = numToSend;
}
@Override
public void run() {
try {
Session session = clientConnection.createSession(clientTransactional, clientTransactional ? Session.SESSION_TRANSACTED : Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(serverDestination);
for (int i = 0; i < numToSend; i++) {
TemporaryQueue replyTo = session.createTemporaryQueue();
MessageConsumer consumer = session.createConsumer(replyTo);
Message msg = session.createMessage();
msg.setJMSReplyTo(replyTo);
producer.send(msg);
if (clientTransactional) {
session.commit();
}
consumer.receive();
if (clientTransactional) {
session.commit();
}
consumer.close();
if (deleteTempQueue) {
replyTo.delete();
} else {
// temp queue will be cleaned up on clientConnection.close
}
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}
Vector<Thread> threads = new Vector<>(numProducers);
for (int i = 0; i < numProducers; i++) {
threads.add(new Producer(messagesToSend / numProducers));
}
startAndJoinThreads(threads);
clientSession.close();
serverSession.close();
clientConnection.close();
serverConnection.close();
AdvisoryBroker ab = (AdvisoryBroker) broker.getBroker().getAdaptor(AdvisoryBroker.class);
// The server destination will be left
assertTrue(ab.getAdvisoryDestinations().size() == 1);
assertTrue("should be zero but is " + ab.getAdvisoryConsumers().size(), ab.getAdvisoryConsumers().size() == 0);
assertTrue("should be zero but is " + ab.getAdvisoryProducers().size(), ab.getAdvisoryProducers().size() == 0);
RegionBroker rb = (RegionBroker) broker.getBroker().getAdaptor(RegionBroker.class);
assertTrue(rb.getDestinationMap().size() >= 6);
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JMSTest method testMessageListener.
@Test
public void testMessageListener() throws Exception {
conn = createConnection();
Session sessionConsumer = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer cons = sessionConsumer.createConsumer(queue1);
final AtomicReference<Message> message = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
cons.setMessageListener(new MessageListener() {
@Override
public void onMessage(final Message m) {
message.set(m);
latch.countDown();
}
});
conn.start();
Session sessionProducer = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer prod = sessionProducer.createProducer(queue1);
prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
TextMessage m = sessionProducer.createTextMessage("one");
prod.send(m);
boolean gotMessage = latch.await(5000, MILLISECONDS);
ProxyAssertSupport.assertTrue(gotMessage);
TextMessage rm = (TextMessage) message.get();
ProxyAssertSupport.assertEquals("one", rm.getText());
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class MiscellaneousTest method testClosingConsumerFromMessageListenerAutoAck.
/**
* Test case for http://jira.jboss.org/jira/browse/JBMESSAGING-542
*/
@Test
public void testClosingConsumerFromMessageListenerAutoAck() throws Exception {
Connection c = null;
try {
c = createConnection();
Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer prod = s.createProducer(queue1);
Message m = s.createMessage();
prod.send(m);
} finally {
if (c != null) {
c.close();
}
}
final Result result = new Result();
Connection conn = createConnection();
Session s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
final MessageConsumer cons = s.createConsumer(queue1);
cons.setMessageListener(new MessageListener() {
@Override
public void onMessage(final Message m) {
// close the connection on the same thread that processed the message
try {
cons.close();
result.setSuccess();
} catch (Exception e) {
result.setFailure(e);
}
}
});
conn.start();
result.waitForResult();
ProxyAssertSupport.assertTrue(result.isSuccess());
ProxyAssertSupport.assertNull(result.getFailure());
// it's auto _ack so message *should not* be acked (auto ack acks after successfully completion of onMessage
Thread.sleep(1000);
assertRemainingMessages(1);
conn.close();
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class RequestReplyTempDestRemovalAdvisoryRaceTest method noConsumerAdvisory.
private void noConsumerAdvisory() throws JMSException {
for (BrokerItem item : brokers.values()) {
ActiveMQConnectionFactory brokerAFactory = new ActiveMQConnectionFactory(item.broker.getTransportConnectorByScheme("tcp").getName() + "?jms.watchTopicAdvisories=false");
Connection connection = brokerAFactory.createConnection();
connection.start();
connection.createSession(false, Session.AUTO_ACKNOWLEDGE).createConsumer(AdvisorySupport.getNoTopicConsumersAdvisoryTopic(new ActiveMQTempTopic(">"))).setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
sendsWithNoConsumers.incrementAndGet();
}
});
}
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class RequestReplyTempDestRemovalAdvisoryRaceTest method forwardFailureAdvisory.
private void forwardFailureAdvisory() throws JMSException {
for (BrokerItem item : brokers.values()) {
ActiveMQConnectionFactory brokerAFactory = new ActiveMQConnectionFactory(item.broker.getTransportConnectorByScheme("tcp").getName() + "?jms.watchTopicAdvisories=false");
Connection connection = brokerAFactory.createConnection();
connection.start();
connection.createSession(false, Session.AUTO_ACKNOWLEDGE).createConsumer(AdvisorySupport.getNetworkBridgeForwardFailureAdvisoryTopic()).setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
forwardFailures.incrementAndGet();
}
});
}
}
Aggregations