Search in sources :

Example 26 with BytesMessage

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();
    }
}
Also used : BytesMessage(javax.jms.BytesMessage) JMSException(javax.jms.JMSException)

Example 27 with BytesMessage

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));
}
Also used : MessageConsumer(javax.jms.MessageConsumer) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) BytesMessage(javax.jms.BytesMessage) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) Topic(javax.jms.Topic) CountDownLatch(java.util.concurrent.CountDownLatch) Session(javax.jms.Session)

Example 28 with BytesMessage

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));
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) Connection(javax.jms.Connection) BytesMessage(javax.jms.BytesMessage) JMSException(javax.jms.JMSException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) CyclicBarrier(java.util.concurrent.CyclicBarrier) JmsConnectionFactory(org.apache.qpid.jms.JmsConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLongArray(java.util.concurrent.atomic.AtomicLongArray) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Test(org.junit.Test)

Example 29 with BytesMessage

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();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) MapMessage(javax.jms.MapMessage) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) Connection(javax.jms.Connection) BytesMessage(javax.jms.BytesMessage) JmsConnectionFactory(org.apache.qpid.jms.JmsConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) ObjectMessage(javax.jms.ObjectMessage) StreamMessage(javax.jms.StreamMessage) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession)

Example 30 with BytesMessage

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);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) BytesMessage(javax.jms.BytesMessage) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) Session(javax.jms.Session)

Aggregations

BytesMessage (javax.jms.BytesMessage)195 Session (javax.jms.Session)79 MessageProducer (javax.jms.MessageProducer)78 MessageConsumer (javax.jms.MessageConsumer)71 Test (org.junit.Test)62 Message (javax.jms.Message)57 TextMessage (javax.jms.TextMessage)54 Connection (javax.jms.Connection)41 JMSException (javax.jms.JMSException)39 Queue (javax.jms.Queue)37 MapMessage (javax.jms.MapMessage)34 ObjectMessage (javax.jms.ObjectMessage)30 StreamMessage (javax.jms.StreamMessage)24 HashMap (java.util.HashMap)20 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)17 Map (java.util.Map)15 Topic (javax.jms.Topic)14 ActiveMQMessage (org.apache.activemq.command.ActiveMQMessage)12 IOException (java.io.IOException)11 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)10