Search in sources :

Example 6 with ActiveMQTextMessage

use of org.apache.activemq.command.ActiveMQTextMessage in project activemq-artemis by apache.

the class ActiveMQXAConnectionFactoryTest method testSessionCloseTransactionalSendReceive.

public void testSessionCloseTransactionalSendReceive() throws Exception {
    ActiveMQXAConnectionFactory cf1 = new ActiveMQXAConnectionFactory("vm://localhost?broker.persistent=false");
    XAConnection connection1 = (XAConnection) cf1.createConnection();
    connection1.start();
    XASession session = connection1.createXASession();
    XAResource resource = session.getXAResource();
    Destination dest = new ActiveMQQueue(getName());
    // publish a message
    Xid tid = createXid();
    resource.start(tid, XAResource.TMNOFLAGS);
    MessageProducer producer = session.createProducer(dest);
    ActiveMQTextMessage message = new ActiveMQTextMessage();
    message.setText(getName());
    producer.send(message);
    session.close();
    resource.end(tid, XAResource.TMSUCCESS);
    resource.commit(tid, true);
    session = connection1.createXASession();
    MessageConsumer consumer = session.createConsumer(dest);
    tid = createXid();
    resource = session.getXAResource();
    resource.start(tid, XAResource.TMNOFLAGS);
    TextMessage receivedMessage = (TextMessage) consumer.receive(1000);
    session.close();
    assertNotNull(receivedMessage);
    assertEquals(getName(), receivedMessage.getText());
    resource.end(tid, XAResource.TMSUCCESS);
    resource.commit(tid, true);
    session = connection1.createXASession();
    consumer = session.createConsumer(dest);
    tid = createXid();
    resource = session.getXAResource();
    resource.start(tid, XAResource.TMNOFLAGS);
    assertNull(consumer.receive(1000));
    resource.end(tid, XAResource.TMSUCCESS);
    resource.commit(tid, true);
}
Also used : Destination(javax.jms.Destination) XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) MessageConsumer(javax.jms.MessageConsumer) XASession(javax.jms.XASession) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) TextMessage(javax.jms.TextMessage) XAConnection(javax.jms.XAConnection) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage)

Example 7 with ActiveMQTextMessage

use of org.apache.activemq.command.ActiveMQTextMessage in project activemq-artemis by apache.

the class ActiveMQXAConnectionFactoryTest method testVanilaTransactionalProduceReceive.

public void testVanilaTransactionalProduceReceive() throws Exception {
    XAConnection connection1 = null;
    try {
        ActiveMQXAConnectionFactory cf1 = new ActiveMQXAConnectionFactory("vm://localhost?broker.persistent=false");
        connection1 = (XAConnection) cf1.createConnection();
        connection1.start();
        XASession session = connection1.createXASession();
        XAResource resource = session.getXAResource();
        Destination dest = new ActiveMQQueue(getName());
        // publish a message
        Xid tid = createXid();
        resource.start(tid, XAResource.TMNOFLAGS);
        MessageProducer producer = session.createProducer(dest);
        ActiveMQTextMessage message = new ActiveMQTextMessage();
        message.setText(getName());
        producer.send(message);
        resource.end(tid, XAResource.TMSUCCESS);
        resource.commit(tid, true);
        session.close();
        session = connection1.createXASession();
        MessageConsumer consumer = session.createConsumer(dest);
        tid = createXid();
        resource = session.getXAResource();
        resource.start(tid, XAResource.TMNOFLAGS);
        TextMessage receivedMessage = (TextMessage) consumer.receive(1000);
        assertNotNull(receivedMessage);
        assertEquals(getName(), receivedMessage.getText());
        resource.end(tid, XAResource.TMSUCCESS);
        resource.commit(tid, true);
        session.close();
    } finally {
        if (connection1 != null) {
            try {
                connection1.close();
            } catch (Exception e) {
            // ignore
            }
        }
    }
}
Also used : Destination(javax.jms.Destination) XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) MessageConsumer(javax.jms.MessageConsumer) XASession(javax.jms.XASession) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) TextMessage(javax.jms.TextMessage) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) JMSException(javax.jms.JMSException) XAException(javax.transaction.xa.XAException) XAConnection(javax.jms.XAConnection) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage)

