use of javax.jms.JMSContext in project wildfly by wildfly.
the class AbstractMessagingHATestCase method receiveNoMessage.
protected static void receiveNoMessage(Context ctx, String destinationLookup) throws NamingException {
ConnectionFactory cf = (ConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory");
assertNotNull(cf);
Destination destination = (Destination) ctx.lookup(destinationLookup);
assertNotNull(destination);
try (JMSContext context = cf.createContext("guest", "guest");
JMSConsumer consumer = context.createConsumer(destination)) {
String text = consumer.receiveBody(String.class, TimeoutUtil.adjust(5000));
assertNull(text);
}
}
use of javax.jms.JMSContext in project wildfly by wildfly.
the class CorruptedLogTestCase method testCorruptedJournal.
/**
* Set the journal file size to 100k.
* Send 100 messages of 20k.
* Set the journal file size to 200k.
* Restart the server.
* Consume the messages.
* Should have journal files in the attic.
*
* @throws javax.naming.NamingException
* @throws java.io.IOException
*/
@Test
public void testCorruptedJournal() throws NamingException, IOException {
counter++;
InitialContext remoteContext = createJNDIContext();
ConnectionFactory cf = (ConnectionFactory) remoteContext.lookup("jms/RemoteConnectionFactory");
Queue queue = (Queue) remoteContext.lookup("queue/corrupted");
try (JMSContext context = cf.createContext("guest", "guest", JMSContext.AUTO_ACKNOWLEDGE)) {
JMSProducer producer = context.createProducer();
for (int i = 0; i < 100; i++) {
producer.send(queue, context.createTextMessage(RandomStringUtils.randomAlphabetic(20000)));
}
}
setJournalSize(200L);
remoteContext.close();
managementClient.close();
container.stop(DEFAULT_FULL_JBOSSAS);
container.start(DEFAULT_FULL_JBOSSAS);
managementClient = createManagementClient();
remoteContext = createJNDIContext();
cf = (ConnectionFactory) remoteContext.lookup("jms/RemoteConnectionFactory");
queue = (Queue) remoteContext.lookup("queue/corrupted");
try (JMSContext context = cf.createContext("guest", "guest", JMSContext.AUTO_ACKNOWLEDGE)) {
JMSConsumer consumer = context.createConsumer(queue);
for (int i = 0; i < 100; i++) {
TextMessage message = (TextMessage) consumer.receive(TimeoutUtil.adjust(500));
Assert.assertNotNull(message);
}
}
Path attic = getAtticPath();
Assert.assertTrue("Couldn't find " + attic.toAbsolutePath(), Files.exists(attic));
Assert.assertTrue(Files.isDirectory(attic));
try (Stream<Path> stream = Files.list(attic)) {
long nbAtticFiles = stream.collect(Collectors.counting());
Assert.assertTrue(nbAtticFiles > 0L);
}
}
use of javax.jms.JMSContext in project wildfly by wildfly.
the class CorruptedLogTestCase method testCorruptedJournalNotSaved.
/**
* Set the journal max attic files to 0 (aka don't store corrupted journal files).
* Set the journal file size to 100k.
* Send 100 messages of 20k.
* Set the journal file size to 200k.
* Restart the server.
* Consume the messages.
* Shouldn't have journal files in the attic.
*
* @throws javax.naming.NamingException
* @throws java.io.IOException
*/
@Test
public void testCorruptedJournalNotSaved() throws NamingException, IOException {
Path attic = getAtticPath();
counter++;
JMSOperations jmsOperations = JMSOperationsProvider.getInstance(managementClient.getControllerClient());
managementClient.getControllerClient().execute(Operations.createWriteAttributeOperation(jmsOperations.getServerAddress(), "journal-max-attic-files", 0));
jmsOperations.close();
managementClient.close();
container.stop(DEFAULT_FULL_JBOSSAS);
PathUtil.deleteRecursively(attic);
container.start(DEFAULT_FULL_JBOSSAS);
Assert.assertFalse("Couldn't find " + attic.toAbsolutePath(), Files.exists(attic));
managementClient = createManagementClient();
InitialContext remoteContext = createJNDIContext();
ConnectionFactory cf = (ConnectionFactory) remoteContext.lookup("jms/RemoteConnectionFactory");
Queue queue = (Queue) remoteContext.lookup("queue/corrupted");
try (JMSContext context = cf.createContext("guest", "guest", JMSContext.AUTO_ACKNOWLEDGE)) {
JMSProducer producer = context.createProducer();
for (int i = 0; i < 100; i++) {
producer.send(queue, context.createTextMessage(RandomStringUtils.randomAlphabetic(20000)));
}
}
setJournalSize(200L);
remoteContext.close();
managementClient.close();
container.stop(DEFAULT_FULL_JBOSSAS);
container.start(DEFAULT_FULL_JBOSSAS);
managementClient = createManagementClient();
remoteContext = createJNDIContext();
cf = (ConnectionFactory) remoteContext.lookup("jms/RemoteConnectionFactory");
queue = (Queue) remoteContext.lookup("queue/corrupted");
try (JMSContext context = cf.createContext("guest", "guest", JMSContext.AUTO_ACKNOWLEDGE)) {
JMSConsumer consumer = context.createConsumer(queue);
for (int i = 0; i < 100; i++) {
TextMessage message = (TextMessage) consumer.receive(TimeoutUtil.adjust(500));
Assert.assertNotNull(message);
}
}
if (Files.exists(attic)) {
Assert.assertTrue(Files.isDirectory(attic));
try (Stream<Path> stream = Files.list(attic)) {
long nbAtticFiles = stream.collect(Collectors.counting());
Assert.assertEquals("We shouldn't have any file in the attic", 0L, nbAtticFiles);
}
}
}
use of javax.jms.JMSContext 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);
}
use of javax.jms.JMSContext in project tomee by apache.
the class JMS2AMQTest method cdi.
@Test
public void cdi() throws InterruptedException {
final String text = TEXT + "3";
final AtomicReference<Throwable> error = new AtomicReference<>();
final CountDownLatch ready = new CountDownLatch(1);
final CountDownLatch over = new CountDownLatch(1);
new Thread() {
{
setName(JMS2AMQTest.class.getName() + ".cdi#receiver");
}
@Override
public void run() {
final ContextsService contextsService = WebBeansContext.currentInstance().getContextsService();
// spec defines it for request scope an transaction scope
contextsService.startContext(RequestScoped.class, null);
try {
ready.countDown();
assertEquals(text, context.createConsumer(destination3).receiveBody(String.class, TimeUnit.MINUTES.toMillis(1)));
// ensure we dont do a NPE if there is nothing to read
assertNull(context.createConsumer(destination3).receiveBody(String.class, 100));
} catch (final Throwable t) {
error.set(t);
} finally {
contextsService.endContext(RequestScoped.class, null);
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(destination3, 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