Search in sources :

Example 76 with BytesMessage

use of javax.jms.BytesMessage in project activemq-artemis by apache.

the class BytesMessageTest method assertEquivalent.

@Override
protected void assertEquivalent(final Message m, final int mode, final boolean redelivered) throws JMSException {
    super.assertEquivalent(m, mode, redelivered);
    BytesMessage bm = (BytesMessage) m;
    ProxyAssertSupport.assertEquals(true, bm.readBoolean());
    ProxyAssertSupport.assertEquals((byte) 3, bm.readByte());
    byte[] bytes = new byte[3];
    bm.readBytes(bytes);
    ProxyAssertSupport.assertEquals((byte) 4, bytes[0]);
    ProxyAssertSupport.assertEquals((byte) 5, bytes[1]);
    ProxyAssertSupport.assertEquals((byte) 6, bytes[2]);
    ProxyAssertSupport.assertEquals((char) 7, bm.readChar());
    ProxyAssertSupport.assertEquals(new Double(8.0), new Double(bm.readDouble()));
    ProxyAssertSupport.assertEquals(new Float(9.0), new Float(bm.readFloat()));
    ProxyAssertSupport.assertEquals(10, bm.readInt());
    ProxyAssertSupport.assertEquals(11L, bm.readLong());
    ProxyAssertSupport.assertEquals((short) 12, bm.readShort());
    ProxyAssertSupport.assertEquals("this is an UTF String", bm.readUTF());
}
Also used : BytesMessage(javax.jms.BytesMessage)

Example 77 with BytesMessage

use of javax.jms.BytesMessage in project activemq-artemis by apache.

the class StompV11Test method testSendMessageWithContentLength.

@Test
public void testSendMessageWithContentLength() throws Exception {
    MessageConsumer consumer = session.createConsumer(queue);
    conn.connect(defUser, defPass);
    byte[] data = new byte[] { 1, 0, 0, 4 };
    ClientStompFrame frame = conn.createFrame(Stomp.Commands.SEND).addHeader(Stomp.Headers.Send.DESTINATION, getQueuePrefix() + getQueueName()).setBody(new String(data, StandardCharsets.UTF_8)).addHeader(Stomp.Headers.CONTENT_LENGTH, String.valueOf(data.length));
    conn.sendFrame(frame);
    BytesMessage message = (BytesMessage) consumer.receive(10000);
    Assert.assertNotNull(message);
    assertEquals(data.length, message.getBodyLength());
    assertEquals(data[0], message.readByte());
    assertEquals(data[1], message.readByte());
    assertEquals(data[2], message.readByte());
    assertEquals(data[3], message.readByte());
}
Also used : ClientStompFrame(org.apache.activemq.artemis.tests.integration.stomp.util.ClientStompFrame) MessageConsumer(javax.jms.MessageConsumer) BytesMessage(javax.jms.BytesMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Test(org.junit.Test)

Example 78 with BytesMessage

use of javax.jms.BytesMessage in project activemq-artemis by apache.

the class MemoryLimitTest method testCursorBatch.

@Test(timeout = 120000)
public void testCursorBatch() throws Exception {
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost?jms.prefetchPolicy.all=10");
    factory.setOptimizeAcknowledge(true);
    Connection conn = factory.createConnection();
    conn.start();
    Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    Queue queue = sess.createQueue("STORE");
    final ProducerThread producer = new ProducerThread(sess, queue) {

        @Override
        protected Message createMessage(int i) throws Exception {
            BytesMessage bytesMessage = sess.createBytesMessage();
            bytesMessage.writeBytes(payload);
            return bytesMessage;
        }
    };
    producer.setMessageCount(2000);
    producer.start();
    producer.join();
    Thread.sleep(1000);
    // assert we didn't break high watermark (70%) usage
    final Destination dest = broker.getDestination((ActiveMQQueue) queue);
    LOG.info("Destination usage: " + dest.getMemoryUsage());
    int percentUsage = dest.getMemoryUsage().getPercentUsage();
    assertTrue("Should be less than 70% of limit but was: " + percentUsage, percentUsage <= 71);
    LOG.info("Broker usage: " + broker.getSystemUsage().getMemoryUsage());
    assertTrue(broker.getSystemUsage().getMemoryUsage().getPercentUsage() <= 71);
    // consume one message
    MessageConsumer consumer = sess.createConsumer(queue);
    Message msg = consumer.receive(5000);
    msg.acknowledge();
    // this should free some space and allow us to get new batch of messages in the memory
    // exceeding the limit
    assertTrue("Limit is exceeded", Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            LOG.info("Destination usage: " + dest.getMemoryUsage());
            return dest.getMemoryUsage().getPercentUsage() >= 200;
        }
    }));
    LOG.info("Broker usage: " + broker.getSystemUsage().getMemoryUsage());
    assertTrue(broker.getSystemUsage().getMemoryUsage().getPercentUsage() >= 200);
    // let's make sure we can consume all messages
    for (int i = 1; i < 2000; i++) {
        msg = consumer.receive(5000);
        if (msg == null) {
            dumpAllThreads("NoMessage");
        }
        assertNotNull("Didn't receive message " + i, msg);
        msg.acknowledge();
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) Destination(org.apache.activemq.broker.region.Destination) MessageConsumer(javax.jms.MessageConsumer) ProducerThread(org.apache.activemq.util.ProducerThread) Message(javax.jms.Message) BytesMessage(javax.jms.BytesMessage) Connection(javax.jms.Connection) BytesMessage(javax.jms.BytesMessage) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Queue(javax.jms.Queue) Session(javax.jms.Session) Test(org.junit.Test)

