use of org.apache.activemq.artemis.api.core.client.ClientSessionFactory in project activemq-artemis by apache.
the class CreateQueueIdempotentTest method testSequentialCreateQueueIdempotency.
@Test
public void testSequentialCreateQueueIdempotency() throws Exception {
final SimpleString QUEUE = new SimpleString("SequentialCreateQueueIdempotency");
ServerLocator locator = createInVMNonHALocator();
ClientSessionFactory sf = addSessionFactory(createSessionFactory(locator));
ClientSession session = sf.createSession(false, true, true);
session.createQueue(QUEUE, QUEUE, null, true);
try {
session.createQueue(QUEUE, QUEUE, null, true);
fail("Expected exception, queue already exists");
} catch (ActiveMQQueueExistsException qee) {
// ok
} catch (ActiveMQException e) {
fail("Invalid Exception type:" + e.getType());
}
}
use of org.apache.activemq.artemis.api.core.client.ClientSessionFactory in project activemq-artemis by apache.
the class ExpiryAddressTest method testHeadersSet.
@Test
public void testHeadersSet() throws Exception {
final int NUM_MESSAGES = 5;
SimpleString ea = new SimpleString("DLA");
SimpleString qName = new SimpleString("q1");
AddressSettings addressSettings = new AddressSettings().setExpiryAddress(ea);
server.getAddressSettingsRepository().addMatch(qName.toString(), addressSettings);
SimpleString eq = new SimpleString("EA1");
clientSession.createQueue(ea, eq, null, false);
clientSession.createQueue(qName, qName, null, false);
ServerLocator locator1 = createInVMNonHALocator();
ClientSessionFactory sessionFactory = createSessionFactory(locator1);
ClientSession sendSession = sessionFactory.createSession(false, true, true);
ClientProducer producer = sendSession.createProducer(qName);
long expiration = System.currentTimeMillis();
for (int i = 0; i < NUM_MESSAGES; i++) {
ClientMessage tm = createTextMessage(clientSession, "Message:" + i);
tm.setExpiration(expiration);
producer.send(tm);
}
ClientConsumer clientConsumer = clientSession.createConsumer(qName);
clientSession.start();
ClientMessage m = clientConsumer.receiveImmediate();
Assert.assertNull(m);
// All the messages should now be in the EQ
ClientConsumer cc3 = clientSession.createConsumer(eq);
for (int i = 0; i < NUM_MESSAGES; i++) {
ClientMessage tm = cc3.receive(1000);
Assert.assertNotNull(tm);
String text = tm.getBodyBuffer().readString();
Assert.assertEquals("Message:" + i, text);
// Check the headers
Long actualExpiryTime = (Long) tm.getObjectProperty(Message.HDR_ACTUAL_EXPIRY_TIME);
Assert.assertTrue(actualExpiryTime >= expiration);
}
sendSession.close();
locator1.close();
}
use of org.apache.activemq.artemis.api.core.client.ClientSessionFactory in project activemq-artemis by apache.
the class ConsumerRoundRobinTest method testConsumersRoundRobinCorrectly.
@Test
public void testConsumersRoundRobinCorrectly() throws Exception {
ActiveMQServer server = createServer(false);
server.start();
ServerLocator locator = createInVMNonHALocator();
ClientSessionFactory cf = createSessionFactory(locator);
ClientSession session = addClientSession(cf.createSession(false, true, true));
session.createQueue(addressA, queueA, false);
ClientConsumer[] consumers = new ClientConsumer[5];
// start the session before we create the consumers, this is because start is non blocking and we have to
// guarantee
// all consumers have been started before sending messages
session.start();
consumers[0] = session.createConsumer(queueA);
consumers[1] = session.createConsumer(queueA);
consumers[2] = session.createConsumer(queueA);
consumers[3] = session.createConsumer(queueA);
consumers[4] = session.createConsumer(queueA);
ClientProducer cp = session.createProducer(addressA);
int numMessage = 10;
for (int i = 0; i < numMessage; i++) {
ClientMessage cm = session.createMessage(false);
cm.getBodyBuffer().writeInt(i);
cp.send(cm);
}
int currMessage = 0;
for (int i = 0; i < numMessage / 5; i++) {
log.info("i is " + i);
for (int j = 0; j < 5; j++) {
log.info("j is " + j);
ClientMessage cm = consumers[j].receive(5000);
Assert.assertNotNull(cm);
Assert.assertEquals(currMessage++, cm.getBodyBuffer().readInt());
cm.acknowledge();
}
}
}
use of org.apache.activemq.artemis.api.core.client.ClientSessionFactory in project activemq-artemis by apache.
the class ConsumerTest method testReceiveAndResend.
@Test
public void testReceiveAndResend() throws Exception {
final Set<Object> sessions = new ConcurrentHashSet<>();
final AtomicInteger errors = new AtomicInteger(0);
final SimpleString QUEUE_RESPONSE = SimpleString.toSimpleString("QUEUE_RESPONSE");
final int numberOfSessions = 50;
final int numberOfMessages = 10;
final CountDownLatch latchReceive = new CountDownLatch(numberOfSessions * numberOfMessages);
ClientSessionFactory sf = locator.createSessionFactory();
for (int i = 0; i < numberOfSessions; i++) {
ClientSession session = sf.createSession(false, true, true);
sessions.add(session);
session.createQueue(QUEUE, QUEUE.concat("" + i), null, true);
if (i == 0) {
session.createQueue(QUEUE_RESPONSE, QUEUE_RESPONSE);
}
ClientConsumer consumer = session.createConsumer(QUEUE.concat("" + i));
sessions.add(consumer);
{
consumer.setMessageHandler(new MessageHandler() {
@Override
public void onMessage(final ClientMessage msg) {
try {
ServerLocator locatorSendx = createFactory(isNetty()).setReconnectAttempts(15);
ClientSessionFactory factoryx = locatorSendx.createSessionFactory();
ClientSession sessionSend = factoryx.createSession(true, true);
sessions.add(sessionSend);
sessions.add(locatorSendx);
sessions.add(factoryx);
final ClientProducer prod = sessionSend.createProducer(QUEUE_RESPONSE);
sessionSend.start();
sessions.add(prod);
msg.acknowledge();
prod.send(sessionSend.createMessage(true));
prod.close();
sessionSend.commit();
sessionSend.close();
factoryx.close();
if (Thread.currentThread().isInterrupted()) {
System.err.println("Netty has interrupted a thread!!!");
errors.incrementAndGet();
}
} catch (Throwable e) {
e.printStackTrace();
errors.incrementAndGet();
} finally {
latchReceive.countDown();
}
}
});
}
session.start();
}
Thread tCons = new Thread() {
@Override
public void run() {
try {
final ServerLocator locatorSend = createFactory(isNetty());
final ClientSessionFactory factory = locatorSend.createSessionFactory();
final ClientSession sessionSend = factory.createSession(true, true);
ClientConsumer cons = sessionSend.createConsumer(QUEUE_RESPONSE);
sessionSend.start();
for (int i = 0; i < numberOfMessages * numberOfSessions; i++) {
ClientMessage msg = cons.receive(5000);
if (msg == null) {
break;
}
msg.acknowledge();
}
if (cons.receiveImmediate() != null) {
System.out.println("ERROR: Received an extra message");
errors.incrementAndGet();
}
sessionSend.close();
factory.close();
locatorSend.close();
} catch (Exception e) {
e.printStackTrace();
errors.incrementAndGet();
}
}
};
tCons.start();
ClientSession mainSessionSend = sf.createSession(true, true);
ClientProducer mainProd = mainSessionSend.createProducer(QUEUE);
for (int i = 0; i < numberOfMessages; i++) {
mainProd.send(mainSessionSend.createMessage(true));
}
latchReceive.await(2, TimeUnit.MINUTES);
tCons.join();
sf.close();
assertEquals("Had errors along the execution", 0, errors.get());
}
use of org.apache.activemq.artemis.api.core.client.ClientSessionFactory in project activemq-artemis by apache.
the class ConsumerTest method testInVMURI.
// https://jira.jboss.org/jira/browse/HORNETQ-111
// Test that, on rollback credits are released for messages cleared in the buffer
@Test
public void testInVMURI() throws Exception {
locator.close();
ServerLocator locator = addServerLocator(ServerLocatorImpl.newLocator("vm:/1"));
ClientSessionFactory factory = locator.createSessionFactory();
ClientSession session = factory.createSession();
ClientProducer producer = session.createProducer(QUEUE);
producer.send(session.createMessage(true));
ClientConsumer consumer = session.createConsumer(QUEUE);
session.start();
Assert.assertNotNull(consumer.receiveImmediate());
session.close();
factory.close();
}
Aggregations