Search in sources :

Example 71 with BytesMessage

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

the class MessageTypeTest method testBytesMessage_1.

/**
 * Send a <code>BytesMessage</code> with an empty body.
 * <br />
 * Receive it and test if the message is effectively an instance of
 * <code>BytesMessage</code>
 */
@Test
public void testBytesMessage_1() {
    try {
        BytesMessage message = senderSession.createBytesMessage();
        sender.send(message);
        Message msg = receiver.receive(TestConfig.TIMEOUT);
        Assert.assertTrue("The message should be an instance of BytesMessage.\n", msg instanceof BytesMessage);
    } catch (JMSException e) {
        fail(e);
    }
}
Also used : TextMessage(javax.jms.TextMessage) StreamMessage(javax.jms.StreamMessage) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) BytesMessage(javax.jms.BytesMessage) JMSException(javax.jms.JMSException) Test(org.junit.Test)

Example 72 with BytesMessage

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

the class ReplicationFlowControlTest method internalTest.

private void internalTest(boolean failover) throws Exception {
    int KILL_SERVER = failover ? 50_000 : -1;
    Connection connection = null;
    try {
        server0 = startServer(SERVER_NAME_0, 0, 30000);
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connection = connectionFactory.createConnection();
        Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
        Queue queue = session.createQueue("exampleQueue");
        MessageProducer producer = session.createProducer(queue);
        BytesMessage bytesMessage = session.createBytesMessage();
        bytesMessage.writeBytes(new byte[20 * 1024]);
        for (int i = 0; i < NUM_MESSAGES; i++) {
            producer.send(bytesMessage);
            if (i % 1000 == 0) {
                System.out.println("Sent " + i + " messages, consumed=" + totalConsumed.get());
                session.commit();
            }
            if (i == START_CONSUMERS) {
                System.out.println("Starting consumers");
                // if failover, no AMQP
                startConsumers(!failover);
            }
            if (KILL_SERVER >= 0 && i == KILL_SERVER) {
                session.commit();
                System.out.println("Killing server");
                ServerUtil.killServer(server0);
                Thread.sleep(2000);
                connection.close();
                connection = connectionFactory.createConnection();
                session = connection.createSession(true, Session.SESSION_TRANSACTED);
                queue = session.createQueue("exampleQueue");
                producer = session.createProducer(queue);
            }
            if (i == START_SERVER) {
                System.out.println("Starting extra server");
                server1 = startServer(SERVER_NAME_1, 0, 30000);
            }
        }
        session.commit();
        System.out.println("Awaiting all consumers to finish");
        while (!latch.await(10, TimeUnit.SECONDS)) {
            fail("couldn't receive all messages");
        }
        running.set(false);
        for (Consumer consumer : consumers) {
            consumer.join(10000);
            if (consumer.isAlive()) {
                consumer.interrupt();
            }
        }
    } finally {
        running.set(false);
        if (connection != null) {
            connection.close();
        }
        for (Consumer consumer : consumers) {
            consumer.interrupt();
            consumer.join();
        }
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) JmsConnectionFactory(org.apache.qpid.jms.JmsConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) BytesMessage(javax.jms.BytesMessage) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) Session(javax.jms.Session)

Example 73 with BytesMessage

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

the class StompTest 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 };
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(data);
    baos.flush();
    ClientStompFrame frame = conn.createFrame(Stomp.Commands.SEND).addHeader(Stomp.Headers.Send.DESTINATION, getQueuePrefix() + getQueueName()).addHeader(Stomp.Headers.CONTENT_LENGTH, Integer.toString(data.length)).setBody(new String(baos.toByteArray()));
    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) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Test(org.junit.Test)

Example 74 with BytesMessage

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

the class StompV12Test method testSubscribeWithMessageSentWithProperties.

@Test
public void testSubscribeWithMessageSentWithProperties() throws Exception {
    conn.connect(defUser, defPass);
    subscribe(conn, "sub1", Stomp.Headers.Subscribe.AckModeValues.AUTO);
    MessageProducer producer = session.createProducer(queue);
    BytesMessage message = session.createBytesMessage();
    message.setStringProperty("S", "value");
    message.setBooleanProperty("n", false);
    message.setByteProperty("byte", (byte) 9);
    message.setDoubleProperty("d", 2.0);
    message.setFloatProperty("f", (float) 6.0);
    message.setIntProperty("i", 10);
    message.setLongProperty("l", 121);
    message.setShortProperty("s", (short) 12);
    message.writeBytes("Hello World".getBytes(StandardCharsets.UTF_8));
    producer.send(message);
    ClientStompFrame frame = conn.receiveFrame();
    Assert.assertNotNull(frame);
    Assert.assertTrue(frame.getHeader("S") != null);
    Assert.assertTrue(frame.getHeader("n") != null);
    Assert.assertTrue(frame.getHeader("byte") != null);
    Assert.assertTrue(frame.getHeader("d") != null);
    Assert.assertTrue(frame.getHeader("f") != null);
    Assert.assertTrue(frame.getHeader("i") != null);
    Assert.assertTrue(frame.getHeader("l") != null);
    Assert.assertTrue(frame.getHeader("s") != null);
    Assert.assertEquals("Hello World", frame.getBody());
    conn.disconnect();
}
Also used : ClientStompFrame(org.apache.activemq.artemis.tests.integration.stomp.util.ClientStompFrame) BytesMessage(javax.jms.BytesMessage) MessageProducer(javax.jms.MessageProducer) Test(org.junit.Test)

Example 75 with BytesMessage

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

the class BytesMessageTest method prepareMessage.

// Protected -----------------------------------------------------
@Override
protected void prepareMessage(final Message m) throws JMSException {
    super.prepareMessage(m);
    BytesMessage bm = (BytesMessage) m;
    bm.writeBoolean(true);
    bm.writeByte((byte) 3);
    bm.writeBytes(new byte[] { (byte) 4, (byte) 5, (byte) 6 });
    bm.writeChar((char) 7);
    bm.writeDouble(8.0);
    bm.writeFloat(9.0f);
    bm.writeInt(10);
    bm.writeLong(11L);
    bm.writeShort((short) 12);
    bm.writeUTF("this is an UTF String");
    bm.reset();
}
Also used : BytesMessage(javax.jms.BytesMessage)

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