Search in sources :

Example 16 with ActiveMQBuffer

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

the class ChannelImpl method sendBlocking.

/**
 * Due to networking issues or server issues the server may take longer to answer than expected.. the client may timeout the call throwing an exception
 * and the client could eventually retry another call, but the server could then answer a previous command issuing a class-cast-exception.
 * The expectedPacket will be used to filter out undesirable packets that would belong to previous calls.
 */
@Override
public Packet sendBlocking(final Packet packet, final int reconnectID, byte expectedPacket) throws ActiveMQException {
    String interceptionResult = invokeInterceptors(packet, interceptors, connection);
    if (interceptionResult != null) {
        if (logger.isTraceEnabled()) {
            logger.trace("RemotingConnectionID=" + (connection == null ? "NULL" : connection.getID()) + " interceptionResult=" + interceptionResult);
        }
        // if we don't throw an exception here the client might not unblock
        throw ActiveMQClientMessageBundle.BUNDLE.interceptorRejectedPacket(interceptionResult);
    }
    if (closed) {
        if (logger.isTraceEnabled()) {
            logger.trace("RemotingConnectionID=" + (connection == null ? "NULL" : connection.getID()) + " closed.");
        }
        throw ActiveMQClientMessageBundle.BUNDLE.connectionDestroyed();
    }
    if (connection.getBlockingCallTimeout() == -1) {
        if (logger.isTraceEnabled()) {
            logger.trace("RemotingConnectionID=" + (connection == null ? "NULL" : connection.getID()) + " Cannot do a blocking call timeout on a server side connection");
        }
        throw new IllegalStateException("Cannot do a blocking call timeout on a server side connection");
    }
    // E.g. blocking acknowledge() from inside a message handler at some time as other operation on main thread
    synchronized (sendBlockingLock) {
        packet.setChannelID(id);
        final ActiveMQBuffer buffer = packet.encode(connection);
        lock.lock();
        try {
            if (failingOver) {
                waitForFailOver("RemotingConnectionID=" + (connection == null ? "NULL" : connection.getID()) + " timed-out waiting for fail-over condition on blocking send");
            }
            response = null;
            if (resendCache != null && packet.isRequiresConfirmations()) {
                addResendPacket(packet);
            }
            checkReconnectID(reconnectID);
            if (logger.isTraceEnabled()) {
                logger.trace("RemotingConnectionID=" + (connection == null ? "NULL" : connection.getID()) + " Sending blocking " + packet);
            }
            connection.getTransportConnection().write(buffer, false, false);
            long toWait = connection.getBlockingCallTimeout();
            long start = System.currentTimeMillis();
            while (!closed && (response == null || (response.getType() != PacketImpl.EXCEPTION && response.getType() != expectedPacket)) && toWait > 0) {
                try {
                    sendCondition.await(toWait, TimeUnit.MILLISECONDS);
                } catch (InterruptedException e) {
                    throw new ActiveMQInterruptedException(e);
                }
                if (response != null && response.getType() != PacketImpl.EXCEPTION && response.getType() != expectedPacket) {
                    ActiveMQClientLogger.LOGGER.packetOutOfOrder(response, new Exception("trace"));
                }
                if (closed) {
                    break;
                }
                final long now = System.currentTimeMillis();
                toWait -= now - start;
                start = now;
            }
            if (closed && toWait > 0 && response == null) {
                Throwable cause = ActiveMQClientMessageBundle.BUNDLE.connectionDestroyed();
                throw ActiveMQClientMessageBundle.BUNDLE.unblockingACall(cause);
            }
            if (response == null) {
                throw ActiveMQClientMessageBundle.BUNDLE.timedOutSendingPacket(connection.getBlockingCallTimeout(), packet.getType());
            }
            if (response.getType() == PacketImpl.EXCEPTION) {
                final ActiveMQExceptionMessage mem = (ActiveMQExceptionMessage) response;
                ActiveMQException e = mem.getException();
                e.fillInStackTrace();
                throw e;
            }
        } finally {
            lock.unlock();
        }
        return response;
    }
}
Also used : ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQExceptionMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.ActiveMQExceptionMessage) ActiveMQInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQInterruptedException) ActiveMQInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQInterruptedException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQInterruptedException) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer)

