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();
}
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());
}
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);
}
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());
}
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);
}
Aggregations