Example usage for com.rabbitmq.client BuiltinExchangeType FANOUT

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

Introduction

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

Prototype

BuiltinExchangeType FANOUT

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

Click Source Link

Usage

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

License:Apache License

/**
 * MQ ??  ?.//from   w w  w  .j a  va 2  s  .  c o  m
 * <p>
 * exchange = fanout
 * ??? topic ?
 *
 * @param topic   
 * @param message ?
 * @param confirm ??
 * @return ???rabbit confirm ?????
 */
public boolean doPublish(String topic, String message, boolean confirm) {
    Connection connection = rabbitAdapter.getConnection();
    Channel channel = connection.createChannel(false);
    Object funResult = null;
    try {
        if (confirm) {
            channel.confirmSelect();
        }
        channel.exchangeDeclare(topic, BuiltinExchangeType.FANOUT, 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, "", properties);
        channel.basicPublish(topic, "", properties, message.getBytes());
        if (confirm) {
            try {
                return channel.waitForConfirms();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                logger.error("[MQ] Rabbit publish error.", e);
                sendErrorFun.invoke(e, funResult);
                return false;
            }
        } else {
            return true;
        }
    } catch (IOException e) {
        logger.error("[MQ] Rabbit publish error.", e);
        sendErrorFun.invoke(e, funResult);
        return false;
    } finally {
        try {
            channel.close();
            sendFinishFun.invoke(funResult);
        } catch (IOException | TimeoutException e) {
            logger.error("[MQ] Rabbit publish error.", e);
        }
        connection.close();
    }
}

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

License:Apache License

/**
 * MQ ??  .//from  ww  w.  j ava  2  s . c o  m
 * <p>
 * exchange = fanout
 * ??
 *
 * @param topic    
 * @param consumer ?
 */
@Override
protected void doSubscribe(String topic, Consumer<String> consumer) {
    Channel channel = rabbitAdapter.getConnection().createChannel(false);
    try {
        channel.exchangeDeclare(topic, BuiltinExchangeType.FANOUT, true);
        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName, topic, "");
        channel.basicQos(1);
        channel.basicConsume(queueName, false,
                getDefaultConsumer(channel, topic, topic, "", queueName, consumer));
    } catch (IOException e) {
        logger.error("[MQ] Rabbit response error.", e);
    }
}