Search in sources :

Example 61 with MessageReference

use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.

the class TransactionDetail method toJSON.

public JsonObject toJSON() throws Exception {
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM);
    JsonObjectBuilder detailJson = JsonLoader.createObjectBuilder().add(KEY_CREATION_TIME, dateFormat.format(new Date(this.creationTime))).add(KEY_XID_AS_BASE64, XidImpl.toBase64String(this.xid)).add(KEY_XID_FORMAT_ID, this.xid.getFormatId()).add(KEY_XID_GLOBAL_TXID, new String(this.xid.getGlobalTransactionId())).add(KEY_XID_BRANCH_QUAL, new String(this.xid.getBranchQualifier()));
    JsonArrayBuilder msgsJson = JsonLoader.createArrayBuilder();
    List<TransactionOperation> txops = null;
    if (this.transaction != null) {
        txops = this.transaction.getAllOperations();
    }
    if (txops == null) {
        return detailJson.build();
    }
    for (TransactionOperation op : txops) {
        String opClassName = op.getClass().getName();
        String opType = null;
        if (opClassName.equals("org.apache.activemq.artemis.core.postoffice.impl.PostOfficeImpl$AddOperation")) {
            opType = "(+) send";
        } else if (opClassName.equals("org.apache.activemq.artemis.core.server.impl.QueueImpl$RefsOperation")) {
            opType = "(-) receive";
        }
        List<MessageReference> msgs = op.getRelatedMessageReferences();
        if (msgs == null) {
            continue;
        }
        for (MessageReference ref : msgs) {
            JsonObjectBuilder msgJson = JsonLoader.createObjectBuilder();
            msgJson.add(KEY_MSG_OP_TYPE, opType);
            Message msg = ref.getMessage().copy();
            msgJson.add(KEY_MSG_TYPE, decodeMessageType(msg));
            JsonUtil.addToObject(KEY_MSG_PROPERTIES, decodeMessageProperties(msg), msgJson);
            msgsJson.add(msgJson);
        }
    }
    detailJson.add(KEY_TX_RELATED_MESSAGES, msgsJson);
    return detailJson.build();
}
Also used : Message(org.apache.activemq.artemis.api.core.Message) DateFormat(java.text.DateFormat) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) Date(java.util.Date)

Example 62 with MessageReference

use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.

the class QueueImplTest method testScheduledNoConsumer.

// The tests ----------------------------------------------------------------
@Test
public void testScheduledNoConsumer() throws Exception {
    QueueImpl queue = new QueueImpl(1, new SimpleString("address1"), new SimpleString("queue1"), null, null, false, true, false, scheduledExecutor, null, null, null, ArtemisExecutor.delegate(executor), null, null);
    // Send one scheduled
    long now = System.currentTimeMillis();
    MessageReference ref1 = generateReference(queue, 1);
    ref1.setScheduledDeliveryTime(now + 700);
    queue.addTail(ref1);
    // Send some non scheduled messages
    MessageReference ref2 = generateReference(queue, 2);
    queue.addTail(ref2);
    MessageReference ref3 = generateReference(queue, 3);
    queue.addTail(ref3);
    MessageReference ref4 = generateReference(queue, 4);
    queue.addTail(ref4);
    // Now send some more scheduled messages
    MessageReference ref5 = generateReference(queue, 5);
    ref5.setScheduledDeliveryTime(now + 500);
    queue.addTail(ref5);
    MessageReference ref6 = generateReference(queue, 6);
    ref6.setScheduledDeliveryTime(now + 400);
    queue.addTail(ref6);
    MessageReference ref7 = generateReference(queue, 7);
    ref7.setScheduledDeliveryTime(now + 300);
    queue.addTail(ref7);
    MessageReference ref8 = generateReference(queue, 8);
    ref8.setScheduledDeliveryTime(now + 600);
    queue.addTail(ref8);
    List<MessageReference> refs = new ArrayList<>();
    // Scheduled refs are added back to *FRONT* of queue - otherwise if there were many messages in the queue
    // They may get stranded behind a big backlog
    refs.add(ref1);
    refs.add(ref8);
    refs.add(ref5);
    refs.add(ref6);
    refs.add(ref7);
    refs.add(ref2);
    refs.add(ref3);
    refs.add(ref4);
    Thread.sleep(750);
    FakeConsumer consumer = new FakeConsumer();
    queue.addConsumer(consumer);
    queue.deliverNow();
    assertRefListsIdenticalRefs(refs, consumer.getReferences());
}
Also used : FakeConsumer(org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer) ArrayList(java.util.ArrayList) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) QueueImpl(org.apache.activemq.artemis.core.server.impl.QueueImpl) Test(org.junit.Test)

Example 63 with MessageReference

use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.

the class QueueImplTest method testDeliveryScheduled.

@Test
public void testDeliveryScheduled() throws Exception {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    Consumer consumer = new FakeConsumer() {

        @Override
        public synchronized HandleStatus handle(final MessageReference reference) {
            countDownLatch.countDown();
            return HandleStatus.HANDLED;
        }

        @Override
        public void disconnect() {
        }
    };
    QueueImpl queue = new QueueImpl(1, new SimpleString("address1"), QueueImplTest.queue1, null, null, false, true, false, scheduledExecutor, null, null, null, ArtemisExecutor.delegate(executor), null, null);
    MessageReference messageReference = generateReference(queue, 1);
    queue.addConsumer(consumer);
    messageReference.setScheduledDeliveryTime(System.currentTimeMillis() + 200);
    queue.addHead(messageReference, false);
    boolean gotLatch = countDownLatch.await(3000, TimeUnit.MILLISECONDS);
    Assert.assertTrue(gotLatch);
}
Also used : FakeConsumer(org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer) Consumer(org.apache.activemq.artemis.core.server.Consumer) FakeConsumer(org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) CountDownLatch(java.util.concurrent.CountDownLatch) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) QueueImpl(org.apache.activemq.artemis.core.server.impl.QueueImpl) Test(org.junit.Test)

