Search in sources :

Example 86 with Message

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

the class Redistributor method handle.

@Override
public synchronized HandleStatus handle(final MessageReference reference) throws Exception {
    if (!active) {
        return HandleStatus.BUSY;
    } else if (reference.getMessage().getGroupID() != null) {
        // we shouldn't redistribute with message groups return NO_MATCH so other messages can be delivered
        return HandleStatus.NO_MATCH;
    }
    final Transaction tx = new TransactionImpl(storageManager);
    final Pair<RoutingContext, Message> routingInfo = postOffice.redistribute(reference.getMessage(), queue, tx);
    if (routingInfo == null) {
        return HandleStatus.BUSY;
    }
    if (!reference.getMessage().isLargeMessage()) {
        postOffice.processRoute(routingInfo.getB(), routingInfo.getA(), false);
        ackRedistribution(reference, tx);
    } else {
        active = false;
        executor.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    postOffice.processRoute(routingInfo.getB(), routingInfo.getA(), false);
                    ackRedistribution(reference, tx);
                    synchronized (Redistributor.this) {
                        active = true;
                        count++;
                        queue.deliverAsync();
                    }
                } catch (Exception e) {
                    try {
                        tx.rollback();
                    } catch (Exception e2) {
                        // Nothing much we can do now
                        ActiveMQServerLogger.LOGGER.failedToRollback(e2);
                    }
                }
            }
        });
    }
    return HandleStatus.HANDLED;
}
Also used : RoutingContext(org.apache.activemq.artemis.core.server.RoutingContext) Transaction(org.apache.activemq.artemis.core.transaction.Transaction) Message(org.apache.activemq.artemis.api.core.Message) TransactionImpl(org.apache.activemq.artemis.core.transaction.impl.TransactionImpl)

Example 87 with Message

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

the class CriticalCrashTest method createServer.

ActiveMQServer createServer(String folder) throws Exception {
    final AtomicBoolean blocked = new AtomicBoolean(false);
    Configuration conf = createConfig(folder);
    ActiveMQSecurityManager securityManager = new ActiveMQJAASSecurityManager(InVMLoginModule.class.getName(), new SecurityConfiguration());
    conf.setPersistenceEnabled(true);
    ActiveMQServer server = new ActiveMQServerImpl(conf, securityManager) {

        @Override
        protected StorageManager createStorageManager() {
            JournalStorageManager storageManager = new JournalStorageManager(conf, getCriticalAnalyzer(), executorFactory, scheduledPool, ioExecutorFactory, shutdownOnCriticalIO) {

                @Override
                public void readLock() {
                    super.readLock();
                    if (blocked.get()) {
                        while (true) {
                            try {
                                System.out.println("Blocking forever");
                                Thread.sleep(1000);
                            } catch (Throwable ignored) {
                            }
                        }
                    }
                }

                @Override
                public void storeMessage(Message message) throws Exception {
                    super.storeMessage(message);
                    blocked.set(true);
                }
            };
            this.getCriticalAnalyzer().add(storageManager);
            return storageManager;
        }
    };
    return server;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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) Message(org.apache.activemq.artemis.api.core.Message) ActiveMQJAASSecurityManager(org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager) 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) JournalStorageManager(org.apache.activemq.artemis.core.persistence.impl.journal.JournalStorageManager)

Example 88 with Message

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

the class MQTTUtil method createPubRelMessage.

public static Message createPubRelMessage(MQTTSession session, SimpleString address, int messageId) {
    Message message = createServerMessage(session, address, false, 1);
    message.putIntProperty(MQTTUtil.MQTT_MESSAGE_ID_KEY, messageId);
    message.putIntProperty(MQTTUtil.MQTT_MESSAGE_TYPE_KEY, MqttMessageType.PUBREL.value());
    return message;
}
Also used : CoreMessage(org.apache.activemq.artemis.core.message.impl.CoreMessage) MqttPublishMessage(io.netty.handler.codec.mqtt.MqttPublishMessage) MqttSubscribeMessage(io.netty.handler.codec.mqtt.MqttSubscribeMessage) MqttConnectMessage(io.netty.handler.codec.mqtt.MqttConnectMessage) MqttSubAckMessage(io.netty.handler.codec.mqtt.MqttSubAckMessage) MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) ICoreMessage(org.apache.activemq.artemis.api.core.ICoreMessage) MqttUnsubscribeMessage(io.netty.handler.codec.mqtt.MqttUnsubscribeMessage) Message(org.apache.activemq.artemis.api.core.Message)

Example 89 with Message

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

the class MQTTPublishManager method sendInternal.

/**
 * Sends a message either on behalf of the client or on behalf of the broker (Will Messages)
 * @param messageId
 * @param topic
 * @param qos
 * @param payload
 * @param retain
 * @param internal if true means on behalf of the broker (skips authorisation) and does not return ack.
 * @throws Exception
 */
void sendInternal(int messageId, String topic, int qos, ByteBuf payload, boolean retain, boolean internal) throws Exception {
    synchronized (lock) {
        Message serverMessage = MQTTUtil.createServerMessageFromByteBuf(session, topic, retain, qos, payload);
        if (qos > 0) {
            serverMessage.setDurable(MQTTUtil.DURABLE_MESSAGES);
        }
        if (qos < 2 || !state.getPubRec().contains(messageId)) {
            if (qos == 2 && !internal)
                state.getPubRec().add(messageId);
            Transaction tx = session.getServerSession().newTransaction();
            try {
                if (internal) {
                    session.getServer().getPostOffice().route(serverMessage, tx, true);
                } else {
                    session.getServerSession().send(tx, serverMessage, true, false);
                }
                if (retain) {
                    boolean reset = payload instanceof EmptyByteBuf || payload.capacity() == 0;
                    session.getRetainMessageManager().handleRetainedMessage(serverMessage, topic, reset, tx);
                }
                tx.commit();
            } catch (Throwable t) {
                logger.warn(t.getMessage(), t);
                tx.rollback();
                throw t;
            }
            createMessageAck(messageId, qos, internal);
        }
    }
}
Also used : ICoreMessage(org.apache.activemq.artemis.api.core.ICoreMessage) Message(org.apache.activemq.artemis.api.core.Message) Transaction(org.apache.activemq.artemis.core.transaction.Transaction) EmptyByteBuf(io.netty.buffer.EmptyByteBuf)

Example 90 with Message

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

the class MQTTPublishManager method handlePubRec.

void handlePubRec(int messageId) throws Exception {
    try {
        Pair<Long, Long> ref = outboundStore.publishReceived(messageId);
        if (ref != null) {
            Message m = MQTTUtil.createPubRelMessage(session, getManagementAddress(), messageId);
            session.getServerSession().send(m, true);
            session.getServerSession().individualAcknowledge(ref.getB(), ref.getA());
        } else {
            session.getProtocolHandler().sendPubRel(messageId);
        }
    } catch (ActiveMQIllegalStateException e) {
        log.warn("MQTT Client(" + session.getSessionState().getClientId() + ") attempted to Ack already Ack'd message");
    }
}
Also used : ICoreMessage(org.apache.activemq.artemis.api.core.ICoreMessage) Message(org.apache.activemq.artemis.api.core.Message) ActiveMQIllegalStateException(org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException)

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