use of javax.jms.BytesMessage in project activemq-artemis by apache.
the class LargeMessageTestSupport method onMessage.
@Override
public void onMessage(Message msg) {
try {
BytesMessage ba = (BytesMessage) msg;
validMessageConsumption &= isSame(ba);
assertTrue(ba.getBodyLength() == LARGE_MESSAGE_SIZE);
if (messageCount.incrementAndGet() >= MESSAGE_COUNT) {
synchronized (messageCount) {
messageCount.notify();
}
}
LOG.info("got message = " + messageCount);
if (messageCount.get() % 50 == 0) {
LOG.info("count = " + messageCount);
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of javax.jms.BytesMessage in project activemq-artemis by apache.
the class LoadTestBurnIn method testSendReceive.
public void testSendReceive() throws Exception {
// Durable consumer combination is only valid with topics
if (durableConsumer && destinationType != ActiveMQDestination.TOPIC_TYPE) {
return;
}
connection.setClientID(getName());
connection.getPrefetchPolicy().setAll(1000);
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = createDestination(session, destinationType);
MessageConsumer consumer;
if (durableConsumer) {
consumer = session.createDurableSubscriber((Topic) destination, "sub1:" + System.currentTimeMillis());
} else {
consumer = session.createConsumer(destination);
}
profilerPause("Ready: ");
final CountDownLatch producerDoneLatch = new CountDownLatch(1);
// Send the messages, async
new Thread() {
@Override
public void run() {
Connection connection2 = null;
try {
connection2 = factory.createConnection();
Session session = connection2.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(deliveryMode);
for (int i = 0; i < messageCount; i++) {
BytesMessage m = session.createBytesMessage();
m.writeBytes(new byte[messageSize]);
producer.send(m);
}
producer.close();
} catch (JMSException e) {
e.printStackTrace();
} finally {
safeClose(connection2);
producerDoneLatch.countDown();
}
}
}.start();
// Make sure all the messages were delivered.
Message message = null;
for (int i = 0; i < messageCount; i++) {
message = consumer.receive(5000);
assertNotNull("Did not get message: " + i, message);
}
profilerPause("Done: ");
assertNull(consumer.receiveNoWait());
message.acknowledge();
// Make sure the producer thread finishes.
assertTrue(producerDoneLatch.await(5, TimeUnit.SECONDS));
}
use of javax.jms.BytesMessage in project activemq-artemis by apache.
the class ConsumerTest method testMultipleConsumersOnSharedQueue.
@Test
public void testMultipleConsumersOnSharedQueue() throws Throwable {
if (!isNetty() || this.durable) {
return;
}
final boolean durable = false;
final long TIMEOUT_MILLIS = TimeUnit.MINUTES.toMillis(1);
final int forks = 100;
final int queues = forks;
final int runs = 1;
final int messages = 1;
final ConnectionFactory factorySend = createFactory(1);
final AtomicLongArray receivedMessages = new AtomicLongArray(forks);
final Thread[] producersRunners = new Thread[forks];
final Thread[] consumersRunners = new Thread[forks];
// parties are forks (1 producer 1 consumer) + 1 controller in the main test thread
final CyclicBarrier onStartRun = new CyclicBarrier((forks * 2) + 1);
final CyclicBarrier onFinishRun = new CyclicBarrier((forks * 2) + 1);
final int messagesSent = forks * messages;
final AtomicInteger messagesRecieved = new AtomicInteger(0);
for (int i = 0; i < forks; i++) {
final int forkIndex = i;
final String queueName = "q_" + (forkIndex % queues);
final Thread producerRunner = new Thread(() -> {
try (Connection connection = factorySend.createConnection()) {
connection.start();
try (Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)) {
final javax.jms.Queue queue = session.createQueue(queueName);
try (MessageProducer producer = session.createProducer(queue)) {
producer.setDeliveryMode(durable ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT);
for (int r = 0; r < runs; r++) {
onStartRun.await();
for (int m = 0; m < messages; m++) {
final BytesMessage bytesMessage = session.createBytesMessage();
bytesMessage.writeInt(forkIndex);
producer.send(bytesMessage);
}
onFinishRun.await();
}
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
}
} catch (JMSException e) {
e.printStackTrace();
}
});
producerRunner.setDaemon(true);
final Thread consumerRunner = new Thread(() -> {
try (Connection connection = factorySend.createConnection()) {
connection.start();
try (Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)) {
final javax.jms.Queue queue = session.createQueue(queueName);
try (MessageConsumer consumer = session.createConsumer(queue)) {
for (int r = 0; r < runs; r++) {
onStartRun.await();
while (messagesRecieved.get() != messagesSent) {
final BytesMessage receivedMessage = (BytesMessage) consumer.receive(1000);
if (receivedMessage != null) {
final int receivedConsumerIndex = receivedMessage.readInt();
receivedMessages.getAndIncrement(receivedConsumerIndex);
messagesRecieved.incrementAndGet();
}
}
onFinishRun.await();
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
} catch (JMSException e) {
e.printStackTrace();
}
});
consumerRunner.setDaemon(true);
consumersRunners[forkIndex] = consumerRunner;
producersRunners[forkIndex] = producerRunner;
}
Stream.of(consumersRunners).forEach(Thread::start);
Stream.of(producersRunners).forEach(Thread::start);
final long messagesPerRun = (forks * messages);
for (int r = 0; r < runs; r++) {
onStartRun.await(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
System.out.println("started run " + r);
final long start = System.currentTimeMillis();
onFinishRun.await(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
final long elapsedMillis = System.currentTimeMillis() - start;
System.out.println((messagesPerRun * 1000L) / elapsedMillis + " msg/sec");
}
Stream.of(producersRunners).forEach(runner -> {
try {
runner.join(TIMEOUT_MILLIS * runs);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Stream.of(producersRunners).forEach(Thread::interrupt);
Stream.of(consumersRunners).forEach(runner -> {
try {
runner.join(TIMEOUT_MILLIS * runs);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Stream.of(consumersRunners).forEach(Thread::interrupt);
for (int i = 0; i < forks; i++) {
Assert.assertEquals("The consumer " + i + " must receive all the messages sent.", messages * runs, receivedMessages.get(i));
}
}
use of javax.jms.BytesMessage in project activemq-artemis by apache.
the class ConsumerTest method internalSend.
public void internalSend(int protocolSender, int protocolConsumer) throws Throwable {
internalSimpleSend(protocolSender, protocolConsumer);
ConnectionFactory factorySend = createFactory(protocolSender);
ConnectionFactory factoryConsume = protocolConsumer == protocolSender ? factorySend : createFactory(protocolConsumer);
Connection connection = factorySend.createConnection();
try {
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
javax.jms.Queue queue = session.createQueue(QUEUE.toString());
MessageProducer producer = session.createProducer(queue);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
long time = System.currentTimeMillis();
int NUMBER_OF_MESSAGES = 100;
for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
TextMessage msg = session.createTextMessage("hello " + i);
msg.setIntProperty("mycount", i);
producer.send(msg);
ObjectMessage objectMessage = session.createObjectMessage(new MyTest().setI(i));
producer.send(objectMessage);
MapMessage mapMessage = session.createMapMessage();
mapMessage.setInt("intOne", i);
mapMessage.setString("stringOne", Integer.toString(i));
producer.send(mapMessage);
StreamMessage stream = session.createStreamMessage();
stream.writeBoolean(true);
stream.writeInt(i);
producer.send(stream);
BytesMessage bytes = session.createBytesMessage();
bytes.writeUTF("string " + i);
producer.send(bytes);
}
long end = System.currentTimeMillis();
System.out.println("Time = " + (end - time));
{
TextMessage dummyMessage = session.createTextMessage();
dummyMessage.setJMSType("car");
dummyMessage.setStringProperty("color", "red");
dummyMessage.setLongProperty("weight", 3000);
dummyMessage.setText("testSelectorExampleFromSpecs:1");
producer.send(dummyMessage);
dummyMessage = session.createTextMessage();
dummyMessage.setJMSType("car");
dummyMessage.setStringProperty("color", "blue");
dummyMessage.setLongProperty("weight", 3000);
dummyMessage.setText("testSelectorExampleFromSpecs:2");
producer.send(dummyMessage);
}
connection.close();
if (this.durable) {
server.stop();
server.start();
}
connection = factoryConsume.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
queue = session.createQueue(QUEUE.toString());
connection.start();
MessageConsumer consumer = session.createConsumer(queue);
for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
TextMessage message = (TextMessage) consumer.receive(1000);
Assert.assertNotNull(message);
Assert.assertEquals(i, message.getIntProperty("mycount"));
Assert.assertEquals("hello " + i, message.getText());
ObjectMessage objectMessage = (ObjectMessage) consumer.receive(5000);
Assert.assertNotNull(objectMessage);
Assert.assertEquals(i, ((MyTest) objectMessage.getObject()).getI());
MapMessage mapMessage = (MapMessage) consumer.receive(1000);
Assert.assertNotNull(mapMessage);
Assert.assertEquals(i, mapMessage.getInt("intOne"));
Assert.assertEquals(Integer.toString(i), mapMessage.getString("stringOne"));
StreamMessage stream = (StreamMessage) consumer.receive(5000);
Assert.assertTrue(stream.readBoolean());
Assert.assertEquals(i, stream.readInt());
BytesMessage bytes = (BytesMessage) consumer.receive(5000);
Assert.assertEquals("string " + i, bytes.readUTF());
}
consumer.close();
consumer = session.createConsumer(queue, "JMSType = 'car' AND color = 'blue' AND weight > 2500");
TextMessage msg = (TextMessage) consumer.receive(1000);
Assert.assertEquals("testSelectorExampleFromSpecs:2", msg.getText());
consumer.close();
consumer = session.createConsumer(queue);
msg = (TextMessage) consumer.receive(5000);
Assert.assertNotNull(msg);
Assert.assertNull(consumer.receiveNoWait());
Wait.waitFor(() -> server.getPagingManager().getGlobalSize() == 0, 5000, 100);
Assert.assertEquals(0, server.getPagingManager().getGlobalSize());
} finally {
connection.close();
}
}
use of javax.jms.BytesMessage in project activemq-artemis by apache.
the class JMSMessageTypesTest method testBytesMessageSendReceive.
private void testBytesMessageSendReceive(Connection producerConnection, Connection consumerConnection) throws Throwable {
long time = System.currentTimeMillis();
Session session = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(getQueueName());
byte[] bytes = new byte[0xf + 1];
for (int i = 0; i <= 0xf; i++) {
bytes[i] = (byte) i;
}
MessageProducer producer = session.createProducer(queue);
for (int i = 0; i < NUM_MESSAGES; i++) {
System.out.println("Sending " + i);
BytesMessage message = session.createBytesMessage();
message.writeBytes(bytes);
message.setIntProperty("count", i);
producer.send(message);
}
Session sessionConsumer = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue consumerQueue = sessionConsumer.createQueue(getQueueName());
final MessageConsumer consumer = sessionConsumer.createConsumer(consumerQueue);
for (int i = 0; i < NUM_MESSAGES; i++) {
BytesMessage m = (BytesMessage) consumer.receive(5000);
Assert.assertNotNull("Could not receive message count=" + i + " on consumer", m);
m.reset();
long size = m.getBodyLength();
byte[] bytesReceived = new byte[(int) size];
m.readBytes(bytesReceived);
System.out.println("Received " + ByteUtil.bytesToHex(bytesReceived, 1) + " count - " + m.getIntProperty("count"));
Assert.assertArrayEquals(bytes, bytesReceived);
}
long taken = (System.currentTimeMillis() - time) / 1000;
System.out.println("taken = " + taken);
}
Aggregations