Example 8 with ActiveMQTextMessage

use of org.apache.activemq.command.ActiveMQTextMessage in project activemq-artemis by apache.

the class CompressionOverNetworkTest method testCompressedOverCompressedNetwork.

@Test
public void testCompressedOverCompressedNetwork() throws Exception {
    ActiveMQConnection localAmqConnection = (ActiveMQConnection) localConnection;
    localAmqConnection.setUseCompression(true);
    MessageConsumer consumer1 = remoteSession.createConsumer(included);
    MessageProducer producer = localSession.createProducer(included);
    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    waitForConsumerRegistration(localBroker, 1, included);
    StringBuilder payload = new StringBuilder("test-");
    for (int i = 0; i < 100; ++i) {
        payload.append(UUID.randomUUID().toString());
    }
    Message test = localSession.createTextMessage(payload.toString());
    producer.send(test);
    Message msg = consumer1.receive(RECEIVE_TIMEOUT_MILLS);
    assertNotNull(msg);
    ActiveMQTextMessage message = (ActiveMQTextMessage) msg;
    assertTrue(message.isCompressed());
    assertEquals(payload.toString(), message.getText());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ActiveMQBytesMessage(org.apache.activemq.command.ActiveMQBytesMessage) MapMessage(javax.jms.MapMessage) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) ActiveMQObjectMessage(org.apache.activemq.command.ActiveMQObjectMessage) Message(javax.jms.Message) ActiveMQMapMessage(org.apache.activemq.command.ActiveMQMapMessage) StreamMessage(javax.jms.StreamMessage) BytesMessage(javax.jms.BytesMessage) ActiveMQStreamMessage(org.apache.activemq.command.ActiveMQStreamMessage) ActiveMQConnection(org.apache.activemq.ActiveMQConnection) MessageProducer(javax.jms.MessageProducer) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) Test(org.junit.Test)

Example 9 with ActiveMQTextMessage

use of org.apache.activemq.command.ActiveMQTextMessage in project activemq-artemis by apache.

the class BrokerNetworkWithStuckMessagesTest method createMessage.

protected Message createMessage(ProducerInfo producerInfo, ActiveMQDestination destination) {
    ActiveMQTextMessage message = new ActiveMQTextMessage();
    message.setMessageId(new MessageId(producerInfo, ++msgIdGenerator));
    message.setDestination(destination);
    message.setPersistent(false);
    try {
        message.setText("Test Message Payload.");
    } catch (MessageNotWriteableException e) {
    }
    return message;
}
Also used : MessageNotWriteableException(javax.jms.MessageNotWriteableException) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) MessageId(org.apache.activemq.command.MessageId)

Example 10 with ActiveMQTextMessage

use of org.apache.activemq.command.ActiveMQTextMessage in project activemq-artemis by apache.

the class NetworkLoadTest method testRequestReply.

