Search in sources :

Example 61 with BytesMessage

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

the class JMSFailoverTest method testAutomaticFailover.

@Test
public void testAutomaticFailover() throws Exception {
    ActiveMQConnectionFactory jbcf = ActiveMQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.CF, livetc);
    jbcf.setReconnectAttempts(-1);
    jbcf.setBlockOnDurableSend(true);
    jbcf.setBlockOnNonDurableSend(true);
    // Note we set consumer window size to a value so we can verify that consumer credit re-sending
    // works properly on failover
    // The value is small enough that credits will have to be resent several time
    final int numMessages = 10;
    final int bodySize = 1000;
    jbcf.setConsumerWindowSize(numMessages * bodySize / 10);
    Connection conn = JMSUtil.createConnectionAndWaitForTopology(jbcf, 2, 5);
    MyExceptionListener listener = new MyExceptionListener();
    conn.setExceptionListener(listener);
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ClientSession coreSession = ((ActiveMQSession) sess).getCoreSession();
    SimpleString jmsQueueName = new SimpleString("myqueue");
    coreSession.createQueue(jmsQueueName, RoutingType.ANYCAST, jmsQueueName, null, true);
    Queue queue = sess.createQueue("myqueue");
    MessageProducer producer = sess.createProducer(queue);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);
    MessageConsumer consumer = sess.createConsumer(queue);
    byte[] body = RandomUtil.randomBytes(bodySize);
    for (int i = 0; i < numMessages; i++) {
        BytesMessage bm = sess.createBytesMessage();
        bm.writeBytes(body);
        producer.send(bm);
    }
    conn.start();
    JMSFailoverTest.log.info("sent messages and started connection");
    Thread.sleep(2000);
    JMSUtil.crash(liveServer, ((ActiveMQSession) sess).getCoreSession());
    for (int i = 0; i < numMessages; i++) {
        JMSFailoverTest.log.info("got message " + i);
        BytesMessage bm = (BytesMessage) consumer.receive(1000);
        Assert.assertNotNull(bm);
        Assert.assertEquals(body.length, bm.getBodyLength());
    }
    TextMessage tm = (TextMessage) consumer.receiveNoWait();
    Assert.assertNull(tm);
    conn.close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) Connection(javax.jms.Connection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) BytesMessage(javax.jms.BytesMessage) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Test(org.junit.Test)

Example 62 with BytesMessage

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

the class LargeMessageOverBridgeTest method testSendBytesAsLargeOnBridgeOnly.

/**
 * This was causing a text message to ber eventually converted into large message when sent over the bridge
 *
 * @throws Exception
 */
@Test
public void testSendBytesAsLargeOnBridgeOnly() throws Exception {
    createQueue(QUEUE);
    Queue queue = (Queue) context1.lookup("queue/" + QUEUE);
    Connection conn1 = cf1.createConnection();
    Session session1 = conn1.createSession(true, Session.SESSION_TRANSACTED);
    MessageProducer prod1 = session1.createProducer(queue);
    Connection conn2 = cf2.createConnection();
    Session session2 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer cons2 = session2.createConsumer(queue);
    conn2.start();
    byte[] bytes = new byte[10 * 1024];
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = getSamplebyte(i);
    }
    for (int i = 0; i < 10; i++) {
        BytesMessage msg = session1.createBytesMessage();
        msg.writeBytes(bytes);
        prod1.send(msg);
    }
    session1.commit();
    for (int i = 0; i < 5; i++) {
        BytesMessage msg2 = (BytesMessage) cons2.receive(5000);
        assertNotNull(msg2);
        msg2.acknowledge();
        for (int j = 0; j < bytes.length; j++) {
            assertEquals("Position " + i, msg2.readByte(), bytes[j]);
        }
    }
    conn1.close();
    conn2.close();
}
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 63 with BytesMessage

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

the class LargeMessageOverBridgeTest method testSendLargeForBridge.

/**
 * The message won't be large to the client while it will be considered large through the bridge
 *
 * @throws Exception
 */
