Example usage for com.rabbitmq.client Channel exchangeDeclare

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

Introduction

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

Prototype

Exchange.DeclareOk exchangeDeclare(String exchange, BuiltinExchangeType type, boolean durable,
        boolean autoDelete, Map<String, Object> arguments) throws IOException;

Source Link

Document

Declare an exchange.

Usage

From source file:com.jbrisbin.vcloud.cache.RabbitMQAsyncCache.java

License:Apache License

@Override
public void start() {
    active.set(true);/* w  w  w. j  a va2s . co m*/

    try {
        Channel channel = getConnection().createChannel();
        channel.exchangeDeclare(objectRequestExchange, "topic", true, false, null);
        channel.queueDeclare(id, true, false, true, null);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }

    // For loading objects
    for (int i = 0; i < maxWorkers; i++) {
        activeTasks.add(workerPool.submit(new ObjectSender()));
        activeTasks.add(workerPool.submit(new CommandSender()));
        workerPool.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    Channel channel = getConnection().createChannel();
                    ObjectLoadMonitor loadMonitor = new ObjectLoadMonitor(channel);
                    channel.basicConsume(id, loadMonitor);
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        });
    }

    activeTasks.add(workerPool.submit(new HeartbeatMonitor()));
    commandMessages.add(new CommandMessage("ping", heartbeatExchange, ""));
    delayTimer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            if (cacheNodes.size() > 0) {
                numOfCacheNodes.set(cacheNodes.size());
            }
        }
    }, 0, heartbeatInterval);
}

From source file:com.jbrisbin.vcloud.cache.RabbitMQAsyncCacheProvider.java

License:Apache License

@Override
public void start() {
    active.set(true);/*from w  ww  . j a  va 2s. co m*/

    try {
        Channel channel = getConnection().createChannel();
        channel.exchangeDeclare(objectRequestExchange, "topic", true, false, null);
        channel.queueDeclare(cacheNodeQueueName, true, false, true, null);
        channel.queueBind(cacheNodeQueueName, objectRequestExchange, "#");
        channel.exchangeDeclare(heartbeatExchange, "fanout", true, false, null);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }

    activeTasks.add(workerPool.submit(new HeartbeatMonitor()));
    delayTimer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            commandMessages.add(new CommandMessage("ping", heartbeatExchange, ""));
        }
    }, 0, heartbeatInterval);
    delayTimer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            numOfPeers.set(peers.size());
            if (debug) {
                log.debug("Expecting responses from " + numOfPeers.get() + " peers.");
            }
        }
    }, heartbeatInterval, heartbeatInterval);

    for (int i = 0; i < maxWorkers; i++) {
        activeTasks.add(workerPool.submit(new ObjectSender()));
        activeTasks.add(workerPool.submit(new CommandSender()));
        workerPool.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    Channel channel = getConnection().createChannel();
                    ObjectMonitor monitor = new ObjectMonitor(channel);
                    channel.basicConsume(cacheNodeQueueName, monitor);
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        });
    }
}

From source file:com.jbrisbin.vcloud.classloader.RabbitMQClassLoader.java

License:Apache License

protected void declareResources(Channel mq) throws IOException {
    synchronized (mq) {
        mq.exchangeDeclare(exchange, "topic", true, false, null);
    }// w  w w. jav  a 2s.  c o  m
}

From source file:com.jbrisbin.vcloud.classloader.RabbitMQResourceProvider.java

License:Apache License

protected void declareResources() throws IOException {
    Channel mq = getChannel();
    synchronized (mq) {
        mq.exchangeDeclare(exchange, "topic", true, false, null);
        if (null == this.queue) {
            queue = mq.queueDeclare().getQueue();
        } else {//from ww  w .j a v  a  2s  .  c  o  m
            mq.queueDeclare(this.queue, true, false, true, null);
        }
        mq.queueBind(queue, exchange, routingKey);
    }
}

From source file:com.nesscomputing.amqp.ExchangeConsumer.java

License:Apache License

@Override
protected void connectCallback(@Nonnull final Channel channel) throws IOException {
    super.connectCallback(channel);

    if (getConfig().isDeclaring()) {
        channel.exchangeDeclare(getName(), getConfig().getExchangeType(), getConfig().isDurable(),
                getConfig().isAutoDelete(), null);
    }//w ww  .j  a va  2 s . c  o m
    final String queueName = channel.queueDeclare().getQueue();

    channel.queueBind(queueName, getName(), routingKey);

    channel.basicConsume(queueName, false, getConsumer());
}

From source file:com.nesscomputing.amqp.ExchangePublisher.java

License:Apache License