Example 17 with ActiveMQBuffer

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

the class PacketImpl method encode.

@Override
public ActiveMQBuffer encode(final CoreRemotingConnection connection) {
    ActiveMQBuffer buffer = createPacket(connection);
    encodeHeader(buffer);
    encodeRest(buffer);
    encodeSize(buffer);
    return buffer;
}
Also used : ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer)

Example 18 with ActiveMQBuffer

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

the class OpenWireMessageConverter method inbound.

public static org.apache.activemq.artemis.api.core.Message inbound(final Message messageSend, final WireFormat marshaller, final CoreMessageObjectPools coreMessageObjectPools) throws Exception {
    final CoreMessage coreMessage = new CoreMessage(-1, messageSend.getSize(), coreMessageObjectPools);
    final String type = messageSend.getType();
    if (type != null) {
        coreMessage.putStringProperty(JMS_TYPE_PROPERTY, new SimpleString(type));
    }
    coreMessage.setDurable(messageSend.isPersistent());
    coreMessage.setExpiration(messageSend.getExpiration());
    coreMessage.setPriority(messageSend.getPriority());
    coreMessage.setTimestamp(messageSend.getTimestamp());
    final byte coreType = toCoreType(messageSend.getDataStructureType());
    coreMessage.setType(coreType);
    final ActiveMQBuffer body = coreMessage.getBodyBuffer();
    final ByteSequence contents = messageSend.getContent();
    if (contents == null && coreType == org.apache.activemq.artemis.api.core.Message.TEXT_TYPE) {
        body.writeNullableString(null);
    } else if (contents != null) {
        final boolean messageCompressed = messageSend.isCompressed();
        if (messageCompressed) {
            coreMessage.putBooleanProperty(AMQ_MSG_COMPRESSED, messageCompressed);
        }
        switch(coreType) {
            case org.apache.activemq.artemis.api.core.Message.TEXT_TYPE:
                writeTextType(contents, messageCompressed, body);
                break;
            case org.apache.activemq.artemis.api.core.Message.MAP_TYPE:
                writeMapType(contents, messageCompressed, body);
                break;
            case org.apache.activemq.artemis.api.core.Message.OBJECT_TYPE:
                writeObjectType(contents, messageCompressed, body);
                break;
            case org.apache.activemq.artemis.api.core.Message.STREAM_TYPE:
                writeStreamType(contents, messageCompressed, body);
                break;
            case org.apache.activemq.artemis.api.core.Message.BYTES_TYPE:
                writeBytesType(contents, messageCompressed, body);
                break;
            default:
                writeDefaultType(contents, messageCompressed, body);
                break;
        }
    }
    // amq specific
    coreMessage.putLongProperty(AMQ_MSG_ARRIVAL, messageSend.getArrival());
    coreMessage.putLongProperty(AMQ_MSG_BROKER_IN_TIME, messageSend.getBrokerInTime());
    final BrokerId[] brokers = messageSend.getBrokerPath();
    if (brokers != null) {
        putMsgBrokerPath(brokers, coreMessage);
    }
    final BrokerId[] cluster = messageSend.getCluster();
    if (cluster != null) {
        putMsgCluster(cluster, coreMessage);
    }
    coreMessage.putIntProperty(AMQ_MSG_COMMAND_ID, messageSend.getCommandId());
    final String corrId = messageSend.getCorrelationId();
    if (corrId != null) {
        coreMessage.putStringProperty(JMS_CORRELATION_ID_PROPERTY, new SimpleString(corrId));
    }
    final DataStructure ds = messageSend.getDataStructure();
    if (ds != null) {
        putMsgDataStructure(ds, marshaller, coreMessage);
    }
    final String groupId = messageSend.getGroupID();
    if (groupId != null) {
        coreMessage.putStringProperty(AMQ_MSG_GROUP_ID, coreMessageObjectPools.getGroupIdStringSimpleStringPool().getOrCreate(groupId));
    }
    coreMessage.putIntProperty(AMQ_MSG_GROUP_SEQUENCE, messageSend.getGroupSequence());
    final MessageId messageId = messageSend.getMessageId();
    final ByteSequence midBytes = marshaller.marshal(messageId);
    midBytes.compact();
    coreMessage.putBytesProperty(AMQ_MSG_MESSAGE_ID, midBytes.data);
    final ProducerId producerId = messageSend.getProducerId();
    if (producerId != null) {
        final ByteSequence producerIdBytes = marshaller.marshal(producerId);
        producerIdBytes.compact();
        coreMessage.putBytesProperty(AMQ_MSG_PRODUCER_ID, producerIdBytes.data);
    }
    final ByteSequence propBytes = messageSend.getMarshalledProperties();
    if (propBytes != null) {
        putMsgMarshalledProperties(propBytes, messageSend, coreMessage);
    }
    final ActiveMQDestination replyTo = messageSend.getReplyTo();
    if (replyTo != null) {
        putMsgReplyTo(replyTo, marshaller, coreMessage);
    }
    final String userId = messageSend.getUserID();
    if (userId != null) {
        coreMessage.putStringProperty(AMQ_MSG_USER_ID, new SimpleString(userId));
    }
    coreMessage.putBooleanProperty(AMQ_MSG_DROPPABLE, messageSend.isDroppable());
    final ActiveMQDestination origDest = messageSend.getOriginalDestination();
    if (origDest != null) {
        putMsgOriginalDestination(origDest, marshaller, coreMessage);
    }
    return coreMessage;
}
Also used : BrokerId(org.apache.activemq.command.BrokerId) ProducerId(org.apache.activemq.command.ProducerId) DataStructure(org.apache.activemq.command.DataStructure) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ICoreMessage(org.apache.activemq.artemis.api.core.ICoreMessage) CoreMessage(org.apache.activemq.artemis.core.message.impl.CoreMessage) ByteSequence(org.apache.activemq.util.ByteSequence) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) MessageId(org.apache.activemq.command.MessageId) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination)

