Example usage for com.rabbitmq.client BuiltinExchangeType TOPIC

List of usage examples for com.rabbitmq.client BuiltinExchangeType TOPIC

Introduction

In this page you can find the example usage for com.rabbitmq.client BuiltinExchangeType TOPIC.

Prototype

BuiltinExchangeType TOPIC

To view the source code for com.rabbitmq.client BuiltinExchangeType TOPIC.

Click Source Link

Usage

From source file:ms.dew.core.cluster.spi.rabbit.RabbitClusterMQ.java

License:Apache License

/**
 * MQ ??  ?./*from  w ww  . j av a2s. com*/
 * <p>
 * exchange = topic
 *
 * @param topic      
 * @param routingKey Key
 * @param queueName  ??
 * @param message    ?
 * @param confirm    ??
 * @return ???rabbit confirm ?????
 */
public boolean publishWithTopic(String topic, String routingKey, String queueName, String message,
        boolean confirm) {
    logger.trace("[MQ] publishWithTopic {}:{}", topic, message);
    Connection connection = rabbitAdapter.getConnection();
    Channel channel = connection.createChannel(false);
    Object funResult = null;
    try {
        if (confirm) {
            channel.confirmSelect();
        }
        channel.queueDeclare(queueName, true, false, false, null);
        channel.exchangeDeclare(topic, BuiltinExchangeType.TOPIC, true);
        AMQP.BasicProperties properties = new AMQP.BasicProperties("text/plain", null, getMQHeader(topic), 2, 0,
                null, null, null, null, null, null, null, null, null);
        funResult = sendBeforeFun.invoke(topic, routingKey, properties);
        channel.basicPublish(topic, routingKey, properties, message.getBytes());
        if (confirm) {
            try {
                return channel.waitForConfirms();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                logger.error("[MQ] Rabbit publishWithTopic error.", e);
                sendErrorFun.invoke(e, funResult);
                return false;
            }
        } else {
            return true;
        }
    } catch (IOException e) {
        logger.error("[MQ] Rabbit publishWithTopic error.", e);
        sendErrorFun.invoke(e, funResult);
        return false;
    } finally {
        try {
            channel.close();
            sendFinishFun.invoke(funResult);
        } catch (IOException | TimeoutException e) {
            logger.error("[MQ] Rabbit publishWithTopic error.", e);
        }
        connection.close();
    }
}

From source file:ms.dew.core.cluster.spi.rabbit.RabbitClusterMQ.java

License:Apache License

/**
 * MQ ??  .//from   w  w w  .ja  v a  2  s  .c o  m
 * <p>
 * exchange = topic
 * ??
 *
 * @param topic      
 * @param routingKey Key
 * @param queueName  ??
 * @param consumer   ?
 */
public void subscribeWithTopic(String topic, String routingKey, String queueName, Consumer<String> consumer) {
    Channel channel = rabbitAdapter.getConnection().createChannel(false);
    try {
        channel.queueDeclare(queueName, true, false, false, null);
        channel.exchangeDeclare(topic, BuiltinExchangeType.TOPIC, true);
        channel.queueBind(queueName, topic, routingKey);
        channel.basicQos(1);
        channel.basicConsume(queueName, false,
                getDefaultConsumer(channel, topic, topic, routingKey, queueName, consumer));
    } catch (IOException e) {
        logger.error("[MQ] Rabbit subscribeWithTopic error.", e);
    }
}