@Override
protected void connectCallback(@Nonnull final Channel channel) throws IOException {
    if (getConfig().isDeclaring()) {
        channel.exchangeDeclare(getName(), getConfig().getExchangeType(), getConfig().isDurable(),
                getConfig().isAutoDelete(), null);
    }//from   w  w  w  . ja va  2s  . c  o  m
}

From source file:com.nxttxn.vramel.components.rabbitMQ.RabbitMQEndpoint.java

License:Apache License

/**
 * If needed, declare Exchange, declare Queue and bind them with Routing Key
 *//*w w  w  .  j  ava2  s.  c o m*/
public void declareExchangeAndQueue(Channel channel) throws IOException {
    HashMap<String, Object> queueArgs = null;
    if (deadLetterExchange != null) {
        queueArgs = new HashMap<String, Object>();
        queueArgs.put(RabbitMQConstants.RABBITMQ_DEAD_LETTER_EXCHANGE, getDeadLetterExchange());
        queueArgs.put(RabbitMQConstants.RABBITMQ_DEAD_LETTER_ROUTING_KEY, getDeadLetterRoutingKey());

        channel.exchangeDeclare(getDeadLetterExchange(), getDeadLetterExchangeType(), isDurable(),
                isAutoDelete(), new HashMap<String, Object>());
        channel.queueDeclare(getDeadLetterQueue(), isDurable(), false, isAutoDelete(), null);
        channel.queueBind(getDeadLetterQueue(), getDeadLetterExchange(),
                getDeadLetterRoutingKey() == null ? "" : getDeadLetterRoutingKey());
    }
    channel.exchangeDeclare(getExchangeName(), getExchangeType(), isDurable(), isAutoDelete(),
            new HashMap<String, Object>());
    if (getQueue() != null) {
        // need to make sure the queueDeclare is same with the exchange declare
        channel.queueDeclare(getQueue(), isDurable(), false, isAutoDelete(), queueArgs);
        channel.queueBind(getQueue(), getExchangeName(), getRoutingKey() == null ? "" : getRoutingKey());
    }
}

From source file:com.streamsets.pipeline.lib.rabbitmq.common.RabbitUtil.java

License:Apache License

private static void initRabbitConf(Channel channel, BaseRabbitConfigBean conf) throws IOException {
    // Channel is always bound to the default exchange. When specified, we must declare the exchange.
    for (RabbitExchangeConfigBean exchange : conf.exchanges) {
        channel.exchangeDeclare(exchange.name, exchange.type.getValue(), exchange.durable, exchange.autoDelete,
                exchange.declarationProperties);
    }/*from  w  ww  .  j  av a 2s  .com*/

    channel.queueDeclare(conf.queue.name, conf.queue.durable, conf.queue.exclusive, conf.queue.autoDelete,
            conf.queue.properties);

    for (RabbitExchangeConfigBean exchange : conf.exchanges) {
        bindQueue(channel, conf, exchange);
    }
}

From source file:com.vmware.vhadoop.vhm.rabbit.RabbitConnection.java

License:Open Source License

private synchronized String getQueueName() throws IOException {
    if (_queueName == null) {
        String exchangeName = _credentials.getExchangeName();
        Channel channel = getChannel();
        channel.exchangeDeclare(exchangeName, "direct", true, false, null); /* TODO: Externalize? */
        _queueName = channel.queueDeclare().getQueue();
        channel.queueBind(_queueName, exchangeName, _credentials.getRouteKeyCommand());
        _log.fine("Created transient queue: " + _queueName);
    }//from  w w w  .j a va 2 s  .c om

    return _queueName;
}

From source file:info.pancancer.arch3.utils.Utilities.java

License:Open Source License

public static Channel setupExchange(HierarchicalINIConfiguration settings, String queue)
        throws IOException, TimeoutException {

    String server = settings.getString(Constants.RABBIT_HOST);
    String user = settings.getString(Constants.RABBIT_USERNAME);
    String pass = settings.getString(Constants.RABBIT_PASSWORD);

    Channel channel = null;

    try {/*from w ww .  j a va2s. c  om*/

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(server);
        factory.setUsername(user);
        factory.setPassword(pass);
        factory.setAutomaticRecoveryEnabled(true);
        Connection connection = factory.newConnection();
        channel = connection.createChannel();
        channel.exchangeDeclare(queue, "fanout", true, false, null);
        channel.confirmSelect();

    } catch (IOException | TimeoutException ex) {
        // Logger.getLogger(Master.class.getName()).log(Level.SEVERE, null, ex);
        LOG.error("Error setting up queue connections: " + ex.getMessage(), ex);
        throw ex;
    }
    return channel;
}