Search in sources :

Example 6 with StreamException

use of com.rabbitmq.stream.StreamException in project rabbitmq-stream-java-client by rabbitmq.

the class Client method route.

public List<String> route(String routingKey, String superStream) {
    if (routingKey == null || superStream == null) {
        throw new IllegalArgumentException("routing key and stream must not be null");
    }
    int length = 2 + 2 + 4 + 2 + routingKey.length() + 2 + // API code, version, correlation ID, 2 strings
    superStream.length();
    int correlationId = correlationSequence.incrementAndGet();
    try {
        ByteBuf bb = allocate(length + 4);
        bb.writeInt(length);
        bb.writeShort(encodeRequestCode(COMMAND_ROUTE));
        bb.writeShort(VERSION_1);
        bb.writeInt(correlationId);
        bb.writeShort(routingKey.length());
        bb.writeBytes(routingKey.getBytes(StandardCharsets.UTF_8));
        bb.writeShort(superStream.length());
        bb.writeBytes(superStream.getBytes(StandardCharsets.UTF_8));
        OutstandingRequest<List<String>> request = new OutstandingRequest<>(this.rpcTimeout);
        outstandingRequests.put(correlationId, request);
        channel.writeAndFlush(bb);
        request.block();
        return request.response.get();
    } catch (RuntimeException e) {
        outstandingRequests.remove(correlationId);
        throw new StreamException(e);
    }
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) StreamException(com.rabbitmq.stream.StreamException)

Example 7 with StreamException

use of com.rabbitmq.stream.StreamException in project rabbitmq-stream-java-client by rabbitmq.

the class SwiftMqCodec method encode.

