Search in sources :

Example 41 with BytesMessage

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

the class MQTTTest method doTestJmsMapping.

// TODO As with other tests, this should be enabled as part of the cross protocol support with MQTT.
public void doTestJmsMapping(String destinationName) throws Exception {
    // start up jms consumer
    Connection jmsConn = cf.createConnection();
    Session session = jmsConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination dest = session.createQueue(destinationName);
    MessageConsumer consumer = session.createConsumer(dest);
    jmsConn.start();
    // set up mqtt producer
    MQTT mqtt = createMQTTConnection();
    mqtt.setClientId("foo3");
    mqtt.setKeepAlive((short) 2);
    final BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    int messagesToSend = 5;
    // publish
    for (int i = 0; i < messagesToSend; ++i) {
        connection.publish("test/foo", "hello world".getBytes(), QoS.AT_LEAST_ONCE, false);
    }
    connection.disconnect();
    for (int i = 0; i < messagesToSend; i++) {
        javax.jms.Message message = consumer.receive(2 * 1000);
        assertNotNull(message);
        assertTrue(message instanceof BytesMessage);
        BytesMessage bytesMessage = (BytesMessage) message;
        int length = (int) bytesMessage.getBodyLength();
        byte[] buffer = new byte[length];
        bytesMessage.readBytes(buffer);
        assertEquals("hello world", new String(buffer));
    }
    jmsConn.close();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) Connection(javax.jms.Connection) BytesMessage(javax.jms.BytesMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Session(javax.jms.Session) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) MQTTSession(org.apache.activemq.artemis.core.protocol.mqtt.MQTTSession)

Example 42 with BytesMessage

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

the class MQTTTest method doTestSendMQTTReceiveJMS.

public void doTestSendMQTTReceiveJMS(String jmsTopicAddress, String mqttAddress) throws Exception {
    final MQTTClientProvider provider = getMQTTClientProvider();
    initializeConnection(provider);
    // send retained message
    final String address = mqttAddress;
    final String RETAINED = "RETAINED";
    final byte[] payload = RETAINED.getBytes();
    Connection connection = cf.createConnection();
    // MUST set to true to receive retained messages
    connection.start();
    Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    javax.jms.Queue jmsQueue = s.createQueue(jmsTopicAddress);
    MessageConsumer consumer = s.createConsumer(jmsQueue);
    provider.publish(address, RETAINED.getBytes(), AT_LEAST_ONCE, true);
    // check whether we received retained message on JMS subscribe
    BytesMessage message = (BytesMessage) consumer.receive(5000);
    assertNotNull("Should get retained message", message);
    byte[] b = new byte[8];
    message.readBytes(b);
    assertArrayEquals(payload, b);
    for (int i = 0; i < NUM_MESSAGES; i++) {
        String p = "Test Message: " + i;
        provider.publish(address, p.getBytes(), AT_LEAST_ONCE);
        message = (BytesMessage) consumer.receive(5000);
        assertNotNull("Should get a message", message);
        byte[] bytePayload = new byte[p.getBytes().length];
        message.readBytes(bytePayload);
        assertArrayEquals(payload, b);
    }
    connection.close();
    provider.disconnect();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) Connection(javax.jms.Connection) BytesMessage(javax.jms.BytesMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Session(javax.jms.Session) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) MQTTSession(org.apache.activemq.artemis.core.protocol.mqtt.MQTTSession)

Example 43 with BytesMessage

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

the class OpenWireLargeMessageTest method testSendReceiveLargeMessage.

@Test
public void testSendReceiveLargeMessage() throws Exception {
    // Create 1MB Message
    int size = 1024 * 1024;
    byte[] bytes = new byte[size];
    try (Connection connection = factory.createConnection()) {
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(lmAddress.toString());
        MessageProducer producer = session.createProducer(queue);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        bytes[0] = 1;
        BytesMessage message = session.createBytesMessage();
        message.writeBytes(bytes);
        producer.send(message);
    }
    server.stop();
    server.start();
    try (Connection connection = factory.createConnection()) {
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(lmAddress.toString());
        MessageConsumer consumer = session.createConsumer(queue);
        BytesMessage m = (BytesMessage) consumer.receive();
        assertNotNull(m);
        byte[] body = new byte[size];
        m.readBytes(body);
        assertArrayEquals(body, bytes);
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) BytesMessage(javax.jms.BytesMessage) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) Session(javax.jms.Session) Test(org.junit.Test)

