use of javax.jms.JMSConsumer 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, 1000);
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) {
}
}
}
use of javax.jms.JMSConsumer in project wildfly by wildfly.
the class BeanManagedMessageConsumer method receive.
public boolean receive(Destination destination, String expectedText) throws Exception {
transaction.begin();
JMSConsumer consumer = context.createConsumer(destination);
String text = consumer.receiveBody(String.class, 1000);
assertNotNull(text);
assertEquals(expectedText, text);
transaction.commit();
try {
consumer.receiveBody(String.class, 1000);
Assert.fail("call must fail as the injected JMSContext is closed when the transaction is committed");
} catch (JMSRuntimeException e) {
// exception is expected
} catch (Exception e) {
throw e;
}
return true;
}
use of javax.jms.JMSConsumer in project wildfly by wildfly.
the class ClusteredMessagingTestCase method testClusteredTopic.
@Test
public void testClusteredTopic() throws Exception {
InitialContext contextFromServer0 = createJNDIContextFromServer0();
InitialContext contextFromServer1 = createJNDIContextFromServer1();
try (JMSContext jmsContext0 = createJMSContext(createJNDIContextFromServer0());
JMSContext jmsContext1 = createJMSContext(createJNDIContextFromServer1())) {
JMSConsumer consumer0 = jmsContext0.createConsumer((Destination) contextFromServer0.lookup(jmsTopicLookup));
JMSConsumer consumer1 = jmsContext1.createConsumer((Destination) contextFromServer1.lookup(jmsTopicLookup));
String text = UUID.randomUUID().toString();
// WIP test if the problem is that the view is not yet propagated
Thread.sleep(ClusteringTestConstants.GRACE_TIME_TO_MEMBERSHIP_CHANGE);
// send a message to the topic on server 0
sendMessage(contextFromServer0, jmsTopicLookup, text);
// consumers receive it on both servers
receiveMessage(consumer0, text);
receiveMessage(consumer1, text);
String anotherText = UUID.randomUUID().toString();
// send another message to topic on server 1
sendMessage(contextFromServer1, jmsTopicLookup, anotherText);
// consumers receive it on both servers
receiveMessage(consumer0, anotherText);
receiveMessage(consumer1, anotherText);
}
}
use of javax.jms.JMSConsumer 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.JMSConsumer in project tomee by apache.
the class JMS2AMQTest method receiveGetBody.
@Test
public void receiveGetBody() throws InterruptedException {
final String text = TEXT + "2";
final AtomicReference<Throwable> error = new AtomicReference<>();
final CountDownLatch ready = new CountDownLatch(1);
final CountDownLatch over = new CountDownLatch(1);
new Thread() {
@Override
public void run() {
{
setName(JMS2AMQTest.class.getName() + ".receiveGetBody#receiver");
}
try (final JMSContext context = cf.createContext()) {
try (final JMSConsumer consumer = context.createConsumer(destination2)) {
ready.countDown();
final Message receive = consumer.receive(TimeUnit.MINUTES.toMillis(1));
assertEquals(text, receive.getBody(String.class));
}
} catch (final Throwable ex) {
error.set(ex);
} finally {
over.countDown();
}
}
}.start();
ready.await(1, TimeUnit.MINUTES);
// just to ensure we called receive already
sleep(150);
// now send the message
try (final JMSContext context = cf.createContext()) {
context.createProducer().send(destination2, text);
} catch (final JMSRuntimeException ex) {
fail(ex.getMessage());
}
over.await(1, TimeUnit.MINUTES);
// ensure we got the message and no exception
final Throwable exception = error.get();
if (exception != null) {
exception.printStackTrace();
}
assertNull(exception == null ? "ok" : exception.getMessage(), exception);
}
Aggregations