@Override
public EncodedMessage encode(Message message) {
    AMQPMessage outboundMessage;
    if (message instanceof SwiftMqAmqpMessageWrapper) {
        outboundMessage = ((SwiftMqAmqpMessageWrapper) message).message;
    } else {
        outboundMessage = new AMQPMessage();
        if (message.getProperties() != null) {
            com.rabbitmq.stream.Properties headers = message.getProperties();
            com.swiftmq.amqp.v100.generated.messaging.message_format.Properties properties = new com.swiftmq.amqp.v100.generated.messaging.message_format.Properties();
            boolean propertiesSet = false;
            if (headers.getMessageId() != null) {
                if (headers.getMessageId() instanceof String) {
                    properties.setMessageId(new MessageIdString(headers.getMessageIdAsString()));
                } else if (headers.getMessageId().getClass().isPrimitive() || headers.getMessageId() instanceof Long) {
                    properties.setMessageId(new MessageIdUlong(headers.getMessageIdAsLong()));
                } else if (headers.getMessageId().getClass().isArray()) {
                    properties.setMessageId(new MessageIdBinary(headers.getMessageIdAsBinary()));
                } else if (headers.getMessageId() instanceof UUID) {
                    properties.setMessageId(new MessageIdUuid(headers.getMessageIdAsUuid()));
                } else {
                    throw new IllegalStateException("Type not supported for message ID:" + properties.getMessageId().getClass());
                }
                propertiesSet = true;
            }
            if (headers.getUserId() != null) {
                properties.setUserId(new AMQPBinary(headers.getUserId()));
                propertiesSet = true;
            }
            if (headers.getTo() != null) {
                properties.setTo(new AddressString(headers.getTo()));
                propertiesSet = true;
            }
            if (headers.getSubject() != null) {
                properties.setSubject(new AMQPString(headers.getSubject()));
                propertiesSet = true;
            }
            if (headers.getReplyTo() != null) {
                properties.setReplyTo(new AddressString(headers.getReplyTo()));
                propertiesSet = true;
            }
            if (headers.getCorrelationId() != null) {
                if (headers.getCorrelationId() instanceof String) {
                    properties.setCorrelationId(new MessageIdString(headers.getCorrelationIdAsString()));
                } else if (headers.getCorrelationId().getClass().isPrimitive() || headers.getCorrelationId() instanceof Long) {
                    properties.setCorrelationId(new MessageIdUlong(headers.getCorrelationIdAsLong()));
                } else if (headers.getCorrelationId().getClass().isArray()) {
                    properties.setCorrelationId(new MessageIdBinary(headers.getCorrelationIdAsBinary()));
                } else if (headers.getCorrelationId() instanceof UUID) {
                    properties.setCorrelationId(new MessageIdUuid(headers.getCorrelationIdAsUuid()));
                } else {
                    throw new IllegalStateException("Type not supported for correlation ID:" + properties.getCorrelationId().getClass());
                }
                propertiesSet = true;
            }
            if (headers.getContentType() != null) {
                properties.setContentType(new AMQPSymbol(headers.getContentType()));
                propertiesSet = true;
            }
            if (headers.getContentEncoding() != null) {
                properties.setContentEncoding(new AMQPSymbol(headers.getContentEncoding()));
                propertiesSet = true;
            }
            if (headers.getAbsoluteExpiryTime() > 0) {
                properties.setAbsoluteExpiryTime(new AMQPTimestamp(headers.getAbsoluteExpiryTime()));
                propertiesSet = true;
            }
            if (headers.getCreationTime() > 0) {
                properties.setCreationTime(new AMQPTimestamp(headers.getCreationTime()));
                propertiesSet = true;
            }
            if (headers.getGroupId() != null) {
                properties.setGroupId(new AMQPString(headers.getGroupId()));
                propertiesSet = true;
            }
            if (headers.getGroupSequence() >= 0) {
                properties.setGroupSequence(new SequenceNo(headers.getGroupSequence()));
                propertiesSet = true;
            }
            if (headers.getReplyToGroupId() != null) {
                properties.setReplyToGroupId(new AMQPString(headers.getReplyToGroupId()));
                propertiesSet = true;
            }
            if (propertiesSet) {
                outboundMessage.setProperties(properties);
            }
        }
        if (message.getApplicationProperties() != null && !message.getApplicationProperties().isEmpty()) {
            Map<AMQPType, AMQPType> applicationProperties = new LinkedHashMap<>(message.getApplicationProperties().size());
            for (Map.Entry<String, Object> entry : message.getApplicationProperties().entrySet()) {
                applicationProperties.put(new AMQPString(entry.getKey()), convertToSwiftMqType(entry.getValue()));
            }
            try {
                outboundMessage.setApplicationProperties(new ApplicationProperties(applicationProperties));
            } catch (IOException e) {
                throw new StreamException("Error while setting application properties", e);
            }
        }
        if (message.getMessageAnnotations() != null && !message.getMessageAnnotations().isEmpty()) {
            Map<AMQPType, AMQPType> messageAnnotations = new LinkedHashMap<>(message.getMessageAnnotations().size());
            for (Map.Entry<String, Object> entry : message.getMessageAnnotations().entrySet()) {
                messageAnnotations.put(new AMQPSymbol(entry.getKey()), convertToSwiftMqType(entry.getValue()));
            }
            try {
                outboundMessage.setMessageAnnotations(new MessageAnnotations(messageAnnotations));
            } catch (IOException e) {
                throw new StreamException("Error while setting message annotations", e);
            }
        }
        if (message.getBodyAsBinary() != null) {
            outboundMessage.addData(new Data(message.getBodyAsBinary()));
        }
    }
    try {
        int bufferSize;
        // FIXME estimate the size with all message data
        if (outboundMessage.getData() != null && !outboundMessage.getData().isEmpty()) {
            bufferSize = outboundMessage.getData().get(0).getPredictedSize();
        } else {
            bufferSize = 8192;
        }
        DataByteArrayOutputStream output = new DataByteArrayOutputStream(bufferSize);
        outboundMessage.writeContent(output);
        return new EncodedMessage(output.getCount(), output.getBuffer());
    } catch (IOException e) {
        throw new StreamException("Error while writing AMQP 1.0 message to output stream");
    }
}
Also used : SequenceNo(com.swiftmq.amqp.v100.generated.transport.definitions.SequenceNo) DataByteArrayOutputStream(com.swiftmq.tools.util.DataByteArrayOutputStream) com.swiftmq.amqp.v100.generated.messaging.message_format(com.swiftmq.amqp.v100.generated.messaging.message_format) LinkedHashMap(java.util.LinkedHashMap) StreamException(com.rabbitmq.stream.StreamException) UUID(java.util.UUID) com.rabbitmq.stream.amqp(com.rabbitmq.stream.amqp) IOException(java.io.IOException) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 8 with StreamException

use of com.rabbitmq.stream.StreamException in project rabbitmq-stream-java-client by rabbitmq.

the class StreamProducer method send.

