use of org.apache.activemq.artemis.api.core.client.ClientProducer in project activemq-artemis by apache.
the class TransformTest method publish.
public static void publish(String destination, Serializable object, String contentType) throws Exception {
ClientSession session = manager.getQueueManager().getSessionFactory().createSession();
try {
ClientProducer producer = session.createProducer(destination);
ClientMessage message = session.createMessage(Message.OBJECT_TYPE, false);
if (contentType == null) {
ActiveMQ.setEntity(message, object);
} else
ActiveMQ.setEntity(message, object, contentType);
producer.send(message);
session.start();
} finally {
session.close();
}
}
use of org.apache.activemq.artemis.api.core.client.ClientProducer in project activemq-artemis by apache.
the class PostMessage method publish.
public void publish(HttpHeaders headers, byte[] body, String dup, boolean durable, Long ttl, Long expiration, Integer priority) throws Exception {
Pooled pooled = getPooled();
try {
ClientProducer producer = pooled.producer;
ClientMessage message = createActiveMQMessage(headers, body, durable, ttl, expiration, priority, pooled.session);
message.putStringProperty(Message.HDR_DUPLICATE_DETECTION_ID.toString(), dup);
producer.send(message);
ActiveMQRestLogger.LOGGER.debug("Sent message: " + message);
pool.add(pooled);
} catch (Exception ex) {
try {
pooled.session.close();
} catch (ActiveMQException e) {
}
addPooled();
throw ex;
}
}
use of org.apache.activemq.artemis.api.core.client.ClientProducer in project activemq-artemis by apache.
the class ConsumerWindowSizeTest method testMultipleImmediate.
@Test
public void testMultipleImmediate() throws Exception {
final int NUMBER_OF_MESSAGES = 200;
ActiveMQServer server = createServer(false, isNetty());
server.start();
locator.setConsumerWindowSize(0);
final ClientSessionFactory sf = createSessionFactory(locator);
{
ClientSession session = sf.createSession(false, false, false);
session.createQueue("testWindow", "testWindow", true);
session.close();
}
Thread[] threads = new Thread[10];
final AtomicInteger errors = new AtomicInteger(0);
final CountDownLatch latchStart = new CountDownLatch(1);
final AtomicInteger received = new AtomicInteger(0);
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread() {
@Override
public void run() {
try {
ClientSession session = sf.createSession(false, false);
ClientConsumer consumer = session.createConsumer("testWindow");
session.start();
latchStart.await(10, TimeUnit.SECONDS);
while (true) {
ClientMessage msg = consumer.receiveImmediate();
if (msg == null) {
break;
}
msg.acknowledge();
session.commit();
received.incrementAndGet();
}
} catch (Throwable e) {
e.printStackTrace();
errors.incrementAndGet();
}
}
};
threads[i].start();
}
ClientSession senderSession = sf.createSession(false, false);
ClientProducer producer = senderSession.createProducer("testWindow");
ClientMessage sent = senderSession.createMessage(true);
sent.putStringProperty("hello", "world");
for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
producer.send(sent);
senderSession.commit();
}
latchStart.countDown();
for (Thread t : threads) {
t.join();
}
Assert.assertEquals(0, errors.get());
Assert.assertEquals(NUMBER_OF_MESSAGES, received.get());
}
use of org.apache.activemq.artemis.api.core.client.ClientProducer in project activemq-artemis by apache.
the class ConsumerWindowSizeTest method internalTestSlowConsumerNoBuffer.
private void internalTestSlowConsumerNoBuffer(final boolean largeMessages) throws Exception {
ActiveMQServer server = createServer(false, isNetty());
ClientSession sessionB = null;
ClientSession session = null;
try {
final int numberOfMessages = 100;
server.start();
locator.setConsumerWindowSize(0);
if (largeMessages) {
locator.setMinLargeMessageSize(100);
}
ClientSessionFactory sf = createSessionFactory(locator);
session = sf.createSession(false, true, true);
SimpleString ADDRESS = addressA;
session.createQueue(ADDRESS, ADDRESS, true);
sessionB = sf.createSession(false, true, true);
sessionB.start();
session.start();
ClientConsumerInternal consNeverUsed = (ClientConsumerInternal) sessionB.createConsumer(ADDRESS);
ClientProducer prod = session.createProducer(ADDRESS);
// This will force a credit to be sent, but if the message wasn't received we need to take out that credit from
// the server
// or the client will be buffering messages
Assert.assertNull(consNeverUsed.receive(1));
ClientMessage msg = createTextMessage(session, "This one will expire");
if (largeMessages) {
msg.getBodyBuffer().writeBytes(new byte[600]);
}
msg.setExpiration(System.currentTimeMillis() + 100);
prod.send(msg);
msg = createTextMessage(session, "First-on-non-buffered");
prod.send(msg);
Thread.sleep(110);
// It will be able to receive another message, but it shouldn't send a credit again, as the credit was already
// sent
msg = consNeverUsed.receive(TIMEOUT * 1000);
Assert.assertNotNull(msg);
Assert.assertEquals("First-on-non-buffered", getTextMessage(msg));
msg.acknowledge();
ClientConsumer cons1 = session.createConsumer(ADDRESS);
for (int i = 0; i < numberOfMessages; i++) {
msg = createTextMessage(session, "Msg" + i);
if (largeMessages) {
msg.getBodyBuffer().writeBytes(new byte[600]);
}
prod.send(msg);
}
for (int i = 0; i < numberOfMessages; i++) {
msg = cons1.receive(1000);
Assert.assertNotNull("expected message at i = " + i, msg);
Assert.assertEquals("Msg" + i, getTextMessage(msg));
msg.acknowledge();
}
Assert.assertEquals(0, consNeverUsed.getBufferSize());
session.close();
session = null;
sessionB.close();
sessionB = null;
Assert.assertEquals(0, getMessageCount(server, ADDRESS.toString()));
} finally {
try {
if (session != null) {
session.close();
}
if (sessionB != null) {
sessionB.close();
}
} catch (Exception ignored) {
}
}
}
use of org.apache.activemq.artemis.api.core.client.ClientProducer in project activemq-artemis by apache.
the class ConsumerWindowSizeTest method testSendWindowSize.
/*
* tests send window size. we do this by having 2 receivers on the q. since we roundrobin the consumer for delivery we
* know if consumer 1 has received n messages then consumer 2 must have also have received n messages or at least up
* to its window size
* */
@Test
public void testSendWindowSize() throws Exception {
ActiveMQServer messagingService = createServer(false, isNetty());
locator.setBlockOnNonDurableSend(false);
messagingService.start();
int numMessage = 100;
locator.setConsumerWindowSize(numMessage * getMessageEncodeSize(addressA));
ClientSessionFactory cf = createSessionFactory(locator);
ClientSession sendSession = cf.createSession(false, true, true);
ClientSession receiveSession = cf.createSession(false, true, true);
sendSession.createQueue(addressA, queueA, false);
ClientConsumer receivingConsumer = receiveSession.createConsumer(queueA);
ClientSession session = cf.createSession(false, true, true);
ClientProducer cp = sendSession.createProducer(addressA);
ClientConsumer cc = session.createConsumer(queueA);
session.start();
receiveSession.start();
for (int i = 0; i < numMessage * 4; i++) {
cp.send(sendSession.createMessage(false));
}
for (int i = 0; i < numMessage * 2; i++) {
ClientMessage m = receivingConsumer.receive(5000);
Assert.assertNotNull(m);
m.acknowledge();
}
receiveSession.close();
for (int i = 0; i < numMessage * 2; i++) {
ClientMessage m = cc.receive(5000);
Assert.assertNotNull(m);
m.acknowledge();
}
session.close();
sendSession.close();
Assert.assertEquals(0, getMessageCount(messagingService, queueA.toString()));
}
Aggregations