public void testRequestReply() throws Exception {
    // Send to the first broker
    final int to = 0;
    // consume from the last broker..
    int from = brokers.length - 1;
    LOG.info("Staring Final Consumer");
    Connection fromConnection = createConnection(from);
    fromConnection.start();
    Session fromSession = fromConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = fromSession.createConsumer(new ActiveMQQueue("Q" + from));
    final AtomicReference<ActiveMQTextMessage> lastMessageReceived = new AtomicReference<>();
    final AtomicLong producedMessages = new AtomicLong();
    final AtomicLong receivedMessages = new AtomicLong();
    final AtomicBoolean done = new AtomicBoolean();
    // Setup the consumer..
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message msg) {
            ActiveMQTextMessage m = (ActiveMQTextMessage) msg;
            ActiveMQTextMessage last = lastMessageReceived.get();
            if (last != null) {
                // Some order checking...
                if (last.getMessageId().getProducerSequenceId() > m.getMessageId().getProducerSequenceId()) {
                    System.out.println("Received an out of order message. Got " + m.getMessageId() + ", expected something after " + last.getMessageId());
                }
            }
            lastMessageReceived.set(m);
            receivedMessages.incrementAndGet();
        }
    });
    LOG.info("Staring Initial Producer");
    final Connection toConnection = createConnection(to);
    Thread producer = new Thread("Producer") {

        @Override
        public void run() {
            try {
                toConnection.start();
                Session toSession = toConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                final MessageProducer producer = toSession.createProducer(new ActiveMQQueue("Q" + to));
                producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
                producer.setDisableMessageID(true);
                for (int i = 0; !done.get(); i++) {
                    TextMessage msg = toSession.createTextMessage(createMessageText(i));
                    producer.send(msg);
                    producedMessages.incrementAndGet();
                }
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }

        private String createMessageText(int index) {
            StringBuffer buffer = new StringBuffer(MESSAGE_SIZE);
            buffer.append(index + " on " + new Date() + " ...");
            if (buffer.length() > MESSAGE_SIZE) {
                return buffer.substring(0, MESSAGE_SIZE);
            }
            for (int i = buffer.length(); i < MESSAGE_SIZE; i++) {
                buffer.append(' ');
            }
            return buffer.toString();
        }
    };
    producer.start();
    // Give the forwarding clients a chance to get going and fill the down
    // stream broker queues..
    Thread.sleep(BROKER_COUNT * 200);
    for (int i = 0; i < SAMPLES; i++) {
        long start = System.currentTimeMillis();
        producedMessages.set(0);
        receivedMessages.set(0);
        for (int j = 0; j < forwardingClients.length; j++) {
            forwardingClients[j].forwardCounter.set(0);
        }
        Thread.sleep(SAMPLE_DURATION);
        long end = System.currentTimeMillis();
        long r = receivedMessages.get();
        long p = producedMessages.get();
        LOG.info("published: " + p + " msgs at " + (p * 1000f / (end - start)) + " msgs/sec, " + "consumed: " + r + " msgs at " + (r * 1000f / (end - start)) + " msgs/sec");
        StringBuffer fwdingmsg = new StringBuffer(500);
        fwdingmsg.append("  forwarding counters: ");
        for (int j = 0; j < forwardingClients.length; j++) {
            if (j != 0) {
                fwdingmsg.append(", ");
            }
            fwdingmsg.append(forwardingClients[j].forwardCounter.get());
        }
        LOG.info(fwdingmsg.toString());
        // The test is just checking to make sure thaat the producer and consumer does not hang
        // due to the network hops take to route the message form the producer to the consumer.
        assertTrue("Received some messages since last sample", r > 0);
        assertTrue("Produced some messages since last sample", p > 0);
    }
    LOG.info("Sample done.");
    done.set(true);
    // Wait for the producer to finish.
    producer.join(1000 * 5);
    toConnection.close();
    fromConnection.close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) JMSException(javax.jms.JMSException) Date(java.util.Date) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Aggregations

ActiveMQTextMessage (org.apache.activemq.command.ActiveMQTextMessage)71 Test (org.junit.Test)36 TextMessage (javax.jms.TextMessage)16 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)14 Message (javax.jms.Message)10 MessageProducer (javax.jms.MessageProducer)10 MessageConsumer (javax.jms.MessageConsumer)9 MessageId (org.apache.activemq.command.MessageId)9 JMSException (javax.jms.JMSException)7 ActiveMQBytesMessage (org.apache.activemq.command.ActiveMQBytesMessage)7 DestinationStatistics (org.apache.activemq.broker.region.DestinationStatistics)6 MessageReference (org.apache.activemq.broker.region.MessageReference)6 Queue (org.apache.activemq.broker.region.Queue)6 ActiveMQMapMessage (org.apache.activemq.command.ActiveMQMapMessage)6 ActiveMQObjectMessage (org.apache.activemq.command.ActiveMQObjectMessage)6 ActiveMQStreamMessage (org.apache.activemq.command.ActiveMQStreamMessage)6 ConsumerInfo (org.apache.activemq.command.ConsumerInfo)6 SystemUsage (org.apache.activemq.usage.SystemUsage)6 MutableSpan (brave.handler.MutableSpan)5 MessageNotWriteableException (javax.jms.MessageNotWriteableException)5