Search in sources :

Example 41 with Message

use of org.apache.activemq.artemis.api.core.Message in project activemq-artemis by apache.

the class QueueImpl method move.

private void move(final SimpleString toAddress, final Transaction tx, final MessageReference ref, final boolean expiry, final boolean rejectDuplicate, final long... queueIDs) throws Exception {
    Message copyMessage = makeCopy(ref, expiry);
    copyMessage.setAddress(toAddress);
    if (queueIDs != null && queueIDs.length > 0) {
        ByteBuffer buffer = ByteBuffer.allocate(8 * queueIDs.length);
        for (long id : queueIDs) {
            buffer.putLong(id);
        }
        copyMessage.putBytesProperty(Message.HDR_ROUTE_TO_IDS.toString(), buffer.array());
    }
    postOffice.route(copyMessage, tx, false, rejectDuplicate);
    if (expiry) {
        acknowledge(tx, ref, AckReason.EXPIRED);
    } else {
        acknowledge(tx, ref);
    }
}
Also used : Message(org.apache.activemq.artemis.api.core.Message) ByteBuffer(java.nio.ByteBuffer)

Example 42 with Message

use of org.apache.activemq.artemis.api.core.Message in project activemq-artemis by apache.

the class ScaleDownHandler method scaleDownTransactions.

public void scaleDownTransactions(ClientSessionFactory sessionFactory, ResourceManager resourceManager, String user, String password) throws Exception {
    ClientSession session = sessionFactory.createSession(user, password, true, false, false, false, 0);
    ClientSession queueCreateSession = sessionFactory.createSession(user, password, false, true, true, false, 0);
    List<Xid> preparedTransactions = resourceManager.getPreparedTransactions();
    Map<String, Long> queueIDs = new HashMap<>();
    for (Xid xid : preparedTransactions) {
        logger.debug("Scaling down transaction: " + xid);
        Transaction transaction = resourceManager.getTransaction(xid);
        session.start(xid, XAResource.TMNOFLAGS);
        List<TransactionOperation> allOperations = transaction.getAllOperations();
        // Get the information of the Prepared TXs so it could replay the TXs
        Map<Message, Pair<List<Long>, List<Long>>> queuesToSendTo = new HashMap<>();
        for (TransactionOperation operation : allOperations) {
            if (operation instanceof PostOfficeImpl.AddOperation) {
                PostOfficeImpl.AddOperation addOperation = (PostOfficeImpl.AddOperation) operation;
                List<MessageReference> refs = addOperation.getRelatedMessageReferences();
                for (MessageReference ref : refs) {
                    Message message = ref.getMessage();
                    Queue queue = ref.getQueue();
                    long queueID;
                    String queueName = queue.getName().toString();
                    if (queueIDs.containsKey(queueName)) {
                        queueID = queueIDs.get(queueName);
                    } else {
                        queueID = createQueueIfNecessaryAndGetID(queueCreateSession, queue, message.getAddressSimpleString());
                        // store it so we don't have to look it up every time
                        queueIDs.put(queueName, queueID);
                    }
                    Pair<List<Long>, List<Long>> queueIds = queuesToSendTo.get(message);
                    if (queueIds == null) {
                        queueIds = new Pair<List<Long>, List<Long>>(new ArrayList<Long>(), new ArrayList<Long>());
                        queuesToSendTo.put(message, queueIds);
                    }
                    queueIds.getA().add(queueID);
                }
            } else if (operation instanceof RefsOperation) {
                RefsOperation refsOperation = (RefsOperation) operation;
                List<MessageReference> refs = refsOperation.getReferencesToAcknowledge();
                for (MessageReference ref : refs) {
                    Message message = ref.getMessage();
                    Queue queue = ref.getQueue();
                    long queueID;
                    String queueName = queue.getName().toString();
                    if (queueIDs.containsKey(queueName)) {
                        queueID = queueIDs.get(queueName);
                    } else {
                        queueID = createQueueIfNecessaryAndGetID(queueCreateSession, queue, message.getAddressSimpleString());
                        // store it so we don't have to look it up every time
                        queueIDs.put(queueName, queueID);
                    }
                    Pair<List<Long>, List<Long>> queueIds = queuesToSendTo.get(message);
                    if (queueIds == null) {
                        queueIds = new Pair<List<Long>, List<Long>>(new ArrayList<Long>(), new ArrayList<Long>());
                        queuesToSendTo.put(message, queueIds);
                    }
                    queueIds.getA().add(queueID);
                    queueIds.getB().add(queueID);
                }
            }
        }
        ClientProducer producer = session.createProducer();
        for (Map.Entry<Message, Pair<List<Long>, List<Long>>> entry : queuesToSendTo.entrySet()) {
            List<Long> ids = entry.getValue().getA();
            ByteBuffer buffer = ByteBuffer.allocate(ids.size() * 8);
            for (Long id : ids) {
                buffer.putLong(id);
            }
            Message message = entry.getKey();
            message.putBytesProperty(Message.HDR_ROUTE_TO_IDS.toString(), buffer.array());
            ids = entry.getValue().getB();
            if (ids.size() > 0) {
                buffer = ByteBuffer.allocate(ids.size() * 8);
                for (Long id : ids) {
                    buffer.putLong(id);
                }
                message.putBytesProperty(Message.HDR_ROUTE_TO_ACK_IDS.toString(), buffer.array());
            }
            producer.send(message.getAddressSimpleString().toString(), message);
        }
        session.end(xid, XAResource.TMSUCCESS);
        session.prepare(xid);
    }
}
Also used : ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(org.apache.activemq.artemis.api.core.Message) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ArrayList(java.util.ArrayList) List(java.util.List) Queue(org.apache.activemq.artemis.core.server.Queue) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Pair(org.apache.activemq.artemis.api.core.Pair) PostOfficeImpl(org.apache.activemq.artemis.core.postoffice.impl.PostOfficeImpl) TransactionOperation(org.apache.activemq.artemis.core.transaction.TransactionOperation) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) ByteBuffer(java.nio.ByteBuffer) Xid(javax.transaction.xa.Xid) Transaction(org.apache.activemq.artemis.core.transaction.Transaction) HashMap(java.util.HashMap) Map(java.util.Map)

