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)
        throws IOException;

Source Link

Document

Actively declare a non-autodelete exchange with no extra arguments

Usage

From source file:amqp.AmqpConsumer.java

License:Apache License

/**
 * This method declares the exchange, queue and bindings needed by this consumer.
 * The method returns the queue name that will be consumed from by this class.
 *
 * @param channel channel used to issue AMQP commands
 * @return queue that will have messages consumed from
 * @throws IOException thrown if there is any communication exception
 *///w ww  . j  a  v  a2 s  . c o  m
protected String declarationsForChannel(Channel channel) throws IOException {
    // setup exchange, queue and binding
    channel.exchangeDeclare(exchangeName, exchangeType, durableExchange);
    // named queue?
    if (queueName == null) {
        queueName = channel.queueDeclare().getQueue();

    } else {
        channel.queueDeclare(queueName, durable, exclusive, autoDelete, null);
    }

    if (bindings != null) {
        // multiple bindings
        for (String binding : bindings) {
            channel.queueBind(queueName, exchangeName, binding);
        }
    } else {
        // no binding given - this could be the case if it is a fanout exchange
        channel.queueBind(queueName, exchangeName, SERVER_GENERATED_QUEUE_NAME);
    }

    return queueName;
}

From source file:blocker.Blocker.java

/**
 * @param argv/*from  ww w.j ava  2s .c  o m*/
 */
public static void main(String[] argv) throws Exception {
    seconds = Integer.parseInt(argv[7]);
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(argv[0]);
    factory.setUsername(argv[2]);
    factory.setPassword(argv[3]);
    factory.setVirtualHost(argv[1]);
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(argv[4], "direct", true);
    String queueName = channel.queueDeclare(argv[5], true, false, false, null).getQueue();

    //                          exchange  key
    channel.queueBind(queueName, argv[4], argv[6]);

    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    Consumer consumer = new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            JSONParser parser = new JSONParser();

            Thread.UncaughtExceptionHandler h = new Thread.UncaughtExceptionHandler() {
                @Override
                public void uncaughtException(Thread th, Throwable ex) {
                    System.out.println("Uncaught exception: " + ex);
                }
            };

            try {
                Object obj = parser.parse(message);
                JSONObject jobj = (JSONObject) obj;
                String IP = (String) jobj.get("clientip");
                Thread t = new Thread(new BlockerThread(IP));
                t.setUncaughtExceptionHandler(h);
                t.start();
            } catch (ParseException ex) {
                Logger.getLogger(Blocker.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    };
    channel.basicConsume(argv[5], true, consumer);
}

From source file:br.uff.labtempo.omcp.common.utils.RabbitUtil.java

License:Apache License

public static AMQP.Exchange.DeclareOk declareExchange(Channel channel, String exchangeName) throws IOException {
    return channel.exchangeDeclare(exchangeName, "topic", true);
}

From source file:com.abiquo.commons.amqp.impl.am.AMConfiguration.java

License:Open Source License

@Override
public void declareExchanges(Channel channel) throws IOException {
    channel.exchangeDeclare(AM_EXCHANGE, DirectExchange, Durable);
}

From source file:com.abiquo.commons.amqp.impl.datacenter.DatacenterNotificationConfiguration.java

License:Open Source License

@Override
public void declareExchanges(Channel channel) throws IOException {
    channel.exchangeDeclare(NOTIFICATIONS_EXCHANGE, DirectExchange, Durable);
}

From source file:com.abiquo.commons.amqp.impl.datacenter.DatacenterRequestConfiguration.java

License:Open Source License

@Override
public void declareExchanges(Channel channel) throws IOException {
    channel.exchangeDeclare(getDatacenterExchange(), TopicExchange, Durable);
}

From source file:com.abiquo.commons.amqp.impl.ha.HAConfiguration.java

License:Open Source License

@Override
public void declareExchanges(Channel channel) throws IOException {
    channel.exchangeDeclare(HA_EXCHANGE, DirectExchange, Durable);
}

From source file:com.abiquo.commons.amqp.impl.tracer.TracerConfiguration.java

License:Open Source License

@Override
public void declareExchanges(Channel channel) throws IOException {
    channel.exchangeDeclare(TRACER_EXCHANGE, DirectExchange, Durable);
}

From source file:com.abiquo.commons.amqp.impl.vsm.VSMConfiguration.java

License:Open Source License

@Override
public void declareExchanges(Channel channel) throws IOException {
    channel.exchangeDeclare(VSM_EXCHANGE, FanoutExchange, Durable);
}

From source file:com.att.cspd.SimpleSipServlet.java

License:Open Source License

/**
 * {@inheritDoc}//from  w ww .j  a v a 2  s .  com
 */
protected void doNotify(SipServletRequest request) throws ServletException, IOException {

    Channel channel = null;
    String routingKey = "";

    //a trick to change routingKey value.
    //routingKey = getBindingKey();

    try {

        channel = pool.borrowObject();
        String message = request.getCallId();
        logger.info("doNotify method: Request dump: " + request.toString());
        Iterator itr = request.getHeaderNames();
        while (itr.hasNext()) {
            logger.info("Header Name : " + itr.next() + "\n");
        }
        String toHdr = request.getHeader("To");

        Matcher matcher = Pattern.compile("sip:(.*)@.+").matcher(toHdr);
        if (matcher.find()) {
            String userpart = matcher.group(1);
            logger.info("user part of the sip url : " + userpart);
            routingKey = userpart;
        }

        channel.exchangeDeclare(EXCHANGE_NAME, "topic", true);
        channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
        logger.info("PUBLISH A MESSAGE : " + message);

        logger.info("*************************************");
        logger.info("**" + "Number active : " + pool.getNumActive() + " ***");
        logger.info("**" + "Number idle  : " + pool.getNumIdle() + " ***");
        logger.info("*************************************");

    } catch (NoSuchElementException e) {

        logger.error(e.getMessage());
        throw new ServletException(e);

    } catch (IllegalStateException e) {

        logger.error(e.getMessage());
        throw new ServletException(e);
    } catch (Exception e) {

        logger.error(e.getMessage());
        throw new ServletException(e);
    } finally {
        if (channel != null) {
            try {
                pool.returnObject(channel);
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("Failed to return channel back to pool. Exception message: " + e.getMessage());
            }
            //logger.info("RETURN CHANNEL TO THE POOL");
        }

    }
    SipServletResponse sipServletResponse = request.createResponse(SipServletResponse.SC_OK);
    sipServletResponse.send();

}