Example usage for com.rabbitmq.client Channel basicPublish

List of usage examples for com.rabbitmq.client Channel basicPublish

Introduction

In this page you can find the example usage for com.rabbitmq.client Channel basicPublish.

Prototype

void basicPublish(String exchange, String routingKey, boolean mandatory, boolean immediate,
        BasicProperties props, byte[] body) throws IOException;

Source Link

Document

Publish a message.

Usage

From source file:com.navercorp.pinpoint.plugin.jdk7.rabbitmq.RabbitMQTestRunner.java

License:Apache License

void runPushTest() throws Exception {

    final String message = "hello rabbit mq";

    // producer side
    final Connection producerConnection = connectionFactory.newConnection();
    final Channel producerChannel = producerConnection.createChannel();

    producerChannel.exchangeDeclare(RabbitMQTestConstants.EXCHANGE, "direct", false);
    producerChannel.queueDeclare(RabbitMQTestConstants.QUEUE_PUSH, false, false, false, null);
    producerChannel.queueBind(RabbitMQTestConstants.QUEUE_PUSH, RabbitMQTestConstants.EXCHANGE,
            RabbitMQTestConstants.ROUTING_KEY_PUSH);

    AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
    producerChannel.basicPublish(RabbitMQTestConstants.EXCHANGE, RabbitMQTestConstants.ROUTING_KEY_PUSH, false,
            false, builder.appId("test").build(), message.getBytes());

    producerChannel.close();//from w ww.j  av  a 2s .com
    producerConnection.close();

    //comsumer side
    final Connection consumerConnection = connectionFactory.newConnection();
    final Channel consumerChannel = consumerConnection.createChannel();
    final String remoteAddress = consumerConnection.getAddress().getHostAddress() + ":"
            + consumerConnection.getPort();

    consumerChannel.queueDeclare(RabbitMQTestConstants.QUEUE_PUSH, false, false, false, null);

    TestConsumer<String> consumer = new TestConsumer<String>(consumerChannel, MessageConverter.FOR_TEST);
    consumerChannel.basicConsume(RabbitMQTestConstants.QUEUE_PUSH, true, consumer);

    // wait consumer
    Assert.assertEquals(message, consumer.getMessage(10, TimeUnit.SECONDS));

    consumerChannel.close();
    consumerConnection.close();

    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    // Wait till all traces are recorded (consumer traces are recorded from another thread)
    awaitAndVerifyTraceCount(verifier, 6, 5000L);

    verifier.printCache();
    Class<?> producerChannelClass = producerChannel.getClass();
    Method channelBasicPublish = producerChannelClass.getDeclaredMethod("basicPublish", String.class,
            String.class, boolean.class, boolean.class, AMQP.BasicProperties.class, byte[].class);
    ExpectedTrace channelBasicPublishTrace = Expectations.event(RabbitMQTestConstants.RABBITMQ_CLIENT, // serviceType
            channelBasicPublish, // method
            null, // rpc
            remoteAddress, // endPoint
            "exchange-" + RabbitMQTestConstants.EXCHANGE, // destinationId
            Expectations.annotation("rabbitmq.exchange", RabbitMQTestConstants.EXCHANGE),
            Expectations.annotation("rabbitmq.routingkey", RabbitMQTestConstants.ROUTING_KEY_PUSH));
    ExpectedTrace rabbitMqConsumerInvocationTrace = Expectations.root(RabbitMQTestConstants.RABBITMQ_CLIENT, // serviceType
            "RabbitMQ Consumer Invocation", // method
            "rabbitmq://exchange=" + RabbitMQTestConstants.EXCHANGE, // rpc
            null, // endPoint (collected but API to retrieve local address is not available in all versions, so skip)
            remoteAddress, // remoteAddress
            Expectations.annotation("rabbitmq.routingkey", RabbitMQTestConstants.ROUTING_KEY_PUSH));
    Class<?> consumerDispatchClass = Class.forName("com.rabbitmq.client.impl.ConsumerDispatcher");
    Method consumerDispatchHandleDelivery = consumerDispatchClass.getDeclaredMethod("handleDelivery",
            Consumer.class, String.class, Envelope.class, AMQP.BasicProperties.class, byte[].class);
    ExpectedTrace consumerDispatcherHandleDeliveryTrace = Expectations
            .event(RabbitMQTestConstants.RABBITMQ_CLIENT_INTERNAL, consumerDispatchHandleDelivery); // method
    ExpectedTrace asynchronousInvocationTrace = Expectations.event(ServiceType.ASYNC.getName(),
            "Asynchronous Invocation");
    Class<?> consumerClass = consumer.getClass();
    Method consumerHandleDelivery = consumerClass.getDeclaredMethod("handleDelivery", String.class,
            Envelope.class, AMQP.BasicProperties.class, byte[].class);
    ExpectedTrace consumerHandleDeliveryTrace = Expectations
            .event(RabbitMQTestConstants.RABBITMQ_CLIENT_INTERNAL, consumerHandleDelivery);
    Class<?> propagationMarkerClass = PropagationMarker.class;
    Method propagationMarkerMark = propagationMarkerClass.getDeclaredMethod("mark");
    ExpectedTrace markTrace = Expectations.event(ServiceType.INTERNAL_METHOD.getName(), propagationMarkerMark);
    verifier.verifyTrace(channelBasicPublishTrace, rabbitMqConsumerInvocationTrace,
            consumerDispatcherHandleDeliveryTrace, asynchronousInvocationTrace, consumerHandleDeliveryTrace,
            markTrace);
    verifier.verifyTraceCount(0);
}

