use of javax.jms.JMSContext in project wildfly by wildfly.
the class LegacyJMSTestCase method doSendAndReceive.
private void doSendAndReceive(String connectionFactoryLookup, String destinationLoookup) throws Exception {
Destination destination = (Destination) remoteContext.lookup(destinationLoookup);
assertNotNull(destination);
ConnectionFactory cf = (ConnectionFactory) remoteContext.lookup(connectionFactoryLookup);
assertNotNull(cf);
try (JMSContext producerContext = cf.createContext("guest", "guest");
JMSContext consumerContext = cf.createContext("guest", "guest")) {
final CountDownLatch latch = new CountDownLatch(10);
final List<String> result = new ArrayList<String>();
JMSConsumer consumer = consumerContext.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
TextMessage msg = (TextMessage) message;
try {
result.add(msg.getText());
latch.countDown();
} catch (JMSException e) {
e.printStackTrace();
}
}
});
JMSProducer producer = producerContext.createProducer();
for (int i = 0; i < 10; i++) {
String text = "Test" + i;
producer.send(destination, text);
}
assertTrue(latch.await(3, SECONDS));
assertEquals(10, result.size());
for (int i = 0; i < result.size(); i++) {
assertEquals("Test" + i, result.get(i));
}
}
}
use of javax.jms.JMSContext in project wildfly by wildfly.
the class InjectedJMSContext method getDelegate.
/**
* Return the actual JMSContext used by this injection.
*
* The use of the correct AbstractJMSContext (one with the @RequestScoped, the other
* with the @TransactionScoped) is determined by the presence on an active transaction.
*/
@Override
JMSContext getDelegate() {
boolean inTx = isInTransaction();
AbstractJMSContext jmsContext = inTx ? transactedJMSContext.get() : requestedJMSContext;
ROOT_LOGGER.debugf("using %s to create the injected JMSContext", jmsContext, id);
ConnectionFactory connectionFactory = getConnectionFactory();
JMSContext contextInstance = jmsContext.getContext(id, info, connectionFactory);
// Correct phase to call close is afterCompletion {@see TransactionSynchronizationRegistry.registerInterposedSynchronization}
if (inTx) {
TransactedJMSContext transactedJMSContext = (TransactedJMSContext) jmsContext;
transactedJMSContext.registerCleanUpListener(transactionSynchronizationRegistry, contextInstance);
}
return contextInstance;
}
use of javax.jms.JMSContext in project wildfly by wildfly.
the class NotClosingInjectedContextTestCase method testLeakingConnection.
@Test
public void testLeakingConnection() throws Exception {
// length of log before execution
String sizeBefore = getLogLineCount(managementClient.getControllerClient());
try (JMSContext context = factory.createContext();
JMSConsumer consumer = context.createConsumer(queueVerify)) {
int j = 0;
while (true) {
String t = consumer.receiveBody(String.class, adjust(2000));
if (t == null) {
LOGGER.info("Received null message");
break;
} else {
LOGGER.info("Received message:" + (++j));
}
}
Assert.assertEquals("There should be 2 received messages.", 2, j);
// size of log after execution
String sizeAfter = getLogLineCount(managementClient.getControllerClient());
long difference = Long.parseLong(sizeAfter) - Long.parseLong(sizeBefore);
// validate, that there is no "IJ000100: Closing a connection for you. Please close them yourself"
List<ModelNode> lines = getLogs(managementClient.getControllerClient(), difference);
for (ModelNode line : lines) {
if (line.asString().contains("IJ000100:")) {
Assert.fail("JMS context is not closed");
}
}
}
}
use of javax.jms.JMSContext in project wildfly by wildfly.
the class NotClosingInjectedContextTestCase method tearDown.
@After
public void tearDown() throws JMSException {
// drain the queue to remove any pending messages from it
try (JMSContext context = factory.createContext()) {
JMSConsumer consumer = context.createConsumer(queue);
Message m;
do {
m = consumer.receiveNoWait();
} while (m != null);
consumer = context.createConsumer(queueVerify);
do {
m = consumer.receiveNoWait();
} while (m != null);
}
}
use of javax.jms.JMSContext in project wildfly by wildfly.
the class ScopedInjectedJMSContextTestCase method requestScoped.
/**
* Test that a request-scoped JMSContext is properly cleaned up after the transaction completion.
*/
@Test
public void requestScoped() throws Exception {
String text = UUID.randomUUID().toString();
try (JMSContext context = factory.createContext()) {
TemporaryQueue tempQueue = context.createTemporaryQueue();
// send a request/reply message to ensure that the MDB as received the message
// and set its consumer field
context.createProducer().setJMSReplyTo(tempQueue).setDeliveryMode(NON_PERSISTENT).send(queueForRequestScope, text);
JMSConsumer consumer = context.createConsumer(tempQueue);
String reply = consumer.receiveBody(String.class, TimeoutUtil.adjust(5000));
assertNotNull(reply);
JMSConsumer consumerInMDB = RequestScopedMDB.consumer;
assertNotNull(consumerInMDB);
try {
consumerInMDB.receiveBody(String.class);
fail("the EJB must throw an exception as its injected JMSContext must be closed after the MDB handled the message");
} catch (Exception e) {
}
}
}
Aggregations