@Override
public void send(Message message, ConfirmationHandler confirmationHandler) {
    if (confirmationHandler == null) {
        confirmationHandler = NO_OP_CONFIRMATION_HANDLER;
    }
    try {
        if (canSend()) {
            if (this.blockOnMaxUnconfirmed) {
                unconfirmedMessagesSemaphore.acquire();
                doSend(message, confirmationHandler);
            } else {
                if (unconfirmedMessagesSemaphore.tryAcquire(this.enqueueTimeoutMs, TimeUnit.MILLISECONDS)) {
                    doSend(message, confirmationHandler);
                } else {
                    confirmationHandler.handle(new ConfirmationStatus(message, false, CODE_MESSAGE_ENQUEUEING_FAILED));
                }
            }
        } else {
            failPublishing(message, confirmationHandler);
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new StreamException("Interrupted while waiting to accumulate outbound message", e);
    }
}
Also used : ConfirmationStatus(com.rabbitmq.stream.ConfirmationStatus) StreamException(com.rabbitmq.stream.StreamException)

Example 9 with StreamException

use of com.rabbitmq.stream.StreamException in project rabbitmq-stream-java-client by rabbitmq.

the class Client method create.

public Response create(String stream, Map<String, String> arguments) {
    int length = 2 + 2 + 4 + 2 + stream.length() + 4;
    for (Map.Entry<String, String> argument : arguments.entrySet()) {
        length = length + 2 + argument.getKey().length() + 2 + argument.getValue().length();
    }
    int correlationId = correlationSequence.incrementAndGet();
    try {
        ByteBuf bb = allocate(length + 4);
        bb.writeInt(length);
        bb.writeShort(encodeRequestCode(COMMAND_CREATE_STREAM));
        bb.writeShort(VERSION_1);
        bb.writeInt(correlationId);
        bb.writeShort(stream.length());
        bb.writeBytes(stream.getBytes(StandardCharsets.UTF_8));
        bb.writeInt(arguments.size());
        for (Map.Entry<String, String> argument : arguments.entrySet()) {
            bb.writeShort(argument.getKey().length());
            bb.writeBytes(argument.getKey().getBytes(StandardCharsets.UTF_8));
            bb.writeShort(argument.getValue().length());
            bb.writeBytes(argument.getValue().getBytes(StandardCharsets.UTF_8));
        }
        OutstandingRequest<Response> request = new OutstandingRequest<>(this.rpcTimeout);
        outstandingRequests.put(correlationId, request);
        channel.writeAndFlush(bb);
        request.block();
        return request.response.get();
    } catch (RuntimeException e) {
        outstandingRequests.remove(correlationId);
        throw new StreamException(e);
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) StreamException(com.rabbitmq.stream.StreamException)

Example 10 with StreamException

use of com.rabbitmq.stream.StreamException in project rabbitmq-stream-java-client by rabbitmq.

the class Client method declarePublisher.

public Response declarePublisher(byte publisherId, String publisherReference, String stream) {
    int publisherReferenceSize = (publisherReference == null || publisherReference.isEmpty() ? 0 : publisherReference.length());
    if (publisherReferenceSize > 256) {
        throw new IllegalArgumentException("If specified, publisher reference must less than 256 characters");
    }
    int length = 2 + 2 + 4 + 1 + 2 + publisherReferenceSize + 2 + stream.length();
    int correlationId = correlationSequence.getAndIncrement();
    try {
        ByteBuf bb = allocate(length + 4);
        bb.writeInt(length);
        bb.writeShort(encodeRequestCode(COMMAND_DECLARE_PUBLISHER));
        bb.writeShort(VERSION_1);
        bb.writeInt(correlationId);
        bb.writeByte(publisherId);
        bb.writeShort(publisherReferenceSize);
        if (publisherReferenceSize > 0) {
            bb.writeBytes(publisherReference.getBytes(StandardCharsets.UTF_8));
        }
        bb.writeShort(stream.length());
        bb.writeBytes(stream.getBytes(StandardCharsets.UTF_8));
        OutstandingRequest<Response> request = new OutstandingRequest<>(this.rpcTimeout);
        outstandingRequests.put(correlationId, request);
        channel.writeAndFlush(bb);
        request.block();
        return request.response.get();
    } catch (RuntimeException e) {
        outstandingRequests.remove(correlationId);
        throw new StreamException(e);
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) StreamException(com.rabbitmq.stream.StreamException)

Aggregations

StreamException (com.rabbitmq.stream.StreamException)29 ByteBuf (io.netty.buffer.ByteBuf)17 Map (java.util.Map)9 List (java.util.List)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 Duration (java.time.Duration)5 ArrayList (java.util.ArrayList)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 Constants (com.rabbitmq.stream.Constants)4 OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)4 HashMap (java.util.HashMap)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 ExecutorService (java.util.concurrent.ExecutorService)4 Executors (java.util.concurrent.Executors)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 BackOffDelayPolicy (com.rabbitmq.stream.BackOffDelayPolicy)3 ConfirmationHandler (com.rabbitmq.stream.ConfirmationHandler)3 ConfirmationStatus (com.rabbitmq.stream.ConfirmationStatus)3 Environment (com.rabbitmq.stream.Environment)3