Example 19 with ActiveMQBuffer

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

the class OpenWireMessageConverter method toAMQMessage.

private static ActiveMQMessage toAMQMessage(MessageReference reference, ICoreMessage coreMessage, WireFormat marshaller, AMQConsumer consumer) throws IOException {
    final ActiveMQMessage amqMsg;
    final byte coreType = coreMessage.getType();
    final Boolean compressProp = (Boolean) coreMessage.getObjectProperty(AMQ_MSG_COMPRESSED);
    final boolean isCompressed = compressProp == null ? false : compressProp.booleanValue();
    final byte[] bytes;
    final ActiveMQBuffer buffer = coreMessage.getDataBuffer();
    buffer.resetReaderIndex();
    switch(coreType) {
        case org.apache.activemq.artemis.api.core.Message.BYTES_TYPE:
            amqMsg = new ActiveMQBytesMessage();
            bytes = toAMQMessageBytesType(buffer, isCompressed);
            break;
        case org.apache.activemq.artemis.api.core.Message.MAP_TYPE:
            amqMsg = new ActiveMQMapMessage();
            bytes = toAMQMessageMapType(buffer, isCompressed);
            break;
        case org.apache.activemq.artemis.api.core.Message.OBJECT_TYPE:
            amqMsg = new ActiveMQObjectMessage();
            bytes = toAMQMessageObjectType(buffer, isCompressed);
            break;
        case org.apache.activemq.artemis.api.core.Message.STREAM_TYPE:
            amqMsg = new ActiveMQStreamMessage();
            bytes = toAMQMessageStreamType(buffer, isCompressed);
            break;
        case org.apache.activemq.artemis.api.core.Message.TEXT_TYPE:
            amqMsg = new ActiveMQTextMessage();
            bytes = toAMQMessageTextType(buffer, isCompressed);
            break;
        case org.apache.activemq.artemis.api.core.Message.DEFAULT_TYPE:
            amqMsg = new ActiveMQMessage();
            bytes = toAMQMessageDefaultType(buffer, isCompressed);
            break;
        default:
            throw new IllegalStateException("Unknown message type: " + coreMessage.getType());
    }
    final String type = coreMessage.getStringProperty(JMS_TYPE_PROPERTY);
    if (type != null) {
        amqMsg.setJMSType(type);
    }
    amqMsg.setPersistent(coreMessage.isDurable());
    amqMsg.setExpiration(coreMessage.getExpiration());
    amqMsg.setPriority(coreMessage.getPriority());
    amqMsg.setTimestamp(coreMessage.getTimestamp());
    Long brokerInTime = (Long) coreMessage.getObjectProperty(AMQ_MSG_BROKER_IN_TIME);
    if (brokerInTime == null) {
        brokerInTime = 0L;
    }
    amqMsg.setBrokerInTime(brokerInTime);
    amqMsg.setCompressed(isCompressed);
    // we need check null because messages may come from other clients
    // and those amq specific attribute may not be set.
    Long arrival = (Long) coreMessage.getObjectProperty(AMQ_MSG_ARRIVAL);
    if (arrival == null) {
        // messages from other sources (like core client) may not set this prop
        arrival = 0L;
    }
    amqMsg.setArrival(arrival);
    final String brokerPath = (String) coreMessage.getObjectProperty(AMQ_MSG_BROKER_PATH);
    if (brokerPath != null && !brokerPath.isEmpty()) {
        setAMQMsgBrokerPath(amqMsg, brokerPath);
    }
    final String clusterPath = (String) coreMessage.getObjectProperty(AMQ_MSG_CLUSTER);
    if (clusterPath != null && !clusterPath.isEmpty()) {
        setAMQMsgClusterPath(amqMsg, clusterPath);
    }
    Integer commandId = (Integer) coreMessage.getObjectProperty(AMQ_MSG_COMMAND_ID);
    if (commandId == null) {
        commandId = -1;
    }
    amqMsg.setCommandId(commandId);
    final SimpleString corrId = (SimpleString) coreMessage.getObjectProperty(JMS_CORRELATION_ID_PROPERTY);
    if (corrId != null) {
        amqMsg.setCorrelationId(corrId.toString());
    }
    final byte[] dsBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_DATASTRUCTURE);
    if (dsBytes != null) {
        setAMQMsgDataStructure(amqMsg, marshaller, dsBytes);
    }
    final ActiveMQDestination actualDestination = consumer.getOpenwireDestination();
    amqMsg.setDestination(OpenWireUtil.toAMQAddress(coreMessage, actualDestination));
    final Object value = coreMessage.getGroupID();
    if (value != null) {
        String groupId = value.toString();
        amqMsg.setGroupID(groupId);
    }
    Integer groupSequence = (Integer) coreMessage.getObjectProperty(AMQ_MSG_GROUP_SEQUENCE);
    if (groupSequence == null) {
        groupSequence = -1;
    }
    amqMsg.setGroupSequence(groupSequence);
    final MessageId mid;
    final byte[] midBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_MESSAGE_ID);
    if (midBytes != null) {
        ByteSequence midSeq = new ByteSequence(midBytes);
        mid = (MessageId) marshaller.unmarshal(midSeq);
    } else {
        mid = new MessageId(UUIDGenerator.getInstance().generateStringUUID() + ":-1");
    }
    amqMsg.setMessageId(mid);
    final byte[] origDestBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_ORIG_DESTINATION);
    if (origDestBytes != null) {
        setAMQMsgOriginalDestination(amqMsg, marshaller, origDestBytes);
    }
    final byte[] origTxIdBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_ORIG_TXID);
    if (origTxIdBytes != null) {
        setAMQMsgOriginalTransactionId(amqMsg, marshaller, origTxIdBytes);
    }
    final byte[] producerIdBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_PRODUCER_ID);
    if (producerIdBytes != null) {
        ProducerId producerId = (ProducerId) marshaller.unmarshal(new ByteSequence(producerIdBytes));
        amqMsg.setProducerId(producerId);
    }
    final byte[] marshalledBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_MARSHALL_PROP);
    if (marshalledBytes != null) {
        amqMsg.setMarshalledProperties(new ByteSequence(marshalledBytes));
    }
    amqMsg.setRedeliveryCounter(reference.getDeliveryCount() - 1);
    final byte[] replyToBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_REPLY_TO);
    if (replyToBytes != null) {
        setAMQMsgReplyTo(amqMsg, marshaller, replyToBytes);
    }
    final String userId = (String) coreMessage.getObjectProperty(AMQ_MSG_USER_ID);
    if (userId != null) {
        amqMsg.setUserID(userId);
    }
    final Boolean isDroppable = (Boolean) coreMessage.getObjectProperty(AMQ_MSG_DROPPABLE);
    if (isDroppable != null) {
        amqMsg.setDroppable(isDroppable);
    }
    final SimpleString dlqCause = (SimpleString) coreMessage.getObjectProperty(AMQ_MSG_DLQ_DELIVERY_FAILURE_CAUSE_PROPERTY);
    if (dlqCause != null) {
        setAMQMsgDlqDeliveryFailureCause(amqMsg, dlqCause);
    }
    final SimpleString lastValueProperty = coreMessage.getLastValueProperty();
    if (lastValueProperty != null) {
        setAMQMsgHdrLastValueName(amqMsg, lastValueProperty);
    }
    final Set<SimpleString> props = coreMessage.getPropertyNames();
    if (props != null) {
        setAMQMsgObjectProperties(amqMsg, coreMessage, props, consumer);
    }
    if (bytes != null) {
        ByteSequence content = new ByteSequence(bytes);
        amqMsg.setContent(content);
    }
    return amqMsg;
}
Also used : ActiveMQMapMessage(org.apache.activemq.command.ActiveMQMapMessage) ProducerId(org.apache.activemq.command.ProducerId) ActiveMQObjectMessage(org.apache.activemq.command.ActiveMQObjectMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQStreamMessage(org.apache.activemq.command.ActiveMQStreamMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) ActiveMQBytesMessage(org.apache.activemq.command.ActiveMQBytesMessage) ByteSequence(org.apache.activemq.util.ByteSequence) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) MessageId(org.apache.activemq.command.MessageId)

