Search in sources :

Example 1 with BasicProperties

use of com.rabbitmq.client.BasicProperties in project LinkAgent by shulieTech.

the class ChannelNProcessDeliveryInterceptor method beforeFirst.

@Override
public void beforeFirst(Advice advice) {
    Object[] args = advice.getParameterArray();
    String methodName = advice.getBehaviorName();
    if (!PradarSwitcher.isClusterTestEnabled()) {
        logger.warn("PradarSwitcher isClusterTestEnabled false, {} to start shadow {} skip it", advice.getTargetClass().getName(), methodName);
        return;
    }
    AMQP.Basic.Deliver m = (AMQP.Basic.Deliver) args[1];
    validatePressureMeasurement(m.getConsumerTag());
    try {
        Command command = (Command) args[0];
        BasicProperties contentHeader = (BasicProperties) command.getContentHeader();
        Map<String, Object> headers = contentHeader.getHeaders();
        if (null != headers && headers.get(PradarService.PRADAR_CLUSTER_TEST_KEY) != null && ClusterTestUtils.isClusterTestRequest(headers.get(PradarService.PRADAR_CLUSTER_TEST_KEY).toString())) {
            Pradar.setClusterTest(true);
        }
        if (!Pradar.isClusterTest()) {
            String routingKey = m.getRoutingKey();
            if (StringUtils.isNotBlank(routingKey) && ClusterTestUtils.isClusterTestRequest(routingKey)) {
                Pradar.setClusterTest(true);
            }
            String exchange = m.getExchange();
            if (StringUtils.isNotBlank(exchange) && ClusterTestUtils.isClusterTestRequest(exchange)) {
                Pradar.setClusterTest(true);
            }
        }
    } catch (Throwable e) {
        if (Pradar.isClusterTest()) {
            throw new PressureMeasureError(e);
        }
    }
}
Also used : Deliver(com.rabbitmq.client.impl.AMQImpl.Basic.Deliver) AMQP(com.rabbitmq.client.AMQP) Command(com.rabbitmq.client.Command) BasicProperties(com.rabbitmq.client.BasicProperties) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError)

Example 2 with BasicProperties

use of com.rabbitmq.client.BasicProperties in project rabbitmq-jms-client by rabbitmq.

the class RMQMessage method convertAmqpMessage.

private static RMQMessage convertAmqpMessage(RMQSession session, RMQDestination dest, GetResponse response) throws JMSException {
    try {
        BasicProperties props = response.getProps();
        RMQMessage message = RMQMessage.isAmqpTextMessage(props.getHeaders()) ? new RMQTextMessage() : new RMQBytesMessage();
        // Deserialize the message payload from the byte[] body
        message = RMQMessage.fromAmqpMessage(response.getBody(), message);
        // Insert session in received message for Message.acknowledge
        message.setSession(session);
        // Set the redelivered flag
        message.setJMSRedelivered(response.getEnvelope().isRedeliver());
        // Insert delivery tag in received message for Message.acknowledge
        message.setRabbitDeliveryTag(response.getEnvelope().getDeliveryTag());
        // We cannot know the original destination, so set local one
        message.setJMSDestination(dest);
        message.setJMSPropertiesFromAmqpProperties(props);
        // Set readOnly - mandatory for received messages
        message.setReadonly(true);
        return message;
    } catch (IOException x) {
        throw new RMQJMSException(x);
    }
}
Also used : BasicProperties(com.rabbitmq.client.BasicProperties)

Example 3 with BasicProperties

use of com.rabbitmq.client.BasicProperties in project rabbitmq-jms-client by rabbitmq.

the class RMQMessage method convertAmqpMessage.

private static RMQMessage convertAmqpMessage(RMQSession session, RMQDestination dest, GetResponse response, ReceivingContextConsumer receivingContextConsumer) throws JMSException {
    try {
        BasicProperties props = response.getProps();
        RMQMessage message = RMQMessage.isAmqpTextMessage(props.getHeaders()) ? new RMQTextMessage() : new RMQBytesMessage();
        // Deserialize the message payload from the byte[] body
        message = RMQMessage.fromAmqpMessage(response.getBody(), message);
        // Insert session in received message for Message.acknowledge
        message.setSession(session);
        // Set the redelivered flag
        message.setJMSRedelivered(response.getEnvelope().isRedeliver());
        // Insert delivery tag in received message for Message.acknowledge
        message.setRabbitDeliveryTag(response.getEnvelope().getDeliveryTag());
        // We cannot know the original destination, so set local one
        message.setJMSDestination(dest);
        message.setJMSPropertiesFromAmqpProperties(props);
        // Set readOnly - mandatory for received messages
        message.setReadonly(true);
        maybeSetupDirectReplyTo(message, response.getProps().getReplyTo());
        receivingContextConsumer.accept(new ReceivingContext(message));
        return message;
    } catch (IOException x) {
        throw new RMQJMSException(x);
    }
}
Also used : RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) BasicProperties(com.rabbitmq.client.BasicProperties) RMQTextMessage(com.rabbitmq.jms.client.message.RMQTextMessage) RMQBytesMessage(com.rabbitmq.jms.client.message.RMQBytesMessage) IOException(java.io.IOException)