Example 43 with Message

use of org.apache.activemq.artemis.api.core.Message in project activemq-artemis by apache.

the class ScaleDownHandler method scaleDownSNF.

private long scaleDownSNF(final SimpleString address, final Set<Queue> queues, final ClientProducer producer) throws Exception {
    long messageCount = 0;
    final String propertyEnd;
    // If this SNF is towards our targetNodeId
    boolean queueOnTarget = address.toString().endsWith(targetNodeId);
    if (queueOnTarget) {
        propertyEnd = targetNodeId;
    } else {
        propertyEnd = address.toString().substring(address.toString().lastIndexOf("."));
    }
    Transaction tx = new TransactionImpl(storageManager);
    for (Queue queue : queues) {
        // using auto-closeable
        try (LinkedListIterator<MessageReference> messagesIterator = queue.browserIterator()) {
            // loop through every message of this queue
            while (messagesIterator.hasNext()) {
                MessageReference messageRef = messagesIterator.next();
                Message message = messageRef.getMessage().copy();
                /* Here we are taking messages out of a store-and-forward queue and sending them to the corresponding
                * address on the scale-down target server.  However, we have to take the existing _AMQ_ROUTE_TOsf.*
                * property and put its value into the _AMQ_ROUTE_TO property so the message is routed properly.
                */
                byte[] oldRouteToIDs = null;
                List<SimpleString> propertiesToRemove = new ArrayList<>();
                message.removeProperty(Message.HDR_ROUTE_TO_IDS.toString());
                for (SimpleString propName : message.getPropertyNames()) {
                    if (propName.startsWith(Message.HDR_ROUTE_TO_IDS)) {
                        if (propName.toString().endsWith(propertyEnd)) {
                            oldRouteToIDs = message.getBytesProperty(propName.toString());
                        }
                        propertiesToRemove.add(propName);
                    }
                }
                for (SimpleString propertyToRemove : propertiesToRemove) {
                    message.removeProperty(propertyToRemove.toString());
                }
                if (queueOnTarget) {
                    message.putBytesProperty(Message.HDR_ROUTE_TO_IDS.toString(), oldRouteToIDs);
                } else {
                    message.putBytesProperty(Message.HDR_SCALEDOWN_TO_IDS.toString(), oldRouteToIDs);
                }
                logger.debug("Scaling down message " + message + " from " + address + " to " + message.getAddress() + " on node " + targetNodeId);
                producer.send(message.getAddress(), message);
                messageCount++;
                messagesIterator.remove();
                ackMessageOnQueue(tx, queue, messageRef);
            }
        } catch (NoSuchElementException ignored) {
        // this could happen through paging browsing
        }
    }
    tx.commit();
    return messageCount;
}
Also used : ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(org.apache.activemq.artemis.api.core.Message) ArrayList(java.util.ArrayList) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) TransactionImpl(org.apache.activemq.artemis.core.transaction.impl.TransactionImpl) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) Transaction(org.apache.activemq.artemis.core.transaction.Transaction) Queue(org.apache.activemq.artemis.core.server.Queue) NoSuchElementException(java.util.NoSuchElementException)