Example 44 with BytesMessage

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

the class SimpleOpenWireTest method testSendEmptyMessages.

@Test
public void testSendEmptyMessages() throws Exception {
    Queue dest = new ActiveMQQueue(queueName);
    QueueSession defaultQueueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    QueueSender defaultSender = defaultQueueSession.createSender(dest);
    defaultSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    connection.start();
    Message msg = defaultQueueSession.createMessage();
    msg.setStringProperty("testName", "testSendEmptyMessages");
    defaultSender.send(msg);
    QueueReceiver queueReceiver = defaultQueueSession.createReceiver(dest);
    assertNotNull("Didn't receive message", queueReceiver.receive(1000));
    // bytes
    BytesMessage bytesMessage = defaultQueueSession.createBytesMessage();
    bytesMessage.setStringProperty("testName", "testSendEmptyMessages");
    defaultSender.send(bytesMessage);
    assertNotNull("Didn't receive message", queueReceiver.receive(1000));
    // map
    MapMessage mapMessage = defaultQueueSession.createMapMessage();
    mapMessage.setStringProperty("testName", "testSendEmptyMessages");
    defaultSender.send(mapMessage);
    assertNotNull("Didn't receive message", queueReceiver.receive(1000));
    // object
    ObjectMessage objMessage = defaultQueueSession.createObjectMessage();
    objMessage.setStringProperty("testName", "testSendEmptyMessages");
    defaultSender.send(objMessage);
    assertNotNull("Didn't receive message", queueReceiver.receive(1000));
    // stream
    StreamMessage streamMessage = defaultQueueSession.createStreamMessage();
    streamMessage.setStringProperty("testName", "testSendEmptyMessages");
    defaultSender.send(streamMessage);
    assertNotNull("Didn't receive message", queueReceiver.receive(1000));
    // text
    TextMessage textMessage = defaultQueueSession.createTextMessage();
    textMessage.setStringProperty("testName", "testSendEmptyMessages");
    defaultSender.send(textMessage);
    assertNotNull("Didn't receive message", queueReceiver.receive(1000));
}
Also used : MapMessage(javax.jms.MapMessage) StreamMessage(javax.jms.StreamMessage) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) QueueSender(javax.jms.QueueSender) QueueReceiver(javax.jms.QueueReceiver) ObjectMessage(javax.jms.ObjectMessage) MapMessage(javax.jms.MapMessage) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) BytesMessage(javax.jms.BytesMessage) StreamMessage(javax.jms.StreamMessage) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Queue(javax.jms.Queue) TemporaryQueue(javax.jms.TemporaryQueue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 45 with BytesMessage

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

the class JMSMessageTest method testBytesMessageLength.

@Test
public void testBytesMessageLength() throws Exception {
    // Receive a message with the JMS API
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    destination = createDestination(session, destinationType);
    MessageConsumer consumer = session.createConsumer(destination);
    MessageProducer producer = session.createProducer(destination);
    // Send the message
    {
        BytesMessage message = session.createBytesMessage();
        message.writeInt(1);
        message.writeInt(2);
        message.writeInt(3);
        message.writeInt(4);
        producer.send(message);
    }
    // Check the message.
    {
        BytesMessage message = (BytesMessage) consumer.receive(1000);
        assertNotNull(message);
        assertEquals(16, message.getBodyLength());
    }
    assertNull(consumer.receiveNoWait());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) BytesMessage(javax.jms.BytesMessage) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) BasicOpenWireTest(org.apache.activemq.artemis.tests.integration.openwire.BasicOpenWireTest) Test(org.junit.Test)

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