From source file:com.navercorp.pinpoint.plugin.jdk7.rabbitmq.RabbitMQTestRunner.java

License:Apache License

void runPullTest() throws Exception {

    final String message = "hello rabbit mq";

    // producer side
    final Connection producerConnection = connectionFactory.newConnection();
    final Channel producerChannel = producerConnection.createChannel();

    producerChannel.exchangeDeclare(RabbitMQTestConstants.EXCHANGE, "direct", false);
    producerChannel.queueDeclare(RabbitMQTestConstants.QUEUE_PULL, false, false, false, null);
    producerChannel.queueBind(RabbitMQTestConstants.QUEUE_PULL, RabbitMQTestConstants.EXCHANGE,
            RabbitMQTestConstants.ROUTING_KEY_PULL);

    AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
    producerChannel.basicPublish(RabbitMQTestConstants.EXCHANGE, RabbitMQTestConstants.ROUTING_KEY_PULL, false,
            false, builder.appId("test").build(), message.getBytes());

    producerChannel.close();//from   w w  w  .ja  v  a 2 s.c  o m
    producerConnection.close();

    //comsumer side
    final Connection consumerConnection = connectionFactory.newConnection();
    final Channel consumerChannel = consumerConnection.createChannel();
    final String remoteAddress = consumerConnection.getAddress().getHostAddress() + ":"
            + consumerConnection.getPort();

    TestMessagePuller messagePuller = new TestMessagePuller(consumerChannel);
    Assert.assertEquals(message,
            messagePuller.pullMessage(MessageConverter.FOR_TEST, RabbitMQTestConstants.QUEUE_PULL, true));

    consumerChannel.close();
    consumerConnection.close();

    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    // Wait till all traces are recorded (consumer traces are recorded from another thread)
    awaitAndVerifyTraceCount(verifier, 5, 5000L);

    verifier.printCache();
    // verify producer traces
    Class<?> producerChannelClass = producerChannel.getClass();
    Method channelBasicPublish = producerChannelClass.getDeclaredMethod("basicPublish", String.class,
            String.class, boolean.class, boolean.class, AMQP.BasicProperties.class, byte[].class);
    ExpectedTrace channelBasicPublishTrace = Expectations.event(RabbitMQTestConstants.RABBITMQ_CLIENT, // serviceType
            channelBasicPublish, // method
            null, // rpc
            remoteAddress, // endPoint
            "exchange-" + RabbitMQTestConstants.EXCHANGE, // destinationId
            Expectations.annotation("rabbitmq.exchange", RabbitMQTestConstants.EXCHANGE),
            Expectations.annotation("rabbitmq.routingkey", RabbitMQTestConstants.ROUTING_KEY_PULL));
    ExpectedTrace rabbitMqConsumerInvocationTrace = Expectations.root(RabbitMQTestConstants.RABBITMQ_CLIENT, // serviceType
            "RabbitMQ Consumer Invocation", // method
            "rabbitmq://exchange=" + RabbitMQTestConstants.EXCHANGE, // rpc
            null, // endPoint (collected but API to retrieve local address is not available in all versions, so skip)
            remoteAddress, // remoteAddress
            Expectations.annotation("rabbitmq.routingkey", RabbitMQTestConstants.ROUTING_KEY_PULL));
    Class<?> amqChannelClass = Class.forName("com.rabbitmq.client.impl.AMQChannel");
    Method handleCompleteInboundCommand = amqChannelClass.getDeclaredMethod("handleCompleteInboundCommand",
            AMQCommand.class);
    ExpectedTrace handleCompleteInboundCommandTrace = Expectations.event(
            RabbitMQTestConstants.RABBITMQ_CLIENT_INTERNAL, // serviceType
            handleCompleteInboundCommand); // method
    verifier.verifyDiscreteTrace(channelBasicPublishTrace, rabbitMqConsumerInvocationTrace,
            handleCompleteInboundCommandTrace);

    // verify consumer traces
    Class<?> consumerChannelClass = consumerChannel.getClass();
    Method channelBasicGet = consumerChannelClass.getDeclaredMethod("basicGet", String.class, boolean.class);
    ExpectedTrace channelBasicGetTrace = Expectations.event(RabbitMQTestConstants.RABBITMQ_CLIENT_INTERNAL,
            channelBasicGet);
    Class<?> propagationMarkerClass = PropagationMarker.class;
    Method propagationMarkerMark = propagationMarkerClass.getDeclaredMethod("mark");
    ExpectedTrace markTrace = Expectations.event(ServiceType.INTERNAL_METHOD.getName(), propagationMarkerMark);
    verifier.verifyDiscreteTrace(channelBasicGetTrace, markTrace);
    verifier.verifyTraceCount(0);
}