Example 44 with Message

use of org.apache.activemq.artemis.api.core.Message in project activemq-artemis by apache.

the class LoggingActiveMQServerPlugin method messageAcknowledged.

/**
 * A message has been acknowledged
 *
 * @param ref    The acked message
 * @param reason The ack reason
 * @throws ActiveMQException
 */
@Override
public void messageAcknowledged(MessageReference ref, AckReason reason) throws ActiveMQException {
    if (logAll || logDeliveringEvents) {
        // details - debug logging
        LoggingActiveMQServerPluginLogger.LOGGER.messageAcknowledgedDetails(ref, reason);
        if (LoggingActiveMQServerPluginLogger.LOGGER.isInfoEnabled()) {
            Message message = (ref == null ? null : ref.getMessage());
            Queue queue = (ref == null ? null : ref.getQueue());
            // info level logging
            LoggingActiveMQServerPluginLogger.LOGGER.messageAcknowledged((message == null ? UNAVAILABLE : Long.toString(message.getMessageID())), (ref == null ? UNAVAILABLE : ref.hasConsumerId() ? Long.toString(ref.getConsumerId()) : null), (queue == null ? UNAVAILABLE : queue.getName().toString()), reason);
        }
    }
}
Also used : Message(org.apache.activemq.artemis.api.core.Message) Queue(org.apache.activemq.artemis.core.server.Queue)

Example 45 with Message

use of org.apache.activemq.artemis.api.core.Message in project activemq-artemis by apache.

the class SendAckFailTest method startServer.

public ActiveMQServer startServer(boolean fail) {
    try {
        // ActiveMQServerImpl server = (ActiveMQServerImpl) createServer(true, true);
        AtomicInteger count = new AtomicInteger(0);
        ActiveMQSecurityManager securityManager = new ActiveMQJAASSecurityManager(InVMLoginModule.class.getName(), new SecurityConfiguration());
        Configuration configuration = createDefaultConfig(true);
        ActiveMQServer server = new ActiveMQServerImpl(configuration, ManagementFactory.getPlatformMBeanServer(), securityManager) {

            @Override
            public StorageManager createStorageManager() {
                StorageManager original = super.createStorageManager();
                return new StorageManagerDelegate(original) {

                    @Override
                    public void storeMessage(Message message) throws Exception {
                        if (fail) {
                            if (count.incrementAndGet() == 110) {
                                System.out.println("Failing " + message);
                                System.out.flush();
                                Thread.sleep(100);
                                Runtime.getRuntime().halt(-1);
                            }
                        }
                        super.storeMessage(message);
                    }
                };
            }
        };
        System.out.println("Location::" + server.getConfiguration().getJournalLocation().getAbsolutePath());
        server.start();
        return server;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) Configuration(org.apache.activemq.artemis.core.config.Configuration) SecurityConfiguration(org.apache.activemq.artemis.core.config.impl.SecurityConfiguration) LargeServerMessage(org.apache.activemq.artemis.core.server.LargeServerMessage) PagedMessage(org.apache.activemq.artemis.core.paging.PagedMessage) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(org.apache.activemq.artemis.api.core.Message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActiveMQJAASSecurityManager(org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager) StorageManager(org.apache.activemq.artemis.core.persistence.StorageManager) InVMLoginModule(org.apache.activemq.artemis.spi.core.security.jaas.InVMLoginModule) SecurityConfiguration(org.apache.activemq.artemis.core.config.impl.SecurityConfiguration) ActiveMQServerImpl(org.apache.activemq.artemis.core.server.impl.ActiveMQServerImpl) ActiveMQSecurityManager(org.apache.activemq.artemis.spi.core.security.ActiveMQSecurityManager)

Aggregations

Message (org.apache.activemq.artemis.api.core.Message)114 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)56 Test (org.junit.Test)52 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)51 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)48 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)46 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)41 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)35 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)34 CoreMessage (org.apache.activemq.artemis.core.message.impl.CoreMessage)28 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)18 LargeServerMessage (org.apache.activemq.artemis.core.server.LargeServerMessage)16 ArrayList (java.util.ArrayList)15 MessageReference (org.apache.activemq.artemis.core.server.MessageReference)12 ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)11 ICoreMessage (org.apache.activemq.artemis.api.core.ICoreMessage)11 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)10 Transaction (org.apache.activemq.artemis.core.transaction.Transaction)10 HashMap (java.util.HashMap)9 TransactionImpl (org.apache.activemq.artemis.core.transaction.impl.TransactionImpl)8