Example 4 with BasicProperties

use of com.rabbitmq.client.BasicProperties in project LinkAgent by shulieTech.

the class ChannelNProcessDeliveryInterceptor method beforeTrace.

@Override
public SpanRecord beforeTrace(Advice advice) {
    if (ConfigCache.isWorkWithSpring()) {
        return null;
    }
    Object[] args = advice.getParameterArray();
    Command command = (Command) args[0];
    Deliver method = (Deliver) args[1];
    SpanRecord record = new SpanRecord();
    record.setService(method.getExchange());
    record.setMethod(method.getRoutingKey());
    BasicProperties contentHeader = (BasicProperties) command.getContentHeader();
    Map<String, Object> headers = contentHeader.getHeaders();
    if (headers != null) {
        Map<String, String> rpcContext = new HashMap<String, String>();
        for (String key : Pradar.getInvokeContextTransformKeys()) {
            String value = ObjectUtils.toString(headers.get(key));
            if (!StringUtil.isEmpty(value)) {
                rpcContext.put(key, value);
            }
        }
        record.setContext(rpcContext);
    }
    byte[] body = command.getContentBody();
    record.setRequestSize(body.length);
    record.setRequest(body);
    Channel channel = (Channel) advice.getTarget();
    Connection connection = channel.getConnection();
    record.setRemoteIp(connection.getAddress().getHostAddress());
    record.setPort(connection.getPort() + "");
    return record;
}
Also used : Deliver(com.rabbitmq.client.impl.AMQImpl.Basic.Deliver) BasicProperties(com.rabbitmq.client.BasicProperties) HashMap(java.util.HashMap) Channel(com.rabbitmq.client.Channel) AutorecoveringChannel(com.rabbitmq.client.impl.recovery.AutorecoveringChannel) Connection(com.rabbitmq.client.Connection) AutorecoveringConnection(com.rabbitmq.client.impl.recovery.AutorecoveringConnection) AMQConnection(com.rabbitmq.client.impl.AMQConnection) SpanRecord(com.pamirs.pradar.interceptor.SpanRecord) Command(com.rabbitmq.client.Command)

Example 5 with BasicProperties

use of com.rabbitmq.client.BasicProperties in project smallrye-reactive-messaging by smallrye.

the class RabbitMQUsage method produce.

/**
 * Use the supplied function to asynchronously produce messages and write them to the host.
 *
 * @param exchange the exchange, must not be null
 * @param messageCount the number of messages to produce; must be positive
 * @param messageSupplier the function to produce messages; may not be null
 */
void produce(String exchange, String queue, String routingKey, int messageCount, Supplier<Object> messageSupplier) {
    CountDownLatch done = new CountDownLatch(messageCount);
    // Start the machinery to receive the messages
    client.startAndAwait();
    final Thread t = new Thread(() -> {
        LOGGER.infof("Starting RabbitMQ sender to write %s messages with routing key %s", messageCount, routingKey);
        try {
            for (int i = 0; i != messageCount; ++i) {
                final Object payload = messageSupplier.get();
                final Buffer body = Buffer.buffer(payload.toString());
                final BasicProperties properties = new AMQP.BasicProperties().builder().expiration("10000").contentType("text/plain").build();
                client.basicPublish(exchange, routingKey, properties, body).subscribe().with(v -> {
                    LOGGER.infof("Producer sent message %s", payload);
                    done.countDown();
                }, Throwable::printStackTrace);
            }
        } catch (Exception e) {
            LOGGER.error("Unable to send message", e);
        }
        LOGGER.infof("Finished sending %s messages with routing key %s", messageCount, routingKey);
    });
    t.setName(exchange + "-thread");
    t.start();
    try {
        done.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    // Ignore me
    }
}
Also used : Buffer(io.vertx.mutiny.core.buffer.Buffer) BasicProperties(com.rabbitmq.client.BasicProperties) JsonObject(io.vertx.core.json.JsonObject) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException)

Aggregations

BasicProperties (com.rabbitmq.client.BasicProperties)6 AMQP (com.rabbitmq.client.AMQP)2 Command (com.rabbitmq.client.Command)2 Deliver (com.rabbitmq.client.impl.AMQImpl.Basic.Deliver)2 JsonObject (io.vertx.core.json.JsonObject)2 Buffer (io.vertx.mutiny.core.buffer.Buffer)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)1 SpanRecord (com.pamirs.pradar.interceptor.SpanRecord)1 Channel (com.rabbitmq.client.Channel)1 Connection (com.rabbitmq.client.Connection)1 AMQConnection (com.rabbitmq.client.impl.AMQConnection)1 AutorecoveringChannel (com.rabbitmq.client.impl.recovery.AutorecoveringChannel)1 AutorecoveringConnection (com.rabbitmq.client.impl.recovery.AutorecoveringConnection)1 RMQBytesMessage (com.rabbitmq.jms.client.message.RMQBytesMessage)1 RMQTextMessage (com.rabbitmq.jms.client.message.RMQTextMessage)1 RMQJMSException (com.rabbitmq.jms.util.RMQJMSException)1 RabbitMQMessage (io.vertx.mutiny.rabbitmq.RabbitMQMessage)1 Date (java.util.Date)1