use of javax.jms.JMSContext in project activemq-artemis by apache.
the class JmsConsumerTest method testIndividualACKJms2.
@Test
public void testIndividualACKJms2() throws Exception {
JMSContext context = cf.createContext(ActiveMQJMSConstants.INDIVIDUAL_ACKNOWLEDGE);
jBossQueue = ActiveMQJMSClient.createQueue(JmsConsumerTest.Q_NAME);
JMSProducer producer = context.createProducer();
JMSConsumer consumer = context.createConsumer(jBossQueue);
int noOfMessages = 100;
for (int i = 0; i < noOfMessages; i++) {
producer.send(jBossQueue, context.createTextMessage("m" + i));
}
context.start();
// Consume even numbers first
for (int i = 0; i < noOfMessages; i++) {
Message m = consumer.receive(500);
Assert.assertNotNull(m);
if (i % 2 == 0) {
m.acknowledge();
}
}
context.close();
context = cf.createContext(ActiveMQJMSConstants.INDIVIDUAL_ACKNOWLEDGE);
consumer = context.createConsumer(jBossQueue);
// Consume odd numbers first
for (int i = 0; i < noOfMessages; i++) {
if (i % 2 == 0) {
continue;
}
TextMessage m = (TextMessage) consumer.receive(1000);
Assert.assertNotNull(m);
m.acknowledge();
Assert.assertEquals("m" + i, m.getText());
}
SimpleString queueName = new SimpleString(JmsConsumerTest.Q_NAME);
context.close();
Assert.assertEquals(0, ((Queue) server.getPostOffice().getBinding(queueName).getBindable()).getDeliveringCount());
Assert.assertEquals(0, getMessageCount((Queue) server.getPostOffice().getBinding(queueName).getBindable()));
}
use of javax.jms.JMSContext in project activemq-artemis by apache.
the class JmsContextTest method testRollbackTest.
@Test
public void testRollbackTest() {
JMSContext ctx = addContext(cf.createContext(JMSContext.SESSION_TRANSACTED));
JMSProducer producer = ctx.createProducer();
JMSConsumer cons = ctx.createConsumer(queue1);
producer.send(queue1, context.createTextMessage("hello"));
ctx.rollback();
assertNull(cons.receiveNoWait());
producer.send(queue1, context.createTextMessage("hello"));
ctx.commit();
assertNotNull(cons.receiveNoWait());
ctx.commit();
ctx.rollback();
assertNull(cons.receiveNoWait());
cons.close();
}
use of javax.jms.JMSContext in project activemq-artemis by apache.
the class JmsContextTest method testGetAnotherContextFromIt.
@Test
public void testGetAnotherContextFromIt() {
JMSContext c2 = context.createContext(Session.DUPS_OK_ACKNOWLEDGE);
Assert.assertNotNull(c2);
Assert.assertEquals(Session.DUPS_OK_ACKNOWLEDGE, c2.getSessionMode());
Message m2 = c2.createMessage();
Assert.assertNotNull(m2);
// should close its session, but not its (shared) connection
c2.close();
try {
c2.createMessage();
Assert.fail("session should be closed...");
} catch (JMSRuntimeException expected) {
// expected
}
Message m1 = context.createMessage();
Assert.assertNotNull("connection must be open", m1);
}
use of javax.jms.JMSContext in project activemq-artemis by apache.
the class JmsContextTest method testSetGetClientIdNewContext.
@Test
public void testSetGetClientIdNewContext() {
final String id = "123";
// createContext();
JMSContext c = context;
c.setClientID(id);
JMSContext c2 = addContext(c.createContext(Session.CLIENT_ACKNOWLEDGE));
Assert.assertEquals(id, c2.getClientID());
}
use of javax.jms.JMSContext in project activemq-artemis by apache.
the class JmsContextTest method recoverAckTest.
@Test
public void recoverAckTest() throws Exception {
try (JMSContext context = cf.createContext(JMSContext.CLIENT_ACKNOWLEDGE)) {
int numMessages = 10;
TextMessage textMessage = null;
// Create JMSConsumer from JMSContext
JMSConsumer consumer = context.createConsumer(queue1);
// Create JMSProducer from JMSContext
JMSProducer producer = context.createProducer();
// send messages
for (int i = 0; i < numMessages; i++) {
String message = "text message " + i;
textMessage = context.createTextMessage(message);
textMessage.setStringProperty("COM_SUN_JMS_TESTNAME", "recoverAckTest" + i);
producer.send(queue1, textMessage);
}
// receive messages but do not acknowledge
for (int i = 0; i < numMessages; i++) {
textMessage = (TextMessage) consumer.receive(5000);
assertNotNull(textMessage);
}
context.recover();
// receive messages a second time followed by acknowledge
for (int i = 0; i < numMessages; i++) {
textMessage = (TextMessage) consumer.receive(5000);
assertNotNull(textMessage);
}
// Acknowledge all messages
context.acknowledge();
}
// doing this check with another context / consumer to make sure it was acked.
try (JMSContext context = cf.createContext(JMSContext.CLIENT_ACKNOWLEDGE)) {
// Create JMSConsumer from JMSContext
JMSConsumer consumer = context.createConsumer(queue1);
assertNull(consumer.receiveNoWait());
}
}
Aggregations