Example 79 with BytesMessage

use of javax.jms.BytesMessage in project activemq-artemis by apache.

the class GeneralInteropTest method sendBytesMessageUsingOpenWire.

private void sendBytesMessageUsingOpenWire(byte[] bytesData) throws Exception {
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ActiveMQDestination destination = createDestination(session, ActiveMQDestination.QUEUE_TYPE);
    System.out.println("destination: " + destination);
    final ActiveMQMessageProducer producer = (ActiveMQMessageProducer) session.createProducer(destination);
    BytesMessage bytesMessage = session.createBytesMessage();
    bytesMessage.writeBytes(bytesData);
    bytesMessage.writeBoolean(true);
    bytesMessage.writeLong(99999L);
    bytesMessage.writeChar('h');
    bytesMessage.writeInt(987);
    bytesMessage.writeShort((short) 1099);
    bytesMessage.writeUTF("hellobytes");
    producer.send(bytesMessage);
}
Also used : ActiveMQMessageProducer(org.apache.activemq.ActiveMQMessageProducer) BytesMessage(javax.jms.BytesMessage) Session(javax.jms.Session) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination)

Example 80 with BytesMessage

use of javax.jms.BytesMessage in project activemq-artemis by apache.

the class GeneralInteropTest method testReceivingFromCore.

