Search in sources :

Example 36 with BytesMessage

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

the class AMQPToOpenwireTest method testObjectMessage.

@Test
public void testObjectMessage() throws Exception {
    Connection connection = null;
    try {
        connection = qpidfactory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(coreQueue.toString());
        MessageProducer producer = session.createProducer(queue);
        ArrayList list = new ArrayList();
        list.add("aString");
        ObjectMessage objectMessage = session.createObjectMessage(list);
        producer.send(objectMessage);
        connection.close();
        connection = factory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        queue = session.createQueue(queueName);
        MessageConsumer consumer = session.createConsumer(queue);
        connection.start();
        BytesMessage receive = (BytesMessage) consumer.receive(5000);
        assertNotNull(receive);
        byte[] bytes = new byte[(int) receive.getBodyLength()];
        receive.readBytes(bytes);
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
        list = (ArrayList) ois.readObject();
        assertEquals(list.get(0), "aString");
        connection.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectMessage(javax.jms.ObjectMessage) AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) Connection(javax.jms.Connection) ArrayList(java.util.ArrayList) BytesMessage(javax.jms.BytesMessage) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) Session(javax.jms.Session) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 37 with BytesMessage

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

the class JMSFailoverListenerTest method testAutomaticFailover.

// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@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);
    ActiveMQConnection conn = JMSUtil.createConnectionAndWaitForTopology(jbcf, 2, 5);
    MyFailoverListener listener = new MyFailoverListener();
    conn.setFailoverListener(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();
    JMSFailoverListenerTest.log.info("sent messages and started connection");
    Thread.sleep(2000);
    JMSUtil.crash(liveServer, ((ActiveMQSession) sess).getCoreSession());
    Assert.assertEquals(FailoverEventType.FAILURE_DETECTED, listener.get(0));
    for (int i = 0; i < numMessages; i++) {
        JMSFailoverListenerTest.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);
    Assert.assertEquals(FailoverEventType.FAILOVER_COMPLETED, listener.get(1));
    conn.close();
    Assert.assertEquals("Expected 2 FailoverEvents to be triggered", 2, listener.size());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) 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 38 with BytesMessage

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

the class JMSReconnectTest method testReconnectSameNodeServerRestartedWithNonDurableSubOrTempQueue.

// Test that non durable JMS sub gets recreated in auto reconnect
private void testReconnectSameNodeServerRestartedWithNonDurableSubOrTempQueue(final boolean nonDurableSub) throws Exception {
    ActiveMQConnectionFactory jbcf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY));
    jbcf.setReconnectAttempts(-1);
    Connection conn = jbcf.createConnection();
    MyExceptionListener listener = new MyExceptionListener();
    conn.setExceptionListener(listener);
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ClientSession coreSession = ((ActiveMQSession) sess).getCoreSession();
    Destination dest;
    if (nonDurableSub) {
        coreSession.createQueue("mytopic", "blahblah", null, false);
        dest = ActiveMQJMSClient.createTopic("mytopic");
    } else {
        dest = sess.createTemporaryQueue();
    }
    MessageProducer producer = sess.createProducer(dest);
    // Create a non durable subscriber
    MessageConsumer consumer = sess.createConsumer(dest);
    this.server.stop();
    this.server.start();
    // Allow client some time to reconnect
    Thread.sleep(3000);
    final int numMessages = 100;
    byte[] body = RandomUtil.randomBytes(1000);
    for (int i = 0; i < numMessages; i++) {
        BytesMessage bm = sess.createBytesMessage();
        bm.writeBytes(body);
        producer.send(bm);
    }
    conn.start();
    for (int i = 0; i < numMessages; 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();
    Assert.assertNotNull(listener.e);
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) Connection(javax.jms.Connection) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) 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) 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)

Example 39 with BytesMessage

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

the class ReSendMessageTest method testResendWithLargeMessage.

// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testResendWithLargeMessage() throws Exception {
    conn = cf.createConnection();
    conn.start();
    Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
    ArrayList<Message> msgs = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        BytesMessage bm = sess.createBytesMessage();
        bm.setObjectProperty(ActiveMQJMSConstants.JMS_ACTIVEMQ_INPUT_STREAM, ActiveMQTestBase.createFakeLargeStream(2 * ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE));
        msgs.add(bm);
        MapMessage mm = sess.createMapMessage();
        mm.setBoolean("boolean", true);
        mm.setByte("byte", (byte) 3);
        mm.setBytes("bytes", new byte[] { (byte) 3, (byte) 4, (byte) 5 });
        mm.setChar("char", (char) 6);
        mm.setDouble("double", 7.0);
        mm.setFloat("float", 8.0f);
        mm.setInt("int", 9);
        mm.setLong("long", 10L);
        mm.setObject("object", new String("this is an object"));
        mm.setShort("short", (short) 11);
        mm.setString("string", "this is a string");
        msgs.add(mm);
        msgs.add(sess.createTextMessage("hello" + i));
        msgs.add(sess.createObjectMessage(new SomeSerializable("hello" + i)));
    }
    internalTestResend(msgs, sess);
}
Also used : TextMessage(javax.jms.TextMessage) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) MapMessage(javax.jms.MapMessage) ArrayList(java.util.ArrayList) BytesMessage(javax.jms.BytesMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 40 with BytesMessage

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

the class MessageCompressionTest method sendTestBytesMessage.

private void sendTestBytesMessage(ActiveMQConnectionFactory factory, String message) throws JMSException, UnsupportedEncodingException {
    ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = session.createProducer(queue);
    BytesMessage bytesMessage = session.createBytesMessage();
    bytesMessage.writeBytes(message.getBytes(StandardCharsets.UTF_8));
    producer.send(bytesMessage);
    connection.close();
}
Also used : ActiveMQConnection(org.apache.activemq.ActiveMQConnection) BytesMessage(javax.jms.BytesMessage) MessageProducer(javax.jms.MessageProducer) 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