From source file:org.axonframework.amqp.eventhandling.spring.SpringAMQPPublisher.java

License:Apache License

/**
 * Does the actual publishing of the given {@code body} on the given {@code channel}. This method can be
 * overridden to change the properties used to send a message.
 *
 * @param channel     The channel to dispatch the message on
 * @param amqpMessage The AMQPMessage describing the characteristics of the message to publish
 * @throws java.io.IOException when an error occurs while writing the message
 *///  w w  w . jav a2s  .c  om
protected void doSendMessage(Channel channel, AMQPMessage amqpMessage) throws IOException {
    channel.basicPublish(exchangeName, amqpMessage.getRoutingKey(), amqpMessage.isMandatory(),
            amqpMessage.isImmediate(), amqpMessage.getProperties(), amqpMessage.getBody());
}

From source file:org.mule.transport.amqp.internal.endpoint.dispatcher.DispatcherActionDispatch.java

License:Open Source License

public AmqpMessage run(final AmqpConnector amqpConnector, final Channel channel, final String exchange,
        final String routingKey, final AmqpMessage amqpMessage, final long timeout) throws IOException {
    channel.basicPublish(exchange, routingKey, amqpConnector.isMandatory(), amqpConnector.isImmediate(),
            amqpMessage.getProperties(), amqpMessage.getBody());

    return null;/* w  ww.j a  va  2 s. c o m*/
}