@Test
public void testReceivingFromCore() throws Exception {
    final String text = "HelloWorld";
    // text messages
    sendTextMessageUsingCoreJms(queueName, text);
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ActiveMQDestination destination = createDestination(session, ActiveMQDestination.QUEUE_TYPE);
    System.out.println("destination: " + destination);
    final ActiveMQMessageConsumer consumer = (ActiveMQMessageConsumer) session.createConsumer(destination);
    TextMessage textMessage = (TextMessage) consumer.receive(5000);
    assertEquals(text, textMessage.getText());
    assertEquals(destination, textMessage.getJMSDestination());
    // map messages
    sendMapMessageUsingCoreJms(queueName);
    MapMessage mapMessage = (MapMessage) consumer.receive(5000);
    assertEquals(destination, mapMessage.getJMSDestination());
    assertTrue(mapMessage.getBoolean("aboolean"));
    assertEquals((byte) 4, mapMessage.getByte("abyte"));
    byte[] bytes = mapMessage.getBytes("abytes");
    assertEquals(2, bytes.length);
    assertEquals((byte) 4, bytes[0]);
    assertEquals((byte) 5, bytes[1]);
    assertEquals('a', mapMessage.getChar("achar"));
    Double doubleVal = mapMessage.getDouble("adouble");
    assertTrue(doubleVal.equals(Double.valueOf(4.4)));
    Float floatVal = mapMessage.getFloat("afloat");
    assertTrue(floatVal.equals(Float.valueOf(4.5F)));
    assertEquals(40, mapMessage.getInt("aint"));
    assertEquals(80L, mapMessage.getLong("along"));
    assertEquals(65, mapMessage.getShort("ashort"));
    assertEquals("hello", mapMessage.getString("astring"));
    // object message
    SimpleSerializable obj = new SimpleSerializable();
    sendObjectMessageUsingCoreJms(queueName, obj);
    ObjectMessage objectMessage = (ObjectMessage) consumer.receive(5000);
    SimpleSerializable data = (SimpleSerializable) objectMessage.getObject();
    assertEquals(obj.objName, data.objName);
    assertEquals(obj.intVal, data.intVal);
    assertEquals(obj.longVal, data.longVal);
    // stream messages
    sendStreamMessageUsingCoreJms(queueName);
    StreamMessage streamMessage = (StreamMessage) consumer.receive(5000);
    assertEquals(destination, streamMessage.getJMSDestination());
    assertTrue(streamMessage.readBoolean());
    assertEquals((byte) 2, streamMessage.readByte());
    byte[] streamBytes = new byte[2];
    streamMessage.readBytes(streamBytes);
    assertEquals(6, streamBytes[0]);
    assertEquals(7, streamBytes[1]);
    assertEquals('b', streamMessage.readChar());
    Double streamDouble = streamMessage.readDouble();
    assertTrue(streamDouble.equals(Double.valueOf(6.5)));
    Float streamFloat = streamMessage.readFloat();
    assertTrue(streamFloat.equals(Float.valueOf(93.9F)));
    assertEquals(7657, streamMessage.readInt());
    assertEquals(239999L, streamMessage.readLong());
    assertEquals((short) 34222, streamMessage.readShort());
    assertEquals("hello streammessage", streamMessage.readString());
    // bytes messages
    final byte[] bytesData = text.getBytes(StandardCharsets.UTF_8);
    sendBytesMessageUsingCoreJms(queueName, bytesData);
    BytesMessage bytesMessage = (BytesMessage) consumer.receive(5000);
    byte[] rawBytes = new byte[bytesData.length];
    bytesMessage.readBytes(rawBytes);
    for (int i = 0; i < bytesData.length; i++) {
        assertEquals("failed at " + i, bytesData[i], rawBytes[i]);
    }
    assertTrue(bytesMessage.readBoolean());
    assertEquals(99999L, bytesMessage.readLong());
    assertEquals('h', bytesMessage.readChar());
    assertEquals(987, bytesMessage.readInt());
    assertEquals((short) 1099, bytesMessage.readShort());
    assertEquals("hellobytes", bytesMessage.readUTF());
    // generic message
    sendMessageUsingCoreJms(queueName);
    javax.jms.Message genericMessage = consumer.receive(5000);
    assertEquals(destination, genericMessage.getJMSDestination());
    String value = genericMessage.getStringProperty("stringProperty");
    assertEquals("HelloMessage", value);
    assertFalse(genericMessage.getBooleanProperty("booleanProperty"));
    assertEquals(99999L, genericMessage.getLongProperty("longProperty"));
    assertEquals(979, genericMessage.getIntProperty("intProperty"));
    assertEquals((short) 1099, genericMessage.getShortProperty("shortProperty"));
    assertEquals("HelloMessage", genericMessage.getStringProperty("stringProperty"));
}
Also used : ActiveMQMessageConsumer(org.apache.activemq.ActiveMQMessageConsumer) MapMessage(javax.jms.MapMessage) BytesMessage(javax.jms.BytesMessage) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) ObjectMessage(javax.jms.ObjectMessage) StreamMessage(javax.jms.StreamMessage) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) BasicOpenWireTest(org.apache.activemq.artemis.tests.integration.openwire.BasicOpenWireTest) Test(org.junit.Test)

Aggregations

BytesMessage (javax.jms.BytesMessage)180 Session (javax.jms.Session)79 MessageProducer (javax.jms.MessageProducer)77 MessageConsumer (javax.jms.MessageConsumer)70 Test (org.junit.Test)59 Message (javax.jms.Message)54 TextMessage (javax.jms.TextMessage)47 Connection (javax.jms.Connection)41 Queue (javax.jms.Queue)37 JMSException (javax.jms.JMSException)34 MapMessage (javax.jms.MapMessage)29 ObjectMessage (javax.jms.ObjectMessage)26 StreamMessage (javax.jms.StreamMessage)20 HashMap (java.util.HashMap)18 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)17 Topic (javax.jms.Topic)14 Map (java.util.Map)12 ActiveMQMessage (org.apache.activemq.command.ActiveMQMessage)12 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)10 Destination (javax.jms.Destination)9