Example 64 with MessageReference

use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.

the class QueueImplTest method testScheduled.

@Test
public void testScheduled() throws Exception {
    QueueImpl queue = new QueueImpl(1, new SimpleString("address1"), new SimpleString("queue1"), null, null, false, true, false, scheduledExecutor, null, null, null, ArtemisExecutor.delegate(executor), null, null);
    FakeConsumer consumer = null;
    // Send one scheduled
    long now = System.currentTimeMillis();
    MessageReference ref1 = generateReference(queue, 1);
    ref1.setScheduledDeliveryTime(now + 700);
    queue.addTail(ref1);
    // Send some non scheduled messages
    MessageReference ref2 = generateReference(queue, 2);
    queue.addTail(ref2);
    MessageReference ref3 = generateReference(queue, 3);
    queue.addTail(ref3);
    MessageReference ref4 = generateReference(queue, 4);
    queue.addTail(ref4);
    // Now send some more scheduled messages
    MessageReference ref5 = generateReference(queue, 5);
    ref5.setScheduledDeliveryTime(now + 500);
    queue.addTail(ref5);
    MessageReference ref6 = generateReference(queue, 6);
    ref6.setScheduledDeliveryTime(now + 400);
    queue.addTail(ref6);
    MessageReference ref7 = generateReference(queue, 7);
    ref7.setScheduledDeliveryTime(now + 300);
    queue.addTail(ref7);
    MessageReference ref8 = generateReference(queue, 8);
    ref8.setScheduledDeliveryTime(now + 600);
    queue.addTail(ref8);
    consumer = new FakeConsumer();
    queue.addConsumer(consumer);
    queue.deliverNow();
    List<MessageReference> refs = new ArrayList<>();
    refs.add(ref2);
    refs.add(ref3);
    refs.add(ref4);
    assertRefListsIdenticalRefs(refs, consumer.getReferences());
    refs.clear();
    consumer.getReferences().clear();
    MessageReference ref = consumer.waitForNextReference(QueueImplTest.TIMEOUT);
    Assert.assertEquals(ref7, ref);
    long now2 = System.currentTimeMillis();
    Assert.assertTrue(now2 - now >= 300);
    ref = consumer.waitForNextReference(QueueImplTest.TIMEOUT);
    Assert.assertEquals(ref6, ref);
    now2 = System.currentTimeMillis();
    Assert.assertTrue(now2 - now >= 400);
    ref = consumer.waitForNextReference(QueueImplTest.TIMEOUT);
    Assert.assertEquals(ref5, ref);
    now2 = System.currentTimeMillis();
    Assert.assertTrue(now2 - now >= 500);
    ref = consumer.waitForNextReference(QueueImplTest.TIMEOUT);
    Assert.assertEquals(ref8, ref);
    now2 = System.currentTimeMillis();
    Assert.assertTrue(now2 - now >= 600);
    ref = consumer.waitForNextReference(QueueImplTest.TIMEOUT);
    Assert.assertEquals(ref1, ref);
    now2 = System.currentTimeMillis();
    Assert.assertTrue(now2 - now >= 700);
    Assert.assertTrue(consumer.getReferences().isEmpty());
}
Also used : FakeConsumer(org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer) ArrayList(java.util.ArrayList) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) QueueImpl(org.apache.activemq.artemis.core.server.impl.QueueImpl) Test(org.junit.Test)

Example 65 with MessageReference

use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.

the class PostOfficeJournalLoader method handlePreparedTransaction.

@Override
public void handlePreparedTransaction(Transaction tx, List<MessageReference> referencesToAck, Xid xid, ResourceManager resourceManager) throws Exception {
    for (MessageReference ack : referencesToAck) {
        ack.getQueue().reacknowledge(tx, ack);
    }
    tx.setState(Transaction.State.PREPARED);
    resourceManager.putTransaction(xid, tx);
}
Also used : MessageReference(org.apache.activemq.artemis.core.server.MessageReference)

Aggregations

MessageReference (org.apache.activemq.artemis.core.server.MessageReference)82 ArrayList (java.util.ArrayList)29 QueueImpl (org.apache.activemq.artemis.core.server.impl.QueueImpl)26 Test (org.junit.Test)26 FakeConsumer (org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer)18 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)17 Transaction (org.apache.activemq.artemis.core.transaction.Transaction)14 Message (org.apache.activemq.artemis.api.core.Message)12 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)11 TransactionImpl (org.apache.activemq.artemis.core.transaction.impl.TransactionImpl)11 Queue (org.apache.activemq.artemis.core.server.Queue)10 HashMap (java.util.HashMap)9 NoSuchElementException (java.util.NoSuchElementException)9 Map (java.util.Map)8 Filter (org.apache.activemq.artemis.core.filter.Filter)7 LinkedList (java.util.LinkedList)5 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)5 ActiveMQIllegalStateException (org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException)4 BindingsTransactionImpl (org.apache.activemq.artemis.core.transaction.impl.BindingsTransactionImpl)4 HashSet (java.util.HashSet)3