@Test
public void testSendLargeForBridge() throws Exception {
    createQueue(QUEUE);
    Queue queue = (Queue) context1.lookup("queue/" + QUEUE);
    ActiveMQConnectionFactory cf1 = ActiveMQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY, generateInVMParams(1)));
    cf1.setMinLargeMessageSize(200 * 1024);
    Connection conn1 = cf1.createConnection();
    Session session1 = conn1.createSession(true, Session.SESSION_TRANSACTED);
    MessageProducer prod1 = session1.createProducer(queue);
    Connection conn2 = cf2.createConnection();
    Session session2 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer cons2 = session2.createConsumer(queue);
    conn2.start();
    byte[] bytes = new byte[150 * 1024];
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = getSamplebyte(i);
    }
    for (int i = 0; i < 10; i++) {
        BytesMessage msg = session1.createBytesMessage();
        msg.writeBytes(bytes);
        prod1.send(msg);
    }
    session1.commit();
    for (int i = 0; i < 5; i++) {
        BytesMessage msg2 = (BytesMessage) cons2.receive(5000);
        assertNotNull(msg2);
        msg2.acknowledge();
        for (int j = 0; j < bytes.length; j++) {
            assertEquals("Position " + i, msg2.readByte(), bytes[j]);
        }
    }
    conn1.close();
    conn2.close();
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) BytesMessage(javax.jms.BytesMessage) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) Session(javax.jms.Session) Test(org.junit.Test)

Example 64 with BytesMessage

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

the class BodyTest method testBodyConversion.

@Test
public void testBodyConversion() throws Throwable {
    try (Connection conn = cf.createConnection()) {
        Session sess = conn.createSession();
        MessageProducer producer = sess.createProducer(queue);
        MessageConsumer cons = sess.createConsumer(queue);
        conn.start();
        BytesMessage bytesMessage = sess.createBytesMessage();
        BytesMessage bytesMessage2 = sess.createBytesMessage();
        bytesMessage2.writeInt(42);
        bytesMessage2.reset();
        producer.send(bytesMessage);
        Message msg = cons.receiveNoWait();
        producer.send(bytesMessage2);
        Message msg2 = cons.receiveNoWait();
        assertNotNull(msg);
        assertNotNull(msg2);
        // message body is empty. getBody parameter may be set to any type
        Assert.assertNull(msg.getBody(java.lang.Object.class));
        Assert.assertNull(msg.getBody(byte[].class));
        Assert.assertNull(msg.getBody(String.class));
        // message body is not empty. getBody parameter must be set to byte[].class (or java.lang.Object.class)
        Assert.assertNotNull(msg2.getBody(byte[].class));
        Assert.assertNotNull(msg2.getBody(java.lang.Object.class));
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) BytesMessage(javax.jms.BytesMessage) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) Test(org.junit.Test)

Example 65 with BytesMessage

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

the class JmsContextTest method testReceiveBytes.

@Test
public void testReceiveBytes() throws Exception {
    JMSProducer producer = context.createProducer();
    JMSConsumer consumer = context.createConsumer(queue1);
    BytesMessage bytesSend = context.createBytesMessage();
    bytesSend.writeByte((byte) 1);
    bytesSend.writeLong(2L);
    producer.send(queue1, bytesSend);
    BytesMessage msgReceived = (BytesMessage) consumer.receiveNoWait();
    byte[] bytesArray = msgReceived.getBody(byte[].class);
    assertEquals((byte) 1, msgReceived.readByte());
    assertEquals(2L, msgReceived.readLong());
    DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(bytesArray));
    assertEquals((byte) 1, dataInputStream.readByte());
    assertEquals(2L, dataInputStream.readLong());
}
Also used : JMSConsumer(javax.jms.JMSConsumer) ByteArrayInputStream(java.io.ByteArrayInputStream) JMSProducer(javax.jms.JMSProducer) BytesMessage(javax.jms.BytesMessage) DataInputStream(java.io.DataInputStream) 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