Example 20 with ActiveMQBuffer

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

the class AMQPMessageTest method testExtraProperty.

@Test
public void testExtraProperty() {
    MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
    byte[] original = RandomUtil.randomBytes();
    SimpleString name = SimpleString.toSimpleString("myProperty");
    AMQPMessage decoded = encodeAndDecodeMessage(protonMessage);
    decoded.setAddress("someAddress");
    decoded.setMessageID(33);
    decoded.putExtraBytesProperty(name, original);
    ICoreMessage coreMessage = decoded.toCore();
    Assert.assertEquals(original, coreMessage.getBytesProperty(name));
    ActiveMQBuffer buffer = ActiveMQBuffers.pooledBuffer(10 * 1024);
    try {
        decoded.getPersister().encode(buffer, decoded);
        // the journal reader will read 1 byte to find the persister
        Assert.assertEquals(AMQPMessagePersisterV2.getInstance().getID(), buffer.readByte());
        AMQPMessage readMessage = (AMQPMessage) decoded.getPersister().decode(buffer, null);
        Assert.assertEquals(33, readMessage.getMessageID());
        Assert.assertEquals("someAddress", readMessage.getAddress());
        Assert.assertArrayEquals(original, readMessage.getExtraBytesProperty(name));
    } finally {
        buffer.release();
    }
    {
        ICoreMessage embeddedMessage = EmbedMessageUtil.embedAsCoreMessage(decoded);
        AMQPMessage readMessage = (AMQPMessage) EmbedMessageUtil.extractEmbedded(embeddedMessage);
        Assert.assertEquals(33, readMessage.getMessageID());
        Assert.assertEquals("someAddress", readMessage.getAddress());
        Assert.assertArrayEquals(original, readMessage.getExtraBytesProperty(name));
    }
}
Also used : ICoreMessage(org.apache.activemq.artemis.api.core.ICoreMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) MessageImpl(org.apache.qpid.proton.message.impl.MessageImpl) AMQPMessage(org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Aggregations

ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)150 Test (org.junit.Test)81 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)49 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)44 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)39 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)38 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)35 ByteBuffer (java.nio.ByteBuffer)34 Configuration (org.apache.activemq.artemis.core.config.Configuration)34 StoreConfiguration (org.apache.activemq.artemis.core.config.StoreConfiguration)27 DivertConfiguration (org.apache.activemq.artemis.core.config.DivertConfiguration)25 DatabaseStorageConfiguration (org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration)25 ArrayList (java.util.ArrayList)21 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)21 Queue (org.apache.activemq.artemis.core.server.Queue)18 HashMap (java.util.HashMap)17 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)15 RecordInfo (org.apache.activemq.artemis.core.journal.RecordInfo)15 CoreMessage (org.apache.activemq.artemis.core.message.impl.CoreMessage)14